Android 动态列表视图滚动文本视图已自动更改

Android 动态列表视图滚动文本视图已自动更改,android,listview,Android,Listview,当我点击+按钮增加价格,点击-按钮降低价格时,它工作得很好,但当我滚动listview时,tvPrices(TextView)的值发生了变化 我应该怎么做才能获得住宿增值价格 这是我的适配器 public class ListAdapter extends BaseAdapter { public ArrayList<Integer> quantity = new ArrayList<Integer>(); public ArrayList<Integer&

当我点击+按钮增加价格,点击-按钮降低价格时,它工作得很好,但当我滚动listview时,tvPrices(TextView)的值发生了变化

我应该怎么做才能获得住宿增值价格

这是我的适配器

 public class ListAdapter extends BaseAdapter {

 public ArrayList<Integer> quantity = new ArrayList<Integer>();
 public ArrayList<Integer> price = new ArrayList<Integer>();
 private String[] listViewItems, prices, static_price;
 TypedArray images;
 View row = null;

 static String get_price, get_quntity;
 int g_quntity, g_price, g_minus;

 private Context context;
 CustomButtonListener customButtonListener;

 static HashMap<String, String> map = new HashMap<>();


 public ListAdapter(Context context, String[] listViewItems, TypedArray images, String[] prices) {
  this.context = context;
  this.listViewItems = listViewItems;
  this.images = images;
  this.prices = prices;

  for (int i = 0; i < listViewItems.length; i++) {
   quantity.add(0);
   price.add(0);
  }
 }

 public void setCustomButtonListener(CustomButtonListener customButtonListner) {
  this.customButtonListener = customButtonListner;
 }

 @Override
 public int getCount() {
  return listViewItems.length;
 }

 @Override
 public String getItem(int position) {
  return listViewItems[position];
 }


 @Override
 public long getItemId(int position) {
  return 0;
 }

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

  final ListViewHolder listViewHolder;
  if (convertView == null) {
   LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   row = layoutInflater.inflate(R.layout.activity_custom_listview, parent, false);
   listViewHolder = new ListViewHolder();
   listViewHolder.tvProductName = (TextView) row.findViewById(R.id.tvProductName);
   listViewHolder.ivProduct = (ImageView) row.findViewById(R.id.ivproduct);
   listViewHolder.tvPrices = (TextView) row.findViewById(R.id.tvProductPrice);
   listViewHolder.btnPlus = (ImageButton) row.findViewById(R.id.ib_addnew);
   listViewHolder.edTextQuantity = (EditText) row.findViewById(R.id.editTextQuantity);
   listViewHolder.btnMinus = (ImageButton) row.findViewById(R.id.ib_remove);
   static_price = context.getResources().getStringArray(R.array.Price);
   row.setTag(listViewHolder);
  } else {
   row = convertView;
   listViewHolder = (ListViewHolder) convertView.getTag();
  }


  listViewHolder.ivProduct.setImageResource(images.getResourceId(position, -1));


  try {

   listViewHolder.edTextQuantity.setText(quantity.get(position) + "");

  } catch (Exception e) {
   e.printStackTrace();
  }

  listViewHolder.btnPlus.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View v) {
    if (customButtonListener != null) {
     customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, 1);

     quantity.set(position, quantity.get(position) + 1);
     price.set(position, price.get(position) + 1);

     row.getTag(position);

     get_price = listViewHolder.tvPrices.getText().toString();

     g_price = Integer.valueOf(static_price[position]);

     get_quntity = listViewHolder.edTextQuantity.getText().toString();
     g_quntity = Integer.valueOf(get_quntity);

     map.put("" + listViewHolder.tvProductName.getText().toString(), " " + listViewHolder.edTextQuantity.getText().toString());
     listViewHolder.tvPrices.setText("" + g_price * g_quntity);
//                    Log.d("A ", "" + a);
//                    Toast.makeText(context, "A" + a, Toast.LENGTH_LONG).show();
//                    Log.d("Position ", "" + position);
//                    System.out.println(+position + " Values " + map.values());
     ShowHashMapValue();
     listViewHolder.tvPrices.setText("" + g_price * g_quntity);
    }


   }

  });
  listViewHolder.btnMinus.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if (customButtonListener != null) {
     customButtonListener.onButtonClickListener(position, listViewHolder.edTextQuantity, -1);
     if (quantity.get(position) > 0)
      quantity.set(position, quantity.get(position) - 1);

     get_price = listViewHolder.tvPrices.getText().toString();
     g_minus = Integer.valueOf(get_price);
     g_price = Integer.valueOf(static_price[position]);
     int minus = g_minus - g_price;
     if (minus >= g_price) {
      listViewHolder.tvPrices.setText("" + minus);
     }

     map.put("" + listViewHolder.tvProductName.getText().toString(), " " + listViewHolder.edTextQuantity.getText().toString());
     ShowHashMapValue();
    }
   }
  });
  listViewHolder.tvProductName.setText(listViewItems[position]);
  listViewHolder.tvPrices.setText(prices[position]);

  return row;
 }

 private void ShowHashMapValue() {

  /**
   * get the Set Of keys from HashMap
   */
  Set setOfKeys = map.keySet();

/**
 * get the Iterator instance from Set
 */
  Iterator iterator = setOfKeys.iterator();

/**
 * Loop the iterator until we reach the last element of the HashMap
 */
  while (iterator.hasNext()) {
/**
 * next() method returns the next key from Iterator instance.
 * return type of next() method is Object so we need to do DownCasting to String
 */
   String key = (String) iterator.next();

/**
 * once we know the 'key', we can get the value from the HashMap
 * by calling get() method
 */
   String value = map.get(key);

   System.out.println("Key: " + key + ", Value: " + value);
  }
 }
}
公共类ListAdapter扩展了BaseAdapter{
公共ArrayList数量=新建ArrayList();
public ArrayList price=new ArrayList();
私有字符串[]listViewItems、价格、静态价格;
射线图像类型;
视图行=空;
静态字符串获取价格,获取数量;
整数、价格、负数;
私人语境;
CustomButtonListener CustomButtonListener;
静态HashMap map=newhashmap();
公共ListAdapter(上下文上下文,字符串[]listViewItems,类型Darray图像,字符串[]价格){
this.context=上下文;
this.listViewItems=listViewItems;
这个。图像=图像;
价格=价格;
对于(int i=0;i0)
数量.set(位置,数量.get(位置)-1);
get_price=listViewHolder.tvPrices.getText().toString();
g_减=整数.valueOf(获取价格);
g_价格=整数.valueOf(静态_价格[位置]);
int减号=g_减号-g_价格;
如果(减去>=g_价格){
listViewHolder.tvPrices.setText(“+减”);
}
map.put(“+listViewHolder.tvProductName.getText().toString(),”+listViewHolder.edTextQuantity.getText().toString());
ShowHashMapValue();
}
}
});
listViewHolder.tvProductName.setText(listViewItems[position]);
listViewHolder.tvPrices.setText(价格[位置]);
返回行;
}
私有void ShowHashMapValue(){
/**
*从HashMap获取密钥集
*/
Set-setOfKeys=map.keySet();
/**
*从集合中获取迭代器实例
*/
迭代器迭代器=setOfKeys.Iterator();
/**
*循环迭代器,直到到达HashMap的最后一个元素
*/
while(iterator.hasNext()){
/**
*方法返回迭代器实例中的下一个键。
*next()方法的返回类型是Object,所以我们需要对字符串进行向下转换
*/
字符串键=(字符串)迭代器。下一步();
/**
*一旦我们知道了“键”,我们就可以从HashMap中获取值
*通过调用get()方法
*/
字符串值=map.get(键);
System.out.println(“键:+Key+”,值:+Value);
}
}
}

在onclick中,在减少值调用notifyDataSetChanged()后


但这是一个代价高昂的操作,因为在减少值调用notifyDataSetChanged()之后,它会在onclick中刷新完整列表


但这是一个代价高昂的操作,因为它会刷新完整的列表

您需要将所有更新的数据存储在列表中,然后使用adapter.notifydatasetchangedus使用回收器视图。onBindView()将刷新项目列表并