0

I'm doing a simple quotes app that contains quotes in cardView. I've stored the quotes in values>arrays.There are two fragments in my app one which contains the quotes in cardview and the next one is favourites. I've created a like button in cardView such that when the like button is pressed I want that cardview to appear in favourites fragment. Can I do this using sharedpreferences ? If yes, how?

The recylerAdapter for my cardView is given below:

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();
        }
    }

And this is my fragment that contains quotes:

public class Category1 extends Fragment {

    private List<CardItemModel> cardItems = new ArrayList<>(20);



    private MainActivity mainActivity;
    private Toolbar toolbar;
    private RecyclerView recyclerView;
    private RecyclerAdapter recyclerAdapter;

    public static ArrayList<CardItemModel> data;


    public Category1() {

    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mainActivity = (MainActivity)activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.cat1_fragment, container, false);

        toolbar = (Toolbar)view.findViewById(R.id.fab_toolbar);

        setupToolbar();



        recyclerView = (RecyclerView)view.findViewById(R.id.fab_recycler_view);

        setupRecyclerView();

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {





        super.onActivityCreated(savedInstanceState);
        mainActivity.setupNavigationDrawer(toolbar);
    }

    private void setupToolbar(){
        toolbar.setTitle(getString(R.string.cat1_fragment_title));
        mainActivity.setSupportActionBar(toolbar);
    }

    private void setupRecyclerView(){
        recyclerView.setLayoutManager(new LinearLayoutManager(mainActivity));
        recyclerView.setHasFixedSize(true);
        initializeCardItemList();
        recyclerAdapter = new RecyclerAdapter(cardItems);
        recyclerView.setAdapter(recyclerAdapter);
    }

    private void initializeCardItemList(){
        CardItemModel cardItemModel;
        String[] cardTitles = getResources().getStringArray(R.array.cat1_cards);
        String[] cardContents = getResources().getStringArray(R.array.cat1_cards_content);
        final int length = cardTitles.length;
        for(int i=0;i<length;i++){
            cardItemModel = new CardItemModel(cardTitles[i],cardContents[i]);
            cardItems.add(cardItemModel);
        }
    }



}

How do I set up my Favourite Fragment so that I can get the favourited items in it. And this is my model class.

public class CardItemModel {

    public String title;
    public String content;

    public CardItemModel(String title, String content) {
        this.title = title;
        this.content = content;
    }
}

1 Answers1

1
  1. Do not use static viewHolder.
  2. Do not use onClickListener in BindViewHolder. Use it in ViewHolder. Apparently, it uses the instance's from the viewHolder.
  3. Do not use two separate Arrays for quotes(String) and favorites (boolean). Use model class for operations such like that.

If you have any query, DM to me. All the best.

Krunal Kapadiya
  • 2,853
  • 3
  • 16
  • 37