简体   繁体   中英

BaseAdapter listview postion while scrolling

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM