Search code examples
androidandroid-recyclerviewpicasso

How to use bind function of groupie library in java?


I am trying to show a list of user in recyclerView and trying to connect the textview layout in bind method in groupie library and i don't know how to link the id of layout to recyclerview viewHolder? and also how to use picasso library in viewholder?

private void fetchUser(){
DatabaseReference fireBaseReference = FirebaseDatabase.getInstance().getReference().child("/userList");
     fireBaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
         @Override
         public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
             GroupAdapter groupA = new GroupAdapter<ViewHolder>();
             for (DataSnapshot snapshot : dataSnapshot.getChildren()){

                 Log.d("userList",snapshot.toString());
                 Users string = snapshot.getValue(Users.class);
                 groupA.add(new UserItem());


             }
             recyclerView.setAdapter(groupA);


         }

         @Override
         public void onCancelled(@NonNull DatabaseError databaseError) {

         }
     });

}
}
    class  UserItem extends Item<ViewHolder>{



//    public  UserItem(Users users){

//    }
 private   Users users = new Users();

@Override
public void bind(@NonNull ViewHolder viewHolder, int position) {

     viewHolder.itemView.findViewById(R.id.user_name_from_user_list);
     viewHolder.

     Picasso.get().load(users.getUri()).into(viewHolder.itemView.findViewById(R.id.user_photo_from_user_list));




}

@Override
public int getLayout() {
     return R.layout.user_list_view_layout;
}
}

Solution

  • Groupie abstracts away the complexity of multiple item view types. Each Item declares a view layout id, and gets a callback to bind the inflated layout. That's all you need; you can add your new item directly to a GroupAdapter and call it a day.

    Item with Kotlin The Item class gives you simple callbacks to bind your model object to the generated fields. Because of Kotlin Android extensions, there's no need to write a view holder.**

    class SongItem(private val song: Song) : Item() {
    
    override fun getLayout() = R.layout.song
    
    override fun bind(viewHolder: GroupieViewHolder, position: Int) {
        viewHolder.title.text = song.title
        viewHolder.artist.text = song.artist
        }
    }
    

    Item with data binding: The Item class gives you simple callbacks to bind your model object to the generated binding. Because of data binding, there's no need to write a view holder.

    If you're converting existing ViewHolders, you can reference any named views (e.g. R.id.title) directly from the binding instead.

    @Override public void bind(SongBinding binding, int position) {
        binding.title.setText(song.getTitle());
    }
    

    or you can do this way

     @Override
    public void bind(@NonNull final ViewHolder viewHolder, final int position) {
        circleImageView = viewHolder.itemView.findViewById(R.id.circleImageViewForLatestMessage);
        userMessage = viewHolder.itemView.findViewById(R.id.textView2);
        userName = viewHolder.itemView.findViewById(R.id.textView41);
        userName.setText(name);
        userMessage.setText(messages);
    

    You can also mix and match BindableItem and other Items in the adapter, so you can leave legacy viewholders as they are by making an Item.

    For more information visit Groupie Library