2014-10-07 57 views
0

我正在使用包含兩個按鈕的自定義列表視圖是或否當我單擊否按鈕時,將顯示包含編輯文本的佈局。列表視圖中有超過15個項目。當我嘗試使用TextWatcher()在第一項的編輯文本中鍵入並保存該值時,則該值將保存在第5個位置和第1個位置,即在第1個位置和第5個位置都保存相同的值。 我使用的代碼是:在列表視圖中將值設置爲edittext

holder.subQuestionAnswer.addTextChangedListener(new TextWatcher() { 

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

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      questionsModel.get(position).getQuestion_subquestion().get(0).setAnswer(holder.subQuestionAnswer.getText() 
        .toString()); 

      Log.e("value", "no_active<>"+position+"<>"+questionsModel.get(position).getQuestion_subquestion().get(0).getAnswer().toString()); 
     } 
    }); 
} 

我怎樣才能避免這種情況?

+0

這可能是由於在ListView中回收視圖。閱讀本文 - http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works – 2014-10-07 05:14:17

+0

看看[這個鏈接](http://android.amberfog.com/?p=296)到也許明白髮生了什麼事。 – tibuurcio 2014-10-07 05:14:47

回答

1

這是因爲回收的觀點。請檢查以下適配器。根據您的需要使用邏輯。

public class MultiSelectionProductAdapter extends ArrayAdapter<ProductListBean> { 

private Context context; 
private ArrayList<ProductListBean> productList; 
private ArrayList<ProductListBean> mOriginalValues; 
private LayoutInflater li; 


public MultiSelectionProductAdapter(Context ctx, int simpleListItem1, 
            ArrayList<ProductListBean> data) { 
    super(ctx, simpleListItem1, data); 

    this.context = ctx; 
    this.productList = data; 
    li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

} 

@Override 
public int getCount() { 
    return productList.size(); 
} 

@Override 
public ProductListBean getItem(int position) { 
    return productList.get(position); 
} 

@Override 
public long getItemId(int position) { 

    return position; 
} 

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

    final ViewHolder holder; 

    if (convertView == null) { 

     convertView = li.inflate(R.layout.component_inventory_prod_list, null); 
     holder = new ViewHolder(); 

     holder.tvProductName = (TextView) convertView.findViewById(R.id.tvProductName); 
     holder.tvProdShortCode = (TextView) convertView.findViewById(R.id.tvShortName); 
     holder.tvLastOrderedQty = (TextView) convertView.findViewById(R.id.tvLastOrderedQty); 
     holder.etQty = (EditText) convertView.findViewById(R.id.etQty); 
     holder.parentRL = (LinearLayout) convertView.findViewById(R.id.parentRL); 
     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    final ProductListBean tempBean = productList.get(position); 
    holder.etQty.setId(position); 
    final String productName = tempBean.getProductName(); 
    final String productCode = tempBean.getProductCode(); 
    final String productShortCode = tempBean.getProductShortCode(); 

    if (mSelectedProd != null && mSelectedProd.contains(productCode)) { 
     final int indexOfProd = mSelectedProd.indexOf(productCode); 
     holder.etQty.setText(mSelectedProducts.get(indexOfProd).getEnteredQty()); 
    } 

    holder.tvProductName.setText(productName); 
    holder.tvProdShortCode.setText(productShortCode); 
    holder.tvLastOrderedQty.setText(tempBean.getLastOrderedQty()); 
    holder.etQty.setText(tempBean.getEnteredQty()); 

    holder.parentRL.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      holder.etQty.requestFocus(); 
     } 
    }); 


    holder.etQty.addTextChangedListener(new TextWatcher() { 

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

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
             int after) { 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      final int pos = holder.etQty.getId(); 
      final String qty = holder.etQty.getText().toString(); 
      if (qty.length() > 0) { 
       if (String.valueOf(qty.charAt(0)).equals("0")) { 
        holder.etQty.setError("Invalid Quantity"); 
        holder.etQty.setText(""); 
       } else { 
        productList.get(pos).setEnteredQty(holder.etQty.getText().toString()); 
       } 
      } else { 
       productList.get(pos).setEnteredQty(""); 
      } 
     } 
    }); 

    return convertView; 
} 

static class ViewHolder { 
    TextView tvProductName, tvProdShortCode, tvLastOrderedQty; 
    EditText etQty; 
    LinearLayout parentRL; 
} 
相關問題