Claudio Pomo
Claudio Pomo

Reputation: 2472

listView with editText - TextWatcher reloading view

I've coded a ListView with some TextView and an EditText. This one has a TextWatcher which in afterTextChange() method add an instance to a sigleton class. All it's ok, but when I'm scrolling the ListView and I'm modifying value of an EditText, ListView is reloaded and scroll bar is returned on top. It's a normal feedback or I can control this behavior?

PART OF TEXTWATCHER

@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 of ADAPTER

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

Upvotes: 0

Views: 2147

Answers (2)

rahul
rahul

Reputation: 6487

It is normal behavior. You need to store the first visible index and set it to it when the listview is reloaded.

You need to use getFirstVisiblePosition() before reloading to find the index and after reloading use setSelection(position) to set it the older view position.

Upvotes: 2

A. AMAIDI
A. AMAIDI

Reputation: 6849

are you using adapter.notifyDataSetChanged() to reload your ListView? if you use this you won't face such problem..

Upvotes: 0

Related Questions