【问题标题】:listView with editText - TextWatcher reloading view带有editText的listView - TextWatcher重新加载视图
【发布时间】:2012-10-03 11:54:30
【问题描述】:

我用一些TextViewEditText 编写了一个ListView。这个有一个TextWatcher,它在afterTextChange() 方法中将一个实例添加到一个sigleton 类。一切都好,但是当我滚动ListView 并修改EditText 的值时,ListView 被重新加载并在顶部返回滚动条。 这是正常反馈还是我可以控制这种行为?

文本观察者的一部分

@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();
                into = cart.getProdotti();
                pr.set_id(Integer.parseInt(pro.get("id").toString()));
                pr.setPrezzo(Double.parseDouble(pro.get("prezzo").toString()));
                pr.setNome(pro.get("nome").toString());
                boolean flag = false;
                if(!into.isEmpty()){
                    Iterator<Products> iter = into.keySet().iterator();
                    while(iter.hasNext()){
                        if(pr.get_id()==iter.next().get_id()){
                            into.put(pr, Integer.parseInt(s.toString()));
                            cart.setProdotti(into);
                            flag = true;
                        }                       
                    }
                    if(flag==false){
                        into.put(pr, Integer.parseInt(s.toString()));
                        cart.setProdotti(into);
                    }
                }else{
                    into.put(pr, Integer.parseInt(s.toString()));
                    cart.setProdotti(into);
                }
            }
        }
    }

适配器的GETVIEW

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.product_row, null);

        TextView nome = (TextView)vi.findViewById(R.id.nomeProdotto); // title
        TextView descrizione = (TextView)vi.findViewById(R.id.descrizioneProdotto); // artist name
        TextView prezzo = (TextView)vi.findViewById(R.id.prezzoProdotto); // duration
        //ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        final EditText quantity = (EditText)vi.findViewById(R.id.quantity);


        product = products.get(position);


        nome.setText(product.get("nome"));
        descrizione.setText(product.get("descrizione"));
        prezzo.setText("€"+product.get("prezzo"));
        quantity.setText("0");
        quantity.addTextChangedListener(new CustomTextWatcher(products.get(position)));
        //imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
        return vi;
    }

【问题讨论】:

  • 您在哪里创建列表视图?滚动时,列表视图将销毁未显示的元素,并在您向后滚动时重新创建它们。
  • 发布代码可以帮助用户提出任何解决方案

标签: android android-layout android-listview android-edittext textwatcher


【解决方案1】:

这是正常行为。您需要存储第一个可见索引并在重新加载列表视图时将其设置为它。

您需要在重新加载之前使用 getFirstVisiblePosition() 来查找索引,在重新加载之后使用 setSelection(position) 将其设置为旧视图位置。

【讨论】:

  • 但是列表视图在哪里重新加载?我不调用任何重新加载!
【解决方案2】:

您是否使用adapter.notifyDataSetChanged() 重新加载您的ListView?如果您使用它,您将不会遇到这样的问题..

【讨论】:

    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多