2012-10-01 57 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包含很多产品,这个解决方案我认为不是最好的 –