我有三個textview和一個編輯文本在列表中。當我在edittext中輸入任何值時,同樣的值也被複制到另一個edittext中。我厭倦了onFocusChangedListener和onTextChanged的實現,但這兩種情況的問題都是一樣的。在列表視圖中的EditText重複值
public class ProductListAdapter extends BaseAdapter {
Context context;
public ArrayList<Products> prodList;
private static LayoutInflater inflater = null;
public ProductsList productListActivity;
public ProductListAdapter(Context context,ArrayList<Products> prodList) {
this.context = context;
this.prodList = prodList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.e("In adapter", "yes");
}
@Override
public int getCount() {
return prodList.size();
}
@Override
public Products getItem(int position) {
return prodList.get(position); }
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.i(" in prodlist adapter","View holder");
final ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.productslistviewadapter, parent, false);
holder = new ViewHolder();
holder.tvdrCode = (TextView) convertView.findViewById(R.id.tvname);
holder.tvDrName = (TextView) convertView.findViewById(R.id.tvprodpack);
holder.tvterrcode= (TextView) convertView.findViewById(R.id.textView3);
holder.caption = (EditText)convertView.findViewById(R.id.editText1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Products p = prodList.get(position);
holder.tvdrCode.setText(p.getDocCode());
holder.tvDrName.setText(p.getDocName());
holder.tvterrcode.setText(p.getAdr());
//holder.caption.setText(prodList.get(position).caption);
/* holder.caption.setId(position);
holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
final int position = v.getId();
final EditText Caption = (EditText) v;
// prodList.get(position)= Caption.getText().toString();
String d = prodList.get(position).getDocCode();
Log.e("dr code",d);
}
}
}); */
holder.caption.setTag(position);
// holder.caption.setText(ProdList.get(position).toString());
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position);
holder.caption.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start,int before, int count) {
//2nd method String d = planArraylist.get(position).getDocCode();
final int position2 = holder.caption.getId();
final EditText Caption = (EditText) holder.caption;
if(Caption.getText().toString().length()>0){
String d = prodList.get(position2).getDocCode();
Log.e("dr code",d);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return convertView;
}
static class ViewHolder {
TextView tvdrCode;
TextView tvDrName;
TextView tvterrcode;
EditText caption;
}
}
XML代碼
下面是解決您的問題的完整示例.. http://www.widecodes.com/7NNzjgUqPe/edit-text-in-custom-list-view-loses-value-on-scroll.html – Piyush
Uncomment holder (位置).caption' – Harry
@Override public long getItemId(int position){ return prodList.indexOf(getItem(position)); } –