我正在片段中使用listview。在列表视图的一行中,我使用同一行中的按钮来更改textview的值,并且当我向下滚动并返回到该值时,该值已更改并重置为较旧的值。屏幕,那么只会发生此问题。
我已经尝试使用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;
}
}
答案 0 :(得分:0)
因为您更新TextView
对象而不是VegFru
对象的文本。如果要永久更改文本,则应更改ListView
中列出的对象的值。在列表视图适配器的父类中,并在更新数量后调用adapter.notifyDataSetChanged
方法。
更新:
holder.linearLayout.setVisibility(View.INVISIBLE);
行使您的布局不可见。滚动时getView
被触发。并且您不更新列表中的对象。更改逻辑并移至getView
。