2012-10-01 40 views
0

我有一個textView和一個editText的listView。當用戶修改editText的值時,我會保存一個新的對象。我實現了一個TextWatcher,但我無法檢索到引用editText修飾的特定textView的值。有人幫助我嗎?addTextChangedListener保存一個新的對象

編輯

TextWatcher爲ListView控件的具體線路

quantity.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) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     if(!s.toString().isEmpty()){ 
      if (Integer.parseInt(s.toString())!=0){ 


       HashMap<Products, Integer> into = new HashMap<Products, Integer>(); 

       Products pr = new Products(); 
          //product contains value of textView 
       pr.set_id(Integer.parseInt(product.get("id"))); 
       pr.setNome(product.get("nome")); 
       into.put(pr, Integer.parseInt(s.toString())); 
       pArrayList.add(into); 
       cart.setProdotti(pArrayList); 
      } 
     } 
    } 
} 
+0

能否請您分享您的代碼添加textWatcher? –

+0

好的,我已經完成了! –

+0

在afterTextChanged()你想獲得不同的TextView的值?對不起,但我真的不知道你的問題... –

回答

1
<TextView android:id="@+id/p1_name" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:text="Product 1" /> 
<TextView android:id="@+id/p1_price" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:text="20" /> 


public void afterTextChanged(Editable s) { 
    ... 
    String price = ((TextView) findViewById(R.id.p1_price)).getText(); 
    ... 
} 
+0

沒關係,但是我會檢索傳遞給arrayadapter的HashMap ,並在我的TextWatcher中使用它,因爲並不是所有的值都被用到了HashMap中視圖 –

0

也許是有點矯枉過正,但你可以在每行setTag()方法用於信息查看的TextView和EditText上使用這樣的這兩個視圖將以某種方式通過此鍵連接。那麼你只需要調用findViewByTag()。這個操作有點貴,所以一定要在用戶停止寫入時調用它。

+0

我的listview包含很多產品,這個解決方案我認爲不是最好的 –