滚动时我的列表视图项目消失了

My listview items are disappearing while scrolling

我在片段中使用列表视图。在一行 listview 中,我使用同一行中的按钮更改了 textview 的值,当我向下滚动并返回到该值时,该值已更改并用较旧的 value.When 重置,一行正在消失屏幕的那么只有这个问题发生了。

我已经尝试过使用 ViewHolder,但没有用。

public class VegeAdapter extends ArrayAdapter<VegFru> {
    Context context;
    int resource;
    ArrayList<VegFru> food=new ArrayList<>();
    TextView tvWeight,tvprice,textView;

    public VegeAdapter(@NonNull Context context, int resource, @NonNull ArrayList<VegFru> food) {
        super(context, resource,food);
        this.context=context;
        this.resource=resource;
        this.food=food;
    }
    private static DecimalFormat df2 = new DecimalFormat(".##");

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull final ViewGroup parent) {
        View Listview=convertView;
        final ViewHolder holder;

        if(Listview==null){
            Listview= LayoutInflater.from(getContext()).inflate(R.layout.vegge_row,parent,false);
            holder = new ViewHolder();
            holder.tvfname=(TextView) Listview.findViewById(R.id.foodName);
            holder.tvhfname=(HindiTextView) Listview.findViewById(R.id.foodHinName);
            holder.tvWeight=(TextView) Listview.findViewById(R.id.fdweight);
            holder.tvfprice=(TextView)Listview.findViewById(R.id.foodPrice);
            holder.tvprice=(TextView) Listview.findViewById(R.id.fdPrice);
            holder.textView=(TextView) Listview.findViewById(R.id.atcQtyy);
            Listview.setTag(holder);
        }
        else {
            holder=(ViewHolder) Listview.getTag();
        }

        VegFru currentFood=getItem(position);
        holder.tvfname.setText(currentFood.getName());
        holder.tvhfname.setText("("+currentFood.getHinName()+")");
        holder.tvWeight.setText(currentFood.getUnitWeight()+" gm");
        holder.tvfprice.setText(""+currentFood.getPrice()+"₹ per kg");

        int pr=currentFood.getPrice();
        int realpri=pr/2;
        holder.tvprice.setText("0 ₹");

        holder.textView.setText((currentFood.quantity)+"");
        ImageView ivFoodImg=(ImageView) Listview.findViewById(R.id.foodImg);
        Picasso.get().load(currentFood.getImage()).into(ivFoodImg);
        holder.addBtn=(Button) Listview.findViewById(R.id.atcaddBtn);
        holder.subbtn=(Button) Listview.findViewById(R.id.atcsubBtn);
        holder.btnadd=(Button) Listview.findViewById(R.id.addBtn);
        holder.linearLayout=(LinearLayout) 
        Listview.findViewById(R.id.linearL);
        holder.addBtn.setTag(position);
        holder.linearLayout.setVisibility(View.INVISIBLE);
        holder.btnadd.setVisibility(View.VISIBLE);

        holder.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ListView) parent).performItemClick(view, position, 0);
                updateQuantity(position, holder.tvWeight, holder.tvprice, 
                holder.textView,1, holder.linearLayout, holder.btnadd);
            }
        });

        holder.subbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((ListView) parent).performItemClick(view, position, 1);
                updateQuantity(position, holder.tvWeight, holder.tvprice, holder.textView,-1, holder.linearLayout, holder.btnadd);
            }
        });

/*    subbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((GridView) parent).performItemClick(view, position, 2);

        }
    });*/
    //   linearLayout.setVisibility(View.GONE);
    holder.btnadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((ListView) parent).performItemClick(view, position, 22);
            holder.btnadd.setVisibility(View.INVISIBLE);
            holder.linearLayout.setVisibility(View.VISIBLE);
            updateQuantity(position, holder.tvWeight, holder.tvprice, 
            holder.textView,1, holder.linearLayout, holder.btnadd);
        }
    });
    return Listview;
}
private void updateQuantity(int position,TextView unitWeight, TextView prive,TextView edTextQuantity, int value, LinearLayout ll, Button addB) {
    int p=0;
    int total=0;
    double tot=0;
    VegFru products = getItem(position);
    if(value > 0)
    {
        products.quantity = products.quantity + 1;
        int tu=products.getPrice();
        int proice=tu/4;
        p=proice*products.quantity;
        int weight=products.getUnitWeight();
        total=weight*products.quantity;
        if(total>=1000){
            Double d= new Double(total);
            tot=d/1000;

        }else{
            tot=total;
        }
    }
    else
    {
        if(products.quantity > 0)
        {
            products.quantity = products.quantity - 1;
            int tu=products.getPrice();
            int proice=tu/4;
            p=proice*products.quantity;
            int weight=products.getUnitWeight();
            total=weight*products.quantity;
            if(total>=1000){
                Double d= new Double(total);
                tot=d/1000;

            }else{
                tot=total;
            }
            if(products.quantity==0){
                ll.setVisibility(View.INVISIBLE);
                addB.setVisibility(View.VISIBLE);
                p=0;
            }
        }
    }
    edTextQuantity.setText(products.quantity+"");
    prive.setText(p+" ₹");
    if(total>=1000){
        unitWeight.setText(tot+" kg");
    }else{
        unitWeight.setText(total+" gm");
    }
}
static  class ViewHolder{
    TextView tvfname;
    HindiTextView tvhfname;
    TextView tvWeight=null;
    TextView tvfprice;
    TextView tvprice=null;
    TextView textView=null;
    Button addBtn;
    Button subbtn;
    Button btnadd=null;
    LinearLayout linearLayout=null;
}

}

因为您更新的是 TextView 而不是 VegFru 对象的文本。如果您想永久更改文本,您应该更改 ListView 中列出的对象的值。在列表视图适配器的父 class 中执行此操作,并在更新数量调用 adapter.notifyDataSetChanged 方法后执行此操作。

更新: holder.linearLayout.setVisibility(View.INVISIBLE); 行使您的布局不可见。当你滚动时 getView 被触发。而且您不更新列表中的对象。改变你的逻辑并进入 getView.