滚动ListView时随机设置EditText值

时间:2015-10-08 13:55:39

标签: android android-listview

我有一个包含edittext的listview。我想在listview中的edittext中为每一行设置数量。但滚动后,edittext值会随机更改。 请帮帮我,任何答案都会得到满足。 提前谢谢。

我已经提到了List view edittext data loses on scrolling in Android

这是我的适配器类:

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyArrayAdapter extends ArrayAdapter<String> implements QuantityView.OnQuantityChangeListener
{
 List<String> objects;
 Context context;
 ViewHolder viewHolder = null;
 public MyArrayAdapter(Context context, int resource,List<String> objects) 
 {
  super(context, resource, objects);
  this.context=context;
  this.objects=objects;
 }
 static class ViewHolder
 {
  TextView txt;
  QuantityView quantityViewDefault; 
  EditText editText;
  TextWatcher qtyWatcher;
  int ref;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {

       try { 
              final ViewHolder holder;
              // TODO Auto-generated method stub 
              if (convertView == null) {
               holder = new ViewHolder();

          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          convertView = inflater.inflate(R.layout.row, null);

          holder.txt = (TextView) convertView.findViewById(R.id.txt);
          holder.editText = (EditText) convertView.findViewById(R.id.edittxt);

          convertView.setTag(holder);
          } else { 
              holder = (ViewHolder) convertView.getTag();
          } 
          holder.ref = position;
          if (holder.qtyWatcher != null) 
          {
              holder.editText.removeTextChangedListener(holder.qtyWatcher);
          } 

          holder.txt.setText(objects.get(position));
          holder.editText.setHint("Quantity");

          holder.qtyWatcher = new TextWatcher() 
          {
              @Override 
              public void beforeTextChanged(CharSequence s, int start,
                      int count, int after) 
              {
              } 

              @Override 
              public void onTextChanged(CharSequence s, int start,
                      int before, int count) {
              } 

              @Override 
              public void afterTextChanged(Editable s) {
                  //Update the quantity 
              } 
          }; 
          holder.editText.addTextChangedListener(holder.qtyWatcher);


          return convertView;
      } catch (NumberFormatException ex) 
      {
          Toast.makeText(context, "!!!", Toast.LENGTH_SHORT).show();
          ex.printStackTrace();
      } 
      return convertView;
 }
 @Override
 public void onQuantityChanged(int newQuantity, boolean programmatically) 
 {
  Toast.makeText(context, "Quantity: " + newQuantity, Toast.LENGTH_LONG).show();
 }
 @Override
 public void onLimitReached() 
 {
  Log.d(getClass().getSimpleName(), "Limit reached");
 }
}

1 个答案:

答案 0 :(得分:0)

getView(int position, View convertView, ViewGroup parent)方法中,您只需通过设置新的EditText和提示来更新TextWatcher

不使用TextWatcher更新的数量更新文本值会导致重用EditText以前的值。从而改变数据上下文中的值。