How to display data in RecylerView within a Bottom Sheet

30 Views Asked by At

I get the data from database. I created the bottom sheet when i click i can see the layout however i cannot insert my data to the recylerview. I want to display the data in the recylerview in the bottom sheet. I can get the data to the arraylist but can not show it on the screen or pass it to the model. I tried to paste it in different lines but didnt work. Maybe my model can be wrong. I would be really happy if you could help.

---This is the activity ---

public class ContactButtonActivity extends AppCompatActivity {

    private ActivityContactButtonBinding binding;
    public Dialog dialog;
    public ContactEntity tuana;
    public ContactEntity liray;
    private ArrayList<Contact> contacts;
    public ContactRoomAdapter contactRoomAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityContactButtonBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        tuana = new ContactEntity("Tuana", "5432345678");
        liray = new ContactEntity("Liray", "5678905346");

        contacts = new ArrayList<>();
        getContacts();

    }

    public void getContacts(){
        ContactDatabase contactDatabase = Room.databaseBuilder(getApplicationContext(), ContactDatabase.class, "contact-database").allowMainThreadQueries().build();
        contactDatabase.contactDao().insertAll(tuana,liray);
        List<ContactEntity> contactListo = contactDatabase.contactDao().getAllContacts();

        for(ContactEntity list : contactListo){

            String name = list.getName();
            String number = list.getNumber();

            Contact contact = new Contact(name, number);
            System.out.println(contact);
            contacts.add(contact);
        }
        contactRoomAdapter.notifyDataSetChanged();

    }

    public void bottomSheetShow(View view){
        dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.bottomsheetlayout);


        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        contactRoomAdapter = new ContactRoomAdapter(contacts);
        recyclerView.setAdapter(contactRoomAdapter);

        dialog.show();
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
        dialog.getWindow().setGravity(Gravity.BOTTOM);
    }
}

---This is the bottomsheet layout ---

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/dialog_bg"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="409dp"
        android:layout_height="370dp" />
</LinearLayout>

--- This is the Adapter ---

public class ContactRoomAdapter extends RecyclerView.Adapter<ContactRoomAdapter.ContactRoomHolder> {

    private ArrayList<Contact> contactArrayList;


    public ContactRoomAdapter(ArrayList<Contact> contactArrayList) {
        this.contactArrayList = contactArrayList;
    }

    @NonNull
    @Override
    public ContactRoomHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerRowBinding recyclerRowBinding = RecyclerRowBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ContactRoomHolder(recyclerRowBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull ContactRoomAdapter.ContactRoomHolder holder, int position) {
        holder.recyclerRowBinding.recyclerviewRowNameText.setText(contactArrayList.get(position).name);
        holder.recyclerRowBinding.recyclerviewRowNumberText.setText(contactArrayList.get(position).number);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(holder.itemView.getContext(), ContactDetailsActivity.class);
                intent.putExtra("contactName", contactArrayList.get(position).name);
                intent.putExtra("contactNumber", contactArrayList.get(position).number);
                holder.itemView.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return contactArrayList.size();
    }

    class ContactRoomHolder extends RecyclerView.ViewHolder{

        RecyclerRowBinding recyclerRowBinding;

        public ContactRoomHolder(@NonNull RecyclerRowBinding recylerRowBinding) {
            super(recylerRowBinding.getRoot());
            this.recyclerRowBinding = recylerRowBinding;
        }
    }
}

I want to display the data in the recylerview in the bottom sheet. I can get the data to the arraylist but can not show it on the screen or pass it to the model. I tried to paste it in different lines but didnt work. Maybe my model can be wrong. I would be really happy if you could help.

0

There are 0 best solutions below