2015-07-10 57 views
0

我有三個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代碼

​​

+0

下面是解決您的問題的完整示例.. http://www.widecodes.com/7NNzjgUqPe/edit-text-in-custom-list-view-loses-value-on-scroll.html – Piyush

+0

Uncomment holder (位置).caption' – Harry

+0

@Override public long getItemId(int position){ return prodList.indexOf(getItem(position)); } –

回答

5

正如我們所知道的ListView重用列表項的看法,因爲我們滾動整個列表。

所以問題出現了,當我們有一個自定義的ListView和EditText時,如果我們在第一個EditText中輸入任何值並開始滾動,那麼當我們滾動列表視圖時,EditText的值被一個一個地複製到另一個EditTexts。

發生這種情況時,listview重用視圖,並作爲另一個視圖中的其他視圖,即未看到的視圖向上滾動,重用舊列表視圖,因此該視圖的舊值在新的編輯文本中可見。

http://www.webplusandroid.com/creating-listview-with-edittext-and-textwatcher-in-android/

+1

該鏈接非常完整。我的問題解決了。 – Asha

1

添加到下面的答案,你應該總是使用如果其他條件把值getView(..)方法,如果沒有價值在其他條件下的放空的空間,我也面臨同樣的問題。

+0

謝謝你保存我的時間:) – Nilesh