简体   繁体   中英

How to retrieve item count from RecyclerView adapter into another activity(not parent activity)?

i've been trying to display the number of items from the recyclerview adapter(TaskAdapter) into an activity(StatisticsActivity). I am a beginner so I do apologize if i display a low level of understanding.

StatisticsActivity:

 public class StatisticActivity extends AppCompatActivity implements TaskAdapter{ TextView itemcount; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_statistic); itemcount = findViewById(R.id.itemcount); itemcount.setText(myTask.size()); } }

TaskAdapter:

 public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder>{ Context context; ArrayList<MyTask> myTask; public TaskAdapter(Context c, ArrayList<MyTask> p) { context = c; myTask = p; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_task,viewGroup, false)); } @Override public int getItemCount() { return myTask.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView titletask, desctask, datetask, keytask; public MyViewHolder(@NonNull View itemView) { super(itemView); titletask = (TextView) itemView.findViewById(R.id.titletask); desctask = (TextView) itemView.findViewById(R.id.desctask); datetask = (TextView) itemView.findViewById(R.id.datetask); } } }

Here is the thing. If you have an adapter somewhere in your project and you are in an activity where you want the size of the list that adapter is holding.... then it might not be possible.

But if you have a recyclerview displaying list of items, and if you want to go to StatisticActivity when you click on one of the item in the recyclerview then it is possible to pass the number of items in the list to StatisticActivity .

For that you need to use and interface in Adapter and implement that interface in the StatisticActivity .

In adapter create an interface:

public interface OnItemClick {
    void onClick (int size);
}

In StatisticActivity , implement the interface from adapter.

public class StatisticActivity extends AppCompatActivity implements OnItemClick {
      private int listSize;
      ......
      ......

      @Override
      void onClick (int size){
           listSize = size; // this is your list size from adapter.
      }
}

In adapter

public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.MyViewHolder>{

    Context context;
    OnItemClick mCallback;

    ArrayList<MyTask> myTask;

    public TaskAdapter(Context c, ArrayList<MyTask> p, OnItemClick listener) {
        context = c;
        myTask = p;
        this.mCallback = listener;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_task,viewGroup, false));
    }


    @Override
    public int getItemCount() {
        mCallback.onClick(myTask.size());
        return myTask.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView titletask, desctask, datetask, keytask;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            titletask = (TextView) itemView.findViewById(R.id.titletask);
            desctask = (TextView) itemView.findViewById(R.id.desctask);
            datetask = (TextView) itemView.findViewById(R.id.datetask);
        }
    }

    public interface OnItemClick {
         void onClick (int size);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM