动态的ListView滚动的TextView自动改变 [英] Dynamic ListView scroll TextView changed automatically

查看:624
本文介绍了动态的ListView滚动的TextView自动改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的形象

当我点击+按键增加的价格,然后单击 - 按钮减小它的工作完美的价格,但是当我滚动的ListView tvPrices的(TextView的)的值改变

我应该为住宿价格增量怎么办?

这里是我的适配器

 公共类ListAdapter延伸BaseAdapter { 公众的ArrayList<整数GT;量=新的ArrayList<整数GT;();
 公众的ArrayList<整数GT;价格=新的ArrayList<整数GT;();
 私有String [] listViewItems,价格,static_price;
 TypedArray图像;
 查看排= NULL; 静态字符串get_price,get_quntity;
 INT g_quntity,g_price,g_minus; 私人上下文的背景下;
 CustomButtonListener customButtonListener; 静态HashMap的<字符串,字符串>地图=新的HashMap<>();
 公共ListAdapter(上下文的背景下,字符串[] listViewItems,TypedArray图像的String []价格){
  this.context =背景;
  this.listViewItems = listViewItems;
  this.images =图像;
  this.prices =价格;  的for(int i = 0; I< listViewItems.length;我++){
   quantity.add(0);
   price.add(0);
  }
 } 公共无效setCustomButtonListener(CustomButtonListener customButtonListner){
  this.customButtonListener = customButtonListner;
 } @覆盖
 公众诠释的getCount(){
  返回listViewItems.length;
 } @覆盖
 公共字符串的getItem(INT位置){
  返回listViewItems [位置]
 }
 @覆盖
 众长getItemId(INT位置){
  返回0;
 } @覆盖
 公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){  最后ListViewHolder listViewHolder;
  如果(convertView == NULL){
   LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   行= layoutInflater.inflate(R.layout.activity_custom_listview,父母,假);
   listViewHolder =新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);
  }其他{
   排= convertView;
   listViewHolder =(ListViewHolder)convertView.getTag();
  }
  listViewHolder.ivProduct.setImageResource(images.getResourceId(位置,-1));
  尝试{   listViewHolder.edTextQuantity.setText(quantity.get(位置)+);  }赶上(例外五){
   e.printStackTrace();
  }  listViewHolder.btnPlus.setOnClickListener(新View.OnClickListener(){   @覆盖
   公共无效的onClick(视图v){
    如果(customButtonListener!= NULL){
     customButtonListener.onButtonClickListener(位置,listViewHolder.edTextQuantity,1);     quantity.set(位置,quantity.get(位置)+ 1);
     price.set(位置,price.get(位置)+ 1);     row.getTag(位置);     。get_price = listViewHolder.tvPrices.getText()的toString();     g_price = Integer.valueOf(static_price [位置]);     。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,+一);
// Toast.makeText(背景下,A+ A,Toast.LENGTH_LONG).show();
// Log.d(位置,+位置);
//的System.out.println(+位置+值+ map.values​​());
     ShowHashMapValue();
     listViewHolder.tvPrices.setText(+ g_price * g_quntity);
    }
   }  });
  listViewHolder.btnMinus.setOnClickListener(新View.OnClickListener(){
   @覆盖
   公共无效的onClick(视图v){
    如果(customButtonListener!= NULL){
     customButtonListener.onButtonClickListener(位置,listViewHolder.edTextQuantity,-1);
     如果(quantity.get(位置)大于0)
      quantity.set(位置,quantity.get(位置) - 1);     。get_price = listViewHolder.tvPrices.getText()的toString();
     g_minus = Integer.valueOf(get_price);
     g_price = Integer.valueOf(static_price [位置]);
     INT减= g_minus - g_price;
     如果(减去> = g_price){
      listViewHolder.tvPrices.setText(+负);
     }     map.put(+ listViewHolder.tvProductName.getText()的toString(),+ listViewHolder.edTextQuantity.getText()的toString());
     ShowHashMapValue();
    }
   }
  });
  listViewHolder.tvProductName.setText(listViewItems [位置]);
  listViewHolder.tvPrices.setText(价格[位置]);  返回行;
 } 私人无效ShowHashMapValue(){  / **
   *获取一组从HashMap的键
   * /
  设置setOfKeys = map.keySet();/ **
 *获得设置迭代器实例
 * /
  迭代器迭代器= setOfKeys.iterator();/ **
 *循环迭代,直到我们达到HashMap中的最后一个元素
 * /
  而(iterator.hasNext()){
/ **
 * next()方法返回的迭代器实例中的下一个关键。
 * next()方法的返回类型为对象,所以我们需要做的向下转换为字符串
 * /
   字符串键=(字符串)iterator.next();/ **
 *一旦我们知道了钥匙,我们可以从HashMap中获得的价值
 *通过调用get()方法
 * /
   字符串值= map.get(键);   的System.out.println(键+按键+,值:+值);
  }
 }
}


解决方案

在的onclick,你递减后的值调用notifyDataSetChanged()

notifyDataSetChanged

但其昂贵的操作,因为它刷新完整列表

when i click + button increment the price and click - button decrease the price it's work perfectly but when i scroll listview the value of tvPrices (TextView) is changed.

What should i do for the stay increment price?

here is my adapter

 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);
  }
 }
}

解决方案

In onclick, after you decrement the value call notifyDataSetChanged()

notifyDataSetChanged

but its a costly operation, since it refreshes complete list

这篇关于动态的ListView滚动的TextView自动改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆