-1

I've a list of cardViews which contains two textViews each (title and content) and a toggle button (which is a favourite button). I want to add specific cardviews in which togglebutton is checked to another fragment "favourites" so that user can view favourite cards. How can I do ths? This is my recycler adapter:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
        private ClipboardManager myClipboard;
        private ClipData myClip;
        private Context context;





        public List<CardItemModel> cardItems;

        public RecyclerAdapter(List<CardItemModel> cardItems){
            this.cardItems = cardItems;
        }

        public static class ViewHolder extends RecyclerView.ViewHolder{
            ImageView copyButton;
            ImageView shareButton;
            ToggleButton favButton;



            TextView title;
            TextView content;
            public ViewHolder(View itemView) {
                super(itemView);
                this.title = (TextView)itemView.findViewById(R.id.card_title);
                this.content = (TextView)itemView.findViewById(R.id.card_content);
                this.copyButton= (ImageView) itemView.findViewById(R.id.copyButton);
                this.shareButton=(ImageView) itemView.findViewById(R.id.shareButton);

                this.favButton=(ToggleButton) itemView.findViewById(R.id.favButton);

                favButton.setChecked(false);
                favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_launcher));


            }
        }



        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item,parent,false);
            ViewHolder viewHolder = new ViewHolder(view);

            return viewHolder;
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, int position) {
            holder.title.setText(cardItems.get(position).title);
            holder.content.setText(cardItems.get(position).content);
            holder.copyButton.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){


                    myClipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);


                    myClip = ClipData.newPlainText("label", holder.content.getText().toString());
                    myClipboard.setPrimaryClip(myClip);
                    Toast.makeText(v.getContext(), "Copied to clipboard" , Toast.LENGTH_SHORT ).show();

                }
            });
            holder.shareButton.setOnClickListener(new View.OnClickListener(){
                public void onClick(View v){
                    Intent share = new Intent(Intent.ACTION_SEND);
                    share.setType("text/plain");
                    share.putExtra(Intent.EXTRA_TEXT, holder.content.getText().toString());
                    v.getContext().startActivity(Intent.createChooser(share, "Share Text"));
                }
            });

            holder.favButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
                @Override
                public void onCheckedChanged(CompoundButton favButton, boolean isChecked){
                    if (isChecked)
                        favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(),R.mipmap.ic_launcher));

                    else
                        favButton.setBackgroundDrawable(ContextCompat.getDrawable(favButton.getContext(), R.mipmap.ic_cart));
                }
            });
        }

        @Override
        public int getItemCount() {
            return cardItems.size();
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Sopnil Shah
  • 51
  • 2
  • 7

2 Answers2

0

You can save your list in the database. Add a boolean column(favorite) in the table and give default value as false to it. When a user clicks on toggle button make the value true in the favorite column. Now when you want to show favorite fragment then retrieve all the values from the database in which favorite column has true values with below query

select * from table_name where favorite=true

And then pass the list to your adapter.

Rohan Sharma
  • 374
  • 4
  • 11
0

So, what I get straight away from the question, is that you have a list of cardviews having a star button. When the user clicks on the star option, the cardview is maybe added to another tab called "Favorites" so that when the user switches to that tab, he sees all the cards he had marked favorites. I have certain scenarios in mind as you haven't provided any more info.

Scenario 1: You store the data in a local table, and have another column as starred which tells whether the record is starred or not.

Now, you might be fetching the data from an online API, in this case you can create a local replica with one extra column.

Or you might be fetching the list from an already present local db. In that case, just add another column.

Now, in your favorites tab, do a fetch from the database for all favorite records.

Scenario 2: You don't want to store the data locally, you want to put the cardview to the favorites tab on the fly. For this, consider the following structure.

MainActivity (tabLayout with pageViewer, implements StarListener)
- ContentFragment (recyclerView)
- FavoritesFragment (recyclerView)

interface StarListener
- void star(Item item);

We implement StarListener in MainActivity, and keep instances in ContentFragment and the adapter of contentFragment's recycler view. On Click of the star button in the cardview, we call the star method of this instance in the adapter.

In the main activity, the method will do the following. Get the favorites fragment, and add the Item in the FavoritesFragment's Recycler View.

I haven't put the entire code because I believe that given the way to do it, you would know enough to implement it.

In any case, I have the working code for it. Feel free to ping me for it.

Important links to help out with the answer

halfer
  • 19,824
  • 17
  • 99
  • 186
Jeevan MB
  • 148
  • 1
  • 11