【问题标题】:addTextChangedListener to save a new objectaddTextChangedListener 保存一个新对象
【发布时间】:2012-10-01 18:22:38
【问题描述】:

我有一个带有一些 textView 和一个 editText 的 listView。当用户修改editText的值时,我会保存一个新对象。我实现了一个 TextWatcher,但我无法检索引用 editText 修改的特定 textView 的值。有人帮帮我吗?

编辑

listView 特定行的TextWatcher

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);
            }
        }
    }
}

【问题讨论】:

  • 你能分享你添加textWatcher的代码吗?
  • 在afterTextChanged()中你想获取不同TextView的值吗?对不起,我真的不明白你的问题......
  • 我有一个包含很多产品的 listView,对于每一个我都有一个 3 TextView:名称、价格、描述。现在我添加一个editText。当我修改这个 editText 时,我会保存一个新对象的实例。我的问题是将 textView 的值检索到 TextWatcher
  • 你需要为每个TextView分配一个唯一的id,然后在afterTextChanged()中你可以通过为每个id调用findViewById()来获取textView的值。
  • 添加了一些示例代码。如果我误解了克劳迪奥,请告诉我。

标签: android android-listview android-edittext textwatcher


【解决方案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();
    ...
}

【讨论】:

  • 没关系,但我会检索传递给 arrayadapter 的 HashMap 并在我的 TextWatcher 中使用它,因为并非 HashMap 中的所有值都用于视图
【解决方案2】:

也许这有点矫枉过正,但您可以在每一行中为 TextView 和 EditText 的 View 使用 setTag() 方法,这样两个 View 将通过这个键以某种方式连接。然后你只需要调用 findViewByTag()。此操作有点昂贵,因此请确保仅在用户停止写入时才调用它。

【讨论】:

  • 我的列表视图包含很多产品,我认为这个解决方案不是最好的
猜你喜欢
  • 1970-01-01
  • 2021-01-17
  • 2012-03-07
  • 2015-08-31
  • 2017-04-27
  • 2021-01-10
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多