0

I am working on a commercial app as an internship. In one of its activity, I have tab view with two fragments. In each fragment, I'm using the card view to hold the views. The card view has one image view, two text views, and a button and in the bottom of the activity below the tab view, there is a button which has its visibility mode as "GONE".

Now What I want is, Whenever I click on the button in the card view, the button at the bottom of the activity should hide/show for respective clicks.

Cardcaptionadapter.java

   public CaptionedImagesAdapterMenu.ViewHolder onCreateViewHolder(
            ViewGroup parent, int viewType){
        CardView cv = (CardView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.card_captioned_image_menu, parent, false);
        return new ViewHolder(cv);
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position){
        CardView cardView = holder.cardView;
        ImageView imageView = (ImageView)cardView.findViewById(R.id.info_image);
        Drawable drawable = ContextCompat.getDrawable(cardView.getContext(), imageIds[position]);
        imageView.setImageDrawable(drawable);
        imageView.setContentDescription(captions[position]);
        TextView textView = (TextView)cardView.findViewById(R.id.info_text);
        textView.setText(captions[position]);
        TextView textView1 = cardView.findViewById(R.id.info_menu);
        textView1.setText(desc[position]);
        TextView textView2 = cardView.findViewById(R.id.info_price);
        textView2.setText("₹ " + price[position]);
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onClick(position);
                }
            }
        });
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(holder.button.getText().equals("ADD")){
                    holder.button.setText("ADDED");
                    holder.button.setBackgroundColor(Color.parseColor("#00ff00"));
                    SharedPreferences sharedPref = context.getSharedPreferences("ADD",0);
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putInt("ADDED", 1);
                    editor.apply();
                }
                else {
                    holder.button.setText("ADD");
                    holder.button.setBackgroundColor(Color.parseColor("#ffffff"));
                    SharedPreferences sharedPref = context.getSharedPreferences("ADD",0);
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.clear();
                    editor.apply();
                }
            }
        });
    }

MenuActivity.java

 SharedPreferences preferences = getSharedPreferences("ADD",0);
         int addvalue = preferences.getInt("ADDED",0);

         if(addvalue==1){
             orderbutton.setVisibility(View.VISIBLE);
         }
         else{
             orderbutton.setVisibility(View.GONE);
         }
         orderbutton.setOnClickListener(view -> {
             orderbutton.setVisibility(View.GONE);
             paybutton.setVisibility(View.VISIBLE);
         });
Pradeep
  • 798
  • 1
  • 7
  • 18

1 Answers1

0

you should add MenuActivity.java code inside public void onBindViewHolder(ViewHolder holder, final int position){ } method also.

Mohsin kazi
  • 532
  • 1
  • 8
  • 15