user5241618
user5241618

Reputation:

Using Multi-select recyclerview to pass selected items to another activity

What I'm trying to do is getting the selected items and then pass it on using the toolbar to intent values to the next activity. How should i make my toolbar menu know what data it is trying to get. Please help

Adapter onBindViewHolder()

@Override
    public void onBindViewHolder(final MyAdapter.ViewHolder viewHolder, int position) {

        viewHolder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Item item = items.get(viewHolder.position);
                if(item.isSelect()){
                    item.setSelect(false);
                } else {
                    item.setSelect(true);
                }
                items.set(viewHolder.position, item);
                if(changeStatusListener != null){
                    changeStatusListener.onItemChangeListener(viewHolder.position, item);

                }
                notifyItemChanged(viewHolder.position);
            }
        });


        try {
            Item item= items.get(position);
            if(item!= null){
                viewHolder.name.setText(category.getItem());
                viewHolder.imageView.setImageResource(category.getImage());
                viewHolder.position = position;

                if(item.isSelect()) {
                    viewHolder.view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
                }
                else viewHolder.view.setBackgroundResource(R.drawable.item_selector);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }

My toolbar

toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem arg0) {
                switch (arg0.getItemId()) {
                    case R.id.save:
                        Intent intent = new Intent(getApplicationContext(), NextActivity.class);
                        startActivity(intent);
                    default:
                        return false;
                }
            }
});

Upvotes: 2

Views: 1921

Answers (2)

Alok Omkar
Alok Omkar

Reputation: 628

  1. Create a method in your adapter :

    public ArrayList<Item> getSelectedItems() {
        ArrayList<Item> selectedItems = new ArrayList<>();
        for( Item item : items ) {
           if( item.isSelect() )
              selectedItems.add(item);
           }
           return selectedItems;
        }
    
  2. Make sure your model class Item implements serializable or parcelable

  3. In your toolbar menu click listener code :

>      toolbar.setOnMenuItemClickListener(new 
>         
>         Toolbar.OnMenuItemClickListener() {
>             @Override
>             public boolean onMenuItemClick(MenuItem arg0) {
>             switch (arg0.getItemId()) {
>                case R.id.save:
>            
>     
>     Intent intent = new Intent(getApplicationContext(), NextActivity.class);
>            intent.putSerializable("selectedItems", adapter.getSelectedItems()); // In case Item class is implementing
> serializable
> intent.putParcelableArrayListExtra("selectedItems",adapter.getSelectedItems());  // In
> case you are using parcelable in your Item class
>            startActivity(intent);
>     
>                default:
>                return false;
>                }
>               }
>             });

Upvotes: 0

Jayesh Elamgodil
Jayesh Elamgodil

Reputation: 1477

You can maintain a data structure in your adapter class to keep adding the selected items.

I am guessing your toolbar is in your activity or fragment which hopefully has a reference of the adapter which can access this data structure and then pass it in the intent.

I'd use a SparseArray. So in your adapter, do something like the follow

SparseArray itemsArray = new SparseArray<>();

in your onBindViewHolder method

if(item.isSelect()) {
   viewHolder.view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
   if (itemsArray.get(position) != item) {
      itemsArray.put(position, item);
   }} else {
viewHolder.view.setBackgroundResource(R.drawable.item_selector);
if (itemsArray.get(position) == item) {
    itemsArray.put(position, item);
}}

Now, from your activity or fragment use the adapter reference to access the itemsArray variable. Run a for loop on it and add the items into an arrayList.

The arrayList can be set as extra in the intent and can be retrieved in your target activity from the getSerializableExtra() method.

On an another note, you can use position instead of viewHolder.position

Upvotes: 1

Related Questions