【发布时间】:2012-10-03 11:54:30
【问题描述】:
我用一些TextView 和EditText 编写了一个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