Display List Map in RecyclerView

  This post describes a sample project which uses RecyclerView to display a List Map containing keys 'text' and 'image_url'.

1. In main.xml add a Linear linear2 for displaying the RecyclerView.


2. Create a CustomView custom_item.xml. In this add a Linear linear1, an ImageView c_imageview1, and a TextView c_textview1.

3. Switch On AppCompat and Design.

4. Create a More block extra. In this block put codes to declare a RecyclerView recyclerView.

}

androidx.recyclerview.widget.RecyclerView recyclerView;

{

5. In onCreate use following codes.

// Define recyclerView.

recyclerView = new androidx.recyclerview.widget.RecyclerView(this);

// Add RecyclerView to linear2.

linear2.addView(recyclerView);

6. Suppose you have a List Map maplist containing key 'text' for some text and key 'image_url' for url of image.

7. Create another More block adapter, and put following codes in it.

// This code defines a class CategorylistAdapter which can be used as an adapter for RecyclerView.

}

public class CategorylistAdapter extends androidx.recyclerview.widget.RecyclerView.Adapter<CategorylistAdapter.MyViewHolder>{

Context context;

ArrayList<HashMap<String, Object>> maplist;

// Constructor for the adapter.

public CategorylistAdapter(ArrayList<HashMap<String, Object>> list, Context context){

this.maplist = list;

this.context = context;

}

// onCreateViewHolder method. Uses LayoutInflater to create View from the CustomView. The View will act as a ViewHolder.

@androidx.annotation.NonNull

@Override

public CategorylistAdapter.MyViewHolder onCreateViewHolder(@androidx.annotation.NonNull ViewGroup viewGroup, int i) {

View view= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_item, viewGroup, false);

return new CategorylistAdapter.MyViewHolder(view);

}

// onBindViewHolder method. Here we set the text of TextView text and image of ImageView img, by getting items at respective positions of the list. Here we can also set OnClickListeners for the Views.

@Override

public void onBindViewHolder(@androidx.annotation.NonNull final CategorylistAdapter.MyViewHolder myViewHolder, final int i) {

((LinearLayout)myViewHolder.linear1.getParent()).setLayoutParams(myViewHolder.linear1.getLayoutParams());

if (maplist.get(i).containsKey("text")) {

myViewHolder.text.setText(maplist.get(i).get("text").toString ());

}else{

myViewHolder.text.setText("No text");

}

if (maplist.get(i).containsKey("image_url")) {

Glide.with(getApplicationContext()).load(Uri.parse(maplist.get(i).get("image_url").toString())).into(myViewHolder.img);

}else{

Glide.with(getApplicationContext()).load(Uri.parse("https://img.icons8.com/color/48/000000/test-account.png")).into(myViewHolder.img);

}

myViewHolder.linear1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v){

showMessage(maplist.get(i).get("text").toString());

}

});

}

// getItemCount() method. It gets the number of items in the list.

@Override

public int getItemCount() {

return maplist.size();

}

// Define MyViewHolder class. Here we define the Linear linear1, ImageView img and TextView text using the ids in Custom view.

class MyViewHolder extends androidx.recyclerview.widget.RecyclerView.ViewHolder{

public LinearLayout linear1;

public TextView text;

public ImageView img;

public MyViewHolder(@androidx.annotation.NonNull View itemView) {

super(itemView);

linear1= itemView.findViewById(R.id.linear1);

text= itemView.findViewById(R.id.c_textview1);

img= itemView.findViewById(R.id.c_imageview1);

}

}

}

{

8. In the event where you want to set the RecyclerView to display data from maplist (for example onChildAdded event in Firebase project), use following codes to set the adapter for RecyclerView.

// Create a CategorylistAdapter adapter1 for displaying mylist.

CategorylistAdapter adapter1=new CategorylistAdapter(maplist, getApplicationContext());

// Set LayoutManager for RecyclerView. It can be set to display the RecyclerView as HORIZONTAL or VERTICAL.

recyclerView.setLayoutManager(new androidx.recyclerview.widget.LinearLayoutManager(getApplicationContext(), androidx.recyclerview.widget.LinearLayoutManager.HORIZONTAL, false));

// Set adapter1 as adapter of RecyclerView.

recyclerView.setAdapter(adapter1);


Comments

  1. Log in is not working... It always says An error has occurred. SSL handshake time out

    ReplyDelete
    Replies
    1. It's not working for me also. I think there is some problem with their server.

      Delete
    2. Okay. But is there any way I can contact them and notify them please?

      Delete

Post a Comment

Popular posts from this blog

Create Music Player app in Sketchware

A Flash Light App in Sketchware

Admob rewarded video Ads in Sketchware

Firebase auth in Sketchware for Login Activity

Simple car racing android game in Sketchware