Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of listview.getCount in case of Recyclerview

I have used listview.getCount() how can I get count in case of Recyclerview

id = receiver + "-" + splashList.getCount();

Customized method in adapter:

public void setImage(String my_image, String r_image) {
    byte[] decodedrimage = Base64.decode(r_image, Base64.DEFAULT);
    Bitmap decodedrByte = BitmapFactory.decodeByteArray(decodedrimage, 0,
            decodedrimage.length);
    if (my_image.equals("empty")) {
        imgflag = 1;
    } else {
        byte[] decodedmyimage = Base64.decode(my_image, Base64.DEFAULT);
        Bitmap decodedmyByte = BitmapFactory.decodeByteArray(
                decodedmyimage, 0, decodedmyimage.length);
        rimage = createCirclebitmap(decodedrByte);
        myimage = createCirclebitmap(decodedmyByte);
    }

}

and calling of the method in another class:

CustomListAdapter1 mAdapter ;
mAdapter = new CustomListAdapter1(NewUI.this, dummy,
                imgpath, bitmap, id, dateList);
mAdapter.setImage(myimage, rimage);
like image 790
user9213 Avatar asked Jul 04 '15 07:07

user9213


People also ask

Is ListView the same as RecyclerView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.

How do I display a list in RecyclerView?

In this step, we will create a new layout file for the single list item view. Go to app > res > layout > right-click > New > Layout Resource File and name it as list_item. list_item. xml contains an ImageView and a TextView which is used for populating the RecyclerView.

What is ViewHolder in RecyclerView?

A ViewHolder describes an item view and metadata about its place within the RecyclerView. RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View. findViewById(int) results.

What is setHasFixedSize in RecyclerView?

setHasFixedSize(true) means the RecyclerView has children (items) that has fixed width and height. This allows the RecyclerView to optimize better by figuring out the exact height and width of the entire list based on the your adapter.


2 Answers

there is no equivalent. The RecyclerView has no direct knowledge of the underlying dataset. The closest thing is

int count = 0;
if (recyclerViewInstance.getAdapter() != null) {
     count = recyclerViewInstance.getAdapter().getItemCount();
}

or if you have a reference to your adapter, simply call

  int count = 0;
  if (mAdapter != null) {
     count = mAdapter.getItemCount();
  }
like image 168
Blackbelt Avatar answered Sep 17 '22 20:09

Blackbelt


//use this method to return recycler adapter item count..
private int recyclerCount(){
 int Count = 0;
  if (mAdapter != null) {
     count = mAdapter.getItemCount();
  }
return count;
}
like image 45
Daniel Alome Avatar answered Sep 17 '22 20:09

Daniel Alome