0

I have a ListView with many items. Each item has a button and when I click on it, it becomes invisible and another button becomes visible.

The issue is: when I scroll the ListView, many other items buttons are invisible. How can I fix it?

    public class homebuyer_fruits_adapter extends BaseAdapter {
    private ArrayList<Itemssetget> listData;
    private LayoutInflater layoutInflater;
    public  static   int cout=0;

    List  position_item=new ArrayList();

    Context context;

    String check;

    int lastpostition=-1;
    public static List<cartitemslist> cartlist=new ArrayList<>();
    public homebuyer_fruits_adapter(Context aContext, ArrayList<Itemssetget> listData,String number) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(aContext);
        context=aContext;
        check=number;
        this.notifyDataSetChanged();

    }
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View v, ViewGroup parent) {
        final ViewHolder holder;
        final cartitemslist homeitemslist = new cartitemslist();

        if (v == null) {
            v = layoutInflater.inflate(R.layout.food_vegitablews_view, null);
            holder = new ViewHolder();

                holder.name = (TextView) v.findViewById(R.id.name_item);
                holder.price = (TextView) v.findViewById(R.id.price_item);
                holder.add_q = (ImageView) v.findViewById(R.id.add_quantity);
                holder.delete_q = (ImageView) v.findViewById(R.id.delete_quantity);
                holder.quantity = (TextView) v.findViewById(R.id.add_quantity_items);

                //holder.description = (TextView) v.findViewById(R.id.description_item);
                //holder.id = (TextView) v.findViewById(R.id.);
                holder.imageView = (ImageView) v.findViewById(R.id.image_items);
                holder.discount = (TextView) v.findViewById(R.id.discout_price);
                holder.add = (Button) v.findViewById(R.id.addto_cart);
                holder.units=(TextView)v.findViewById(R.id.item_units);
                holder.percentage=(TextView)v.findViewById(R.id.discount_percentage);
                holder.linearLayou=(LinearLayout)v.findViewById(R.id.Quantity_control_linnear_view) ;
                holder.phone=check;

                v.setTag(holder);

        } else {
            holder = (ViewHolder) v.getTag();
            this.notifyDataSetChanged();
        }

        int i=Integer.parseInt(listData.get(position).getPrice())-Integer.parseInt(listData.get(position).getDiscountprice());
        int d=i*100;
        if((d/Integer.parseInt(listData.get(position).getPrice()))==0)
        {
            holder.percentage.setVisibility(View.GONE);
        }
        holder.percentage.setText(""+d/Integer.parseInt(listData.get(position).getPrice())+"% OFF");
           holder.name.setText(listData.get(position).getName());
           holder.price.setText("Rs " + listData.get(position).getPrice());
           if(listData.get(position).getPrice().equals(listData.get(position).getDiscountprice()))
           {
               holder.price.setVisibility(View.GONE);
           }
        holder.price.setPaintFlags(holder.price.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
           holder.discount.setText("Rs " + listData.get(position).getDiscountprice());
           Picasso.with(context).load(listData.get(position).getImageurl())
                   .fit().centerCrop().into(holder.imageView);
           holder.units.setText(listData.get(position).getUnits());

            holder.add_q.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Toast.makeText(context,listData.get(position).getName(),Toast.LENGTH_LONG).show();

                    if(H.containsKey(position))
                    {
                        holder.quantity.setText(""+(1+Integer.parseInt(holder.quantity.getText().toString())));
                        cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());

                    }

                }
            });
            holder.delete_q.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(H.containsKey(position)) {

                   if(holder.quantity.getText().toString().equals("1"))
                   {
                       holder.add.setVisibility(View.VISIBLE);
                       holder.linearLayou.setVisibility(View.GONE);

                       int i=H.get(position);
                       cartlist.remove(i);
                       cout--;
                       Fruits.numberofitems.setText(cout + "");
                   }
                   else {

                           holder.quantity.setText("" + (Integer.parseInt(holder.quantity.getText().toString()) - 1));
                           cartlist.get(H.get(position)).setQuantity(holder.quantity.getText().toString());

                   }
                   }

                }
            });

            holder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //Toast.makeText(context,"Add to cart"+holder.name.getText(),Toast.LENGTH_SHORT).show();

                        holder.add.setVisibility(View.GONE);
                        holder.linearLayou.setVisibility(View.VISIBLE);

                        // check=listData.get(position).getPhonenumber_seller();

                        if(cartlist.isEmpty()) {
                            cout++;
                            homeitemslist.setName(holder.name.getText().toString());
                            homeitemslist.setPrice(holder.price.getText().toString());
                            homeitemslist.setDiscountp(holder.discount.getText().toString());
                            homeitemslist.setQuantity(holder.quantity.getText().toString());
                            homeitemslist.setPhone_id(holder.phone);
                            homeitemslist.setImage(listData.get(position).getImageurl());
                            if(Fruits.numberofitems!=null)
                            {
                                Fruits.numberofitems.setText(cout + "");
                            }

                           shop_items.numberofitems.setText(cout + "");
                           buyer_home.numberofitems.setText(cout + "");
                            cartlist.add(homeitemslist);
                            H.put(position,cartlist.indexOf(homeitemslist));
                        }
                        else {
                            if(check.equals(cartlist.get(0).getPhone_id()))
                            {
                                cout++;
                                cartitemslist homeitemslist = new cartitemslist();
                                homeitemslist.setName(holder.name.getText().toString());
                                homeitemslist.setPrice(holder.price.getText().toString());
                                homeitemslist.setDiscountp(holder.discount.getText().toString());
                                homeitemslist.setQuantity(holder.quantity.getText().toString());
                                homeitemslist.setPhone_id(holder.phone);
                                homeitemslist.setImage(listData.get(position).getImageurl());
                                if(Fruits.numberofitems!=null)
                                {
                                    Fruits.numberofitems.setText(cout + "");
                                }

                                shop_items.numberofitems.setText(cout + "");
                               buyer_home.numberofitems.setText(cout + "");
                                cartlist.add(homeitemslist);
                               H.put(position,cartlist.indexOf(homeitemslist));


                            }
                            else
                            {
                                Toast.makeText(context,"Clear the Previous cart First than you can buy from this shop",Toast.LENGTH_LONG).show();
                            }

                    }


                }


            });


            // holder.description.setText(listData.get(position).getDiscription());

           // holder.id.setText(listData.get(position).getId());
            //holder.imageView.setImageAlpha(R.drawable.banana);
        setanimation(v,position);

        return v;
    }
    static class ViewHolder {
        LinearLayout linearLayou;
        TextView name;
        TextView price;
        TextView discount;
        ImageView add_q;
        ImageView delete_q;
        TextView quantity;
        ImageView imageView;
        TextView description;
        TextView units;
        TextView percentage;
       String phone;
        Button add;
    }

    public  void setanimation(View viewanim,int position)
    {
        if(position>lastpostition)
        {
            ScaleAnimation animation=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f,
            Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
            animation.setDuration(1500);
            viewanim.setAnimation(animation);
            lastpostition=position;
        }

    }


}

Here is my adapter code. I tried many ways but still stuck on this issue.

Bruno
  • 3,872
  • 4
  • 20
  • 37
  • You can't directly use holder.view inside onClick, use setTag() for tagging position ot other information. Check my answer https://stackoverflow.com/questions/31169131/android-adjacent-buttons-in-list-view-automatically-clicked/31578669#31578669 – Jaiprakash Soni Jul 09 '20 at 06:27
  • then what i do ? guide me ViewHolder h=listData.get(position)view.getTag(); error – Basit Abbasi Jul 09 '20 at 06:36

1 Answers1

0

You set the visibility only in the OnClickListener, but you need to set it in getView() too, because your views will get recycled (re-used for a different item) when you scroll.

So in getView() you have to restore the complete state of the view, including button visibility. Store all state information in your list items. Inside your getView() do something like this:

@Override
public View getView(final int position, View v, ViewGroup parent) {

    ...

    // the view (v) could be recycled and showing a state from another item
    // e.g. the add button is visible

    final Itemssetget listItem = listData.get(position);

    // so we have to restore the state of the view for listItem above
    // e.g. update the button visibility
    holder.add.setVisibility(listItem.isAddButtonVisible() ? View.VISIBLE : View.GONE); 
    holder.add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ...
            holder.add.setVisibility(View.GONE);
            // save the state of the button, so it can be restored later
            listItem.setAddButtonVisible(true); 
            ...
        }
    }
    ...
}

Add the necessary member variables and getter/setter functions in Itemssetget:

public class Itemssetget {
    ...
    private boolean isAddButtonVisible = false;
    
    public boolean isAddButtonVisible() {
        return isAddButtonVisible;
    }
    
    public void setAddButtonVisible(boolean isVisible) {
        this.isAddButtonVisible = isVisible;
    }
    ...
}

PS: Your code looks terrible. Have a look at naming conventions.

Bram Stoker
  • 1,202
  • 11
  • 14
  • I did not get it explain it more if you have any link then share me – Basit Abbasi Jul 09 '20 at 09:36
  • This answer provides a basic explanation of how ListView works: https://stackoverflow.com/a/14108676/4706541 – Bram Stoker Jul 09 '20 at 10:06
  • I updated the code example to better explain the solution – Bram Stoker Jul 09 '20 at 10:24
  • showing error in this listItem.isAddButtonVisible() – Basit Abbasi Jul 09 '20 at 10:40
  • You have to create the getter and setter functions in `Itemssetget`, they don't exist yet. You'll also need a member variable `isAddButtonVisible` there to save the state. I didn't include that code as it should be self-explanatory – Bram Stoker Jul 09 '20 at 10:52
  • please add that code I add it to my getter setter class – Basit Abbasi Jul 09 '20 at 10:55
  • I'll include it. This is a good source to get started with common OOP concepts: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html – Bram Stoker Jul 09 '20 at 11:00
  • this is not working when I add these all buttons are invisible I want only that button invisible which click by me second when I scroll same issue with this – Basit Abbasi Jul 10 '20 at 04:42
  • Set the default add button visibility to 'true' in your 'Itemssetget' model. I initialized it to 'false', so all add buttons start invisible. Do note I picked the add button as an example. If you have more 'view state' to save, you have to add it to your model too, just like I did with the add button – Bram Stoker Jul 10 '20 at 06:33