2013-02-12 89 views
0

我有一個ListView與自定義適配器。每行有一個EditText。每個EditText都有一個與該行相關的初始值,用戶可以使用EditTexts更改此值。如何將EditText的更改存儲在ListView的項目中?

我想存儲這些更改的值。我爲每個EditText定義了TextWatcher,但它無法正常工作,尤其是當我滾動或過濾數據時。

我該怎麼做?

我使用合適的方法嗎?

回答

0

問題是由TextWatcher。我解決了它使用OnKeyListener

private boolean programmaticalTextChange = false; 

private KeyEvent UnknownKey = new KeyEvent(KeyEvent.ACTION_DOWN, 
      KeyEvent.KEYCODE_UNKNOWN); 

    public class MyTextWatcher implements TextWatcher 
    { 
     private EditText editText; 

     public MyTextWatcher(EditText editText) 
     { 
      this.editText = editText; 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 
      if (!programmaticalTextChange) 
       editText.dispatchKeyEvent(UnknownKey); 
     } 
    } 


@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
     //... 
     myEditText.setOnKeyListener(new OnKeyListener() { 

      @Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
       updateItem(item, v, rowView); 
       return false; 
      } 
     }); 
     myEditText.addTextChangedListener(new MyTextWatcher(myEditText)); 

} 

protected void updateItem(MyClass item, View v) { 
     try { 
      item.Qty = Double.valueOf(((EditText) v).getText() 
        .toString()); 
     } catch (Exception e) { 

     } 
    } 
相關問題