2013-05-31 145 views
-1

嗨,我知道這個問題已經出現在這裏,但答案並沒有幫助我。好的,我有一個活動,列表視圖綁定到ArrayListAdapter爲我的類ZamowieniePoz。Android ListView和EditText

public class ZamowieniePoz { 

private int _idZamowienie; 
private String _plu; 
private String _nazwa; 
private String _ilosc; 
private String _jm; 
private String _uwagi; 
private String _dataSync; 
private int _isSync; } 

這是我的ListView definitione:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:scrollbars="vertical"> 


    <ListView 
     android:id="@+id/listView" 
     android:scrollbars="vertical"   
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="fill_vertical" 
     android:layout_alignParentTop="true" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_below="@id/listView" > 

     <Button 
      android:id="@+id/btnWyslij" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="WYŚLIJ ZAMÓWIENIE" /> 

     <Button 
      android:id="@+id/btnPowrotZamowienieAdd" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="POWRÓT" /> 

    </LinearLayout> 

這是ListView的行:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txtPlu" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


    <EditText 
     android:id="@+id/txtIlosc" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content"   
     android:layout_toRightOf="@id/txtPlu" 
     android:layout_marginLeft="25sp" 
     android:lines="1" 
     android:inputType="numberDecimal" /> 
</RelativeLayout> 

這是ArrayListAdapter:

public class ZamowieniePozArrayAdapter extends ArrayAdapter<ZamowieniePoz> { 
     Context context; 
     int layoutResourceId; 
     ArrayList<ZamowieniePoz> data=null; 

     public ZamowieniePozArrayAdapter(Context context, int layoutResourceId, ArrayList<ZamowieniePoz> data) { 
      super(context, layoutResourceId, data); 
      this.layoutResourceId=layoutResourceId; 
      this.context=context; 
      this.data = data; 

     } 

     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      View row = convertView; 
      ZamowienieHolder holder = null; 

      if (row == null) {    
       LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
       row=inflater.inflate(layoutResourceId, parent, false); 

       holder = new ZamowienieHolder(); 
       holder.txtPlu = (TextView)row.findViewById(R.id.txtPlu); 
       holder.txtIlosc=(EditText)row.findViewById(R.id.txtIlosc); 

       holder.txtIlosc.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) {     
          data.get(position).setIlosc(s.toString()); 
        } 
       }); 

       row.setTag(holder); 
      } 
      else{ 
       holder = (ZamowienieHolder)row.getTag(); 
      } 

      ZamowieniePoz zamowieniePoz = data.get(position); 
      holder.txtPlu.setText(zamowieniePoz.getPlu()); 
      holder.txtIlosc.setText(zamowieniePoz.getIlosc()); 

      return row; 

     } 


     static class ZamowienieHolder { 
      TextView txtPlu; 
      EditText txtIlosc; 
     } 

} 

這是我如何綁定適配器的ListView:

final DbHandler db = new DbHandler(ZamowienieAddActivity.this); 

     listView = (ListView) findViewById(R.id.listView); 

     arrayZamowieniePoz = db.getAllZamowienie();  

     arrayAdapterZamowieniePoz = new ZamowieniePozArrayAdapter(this, R.layout.zamowienie_add_list_items, arrayZamowieniePoz); 

     listView.setAdapter(arrayAdapterZamowieniePoz); 

因此,這是它。問題是listView的第一行重複最後一行。我認爲這行代碼有些問題data.get(position).setIlosc(s.toString());在getTextChange中。

請幫幫我我不知道什麼是錯的。謝謝。

回答

0

TextWatcher同時作爲行View

被實例化。如果你得到一個循環視圖(row != null),在下面的代碼

@Override 
public void afterTextChanged(Editable s) {     
    data.get(position).setIlosc(s.toString()); 
} 

position仍將參考原始視圖的位置。

您需要重置每個迭代的TextWatcher,確保在再次調用addTextChangeListener()之前調用removeTextChangeListener()


一個簡單的實現可以做到這樣,雖然每次instanciating一個TextWatch可能不是最佳:

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    ZamowienieHolder holder = null; 

    if (row == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     row = inflater.inflate(layoutResourceId, parent, false); 

     holder = new ZamowienieHolder(); 
     holder.txtPlu = (TextView)row.findViewById(R.id.txtPlu); 
     holder.txtIlosc = (EditText)row.findViewById(R.id.txtIlosc); 

     row.setTag(holder); 
    } 
    else { 
     holder = (ZamowienieHolder)row.getTag(); 
    } 

    // Remove previous TextWatcher if any 
    if (holder.textWatcher != null) { 
     holder.txtIlosc.removeTextChangedListener(holder.textWatcher); 
    } 

    // Create the TextWatcher corresponding to this row 
    holder.textWatcher = new TextWatcher() { 
     @Override 
     public void afterTextChanged(Editable s) { 
      data.get(position).setIlosc(s.toString()); 
     } 

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

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

    holder.txtIlosc.addTextChangedListener(holder.textWatcher); 

    ZamowieniePoz zamowieniePoz = data.get(position); 
    holder.txtPlu.setText(zamowieniePoz.getPlu()); 
    holder.txtIlosc.setText(zamowieniePoz.getIlosc()); 

    return row; 
} 
+0

我加了一個TextWatcher和addTextChangeListener之前進行removeTextChangeListener和我doensn't改變任何東西。你可以發表一個例子如何以你的方式做到這一點。也許我做錯了什麼。 THX – silaros88

+0

@ user2440158編輯我的帖子,舉例 – nicopico

+0

我的上帝。尼科皮科你做了我的一天。有用 !!! – silaros88

0

使用arrayAdaper的貝婁碼。

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginBottom="@dimen/padding_small" 
    android:layout_marginTop="@dimen/padding_small" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingRight="@dimen/padding_large" 
     android:paddingTop="@dimen/padding_small" 
     android:text="Rename To: " 
     android:textColor="@color/black" 
     android:textStyle="bold" > 
    </TextView> 

    <EditText 
     android:id="@+id/ZoneRenameEditText" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/relay_txtbox" 
     android:ems="2" 
     android:maxLength="50" 
     android:paddingLeft="5dip" 
     android:paddingRight="5dip" 
     android:singleLine="true" 
     android:text="" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="3dip" 
    android:gravity="center_vertical|right" > 

    <ImageView 
     android:id="@+id/ZoneDoneImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="@dimen/padding_large" 
     android:background="@drawable/bk_done_button" /> 
</LinearLayout>