自定义的ListView适配器。对于错误的EditText TextChangedListener电话 [英] Custom ListView adapter. TextChangedListener calls for wrong EditText

查看:92
本文介绍了自定义的ListView适配器。对于错误的EditText TextChangedListener电话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有旅客自定义适配器什么由两个的EditText名单 - edtFirstName和edtLastName。我想,当用户输入文本将更改保存到列表中,当单击下一步按钮发送名单到另一个活动。

I have the list of travelers with custom adapter what consist two EditText - edtFirstName and edtLastName. I want when user enters text save changes to List, and when next button click send this List to another activity.

我的code:

public class TravellersAdapter extends BaseAdapter {

  private List<Traveler> itemsList;
  private LayoutInflater inflater;
  private Activity context;

  public TravellersAdapter(Activity context, List<Traveler> itemsList) {
    super();
    this.itemsList = itemsList;
    this.context = context;
    inflater = LayoutInflater.from(context);
  }

  public int getCount() { return itemsList.size();  }

  public Object getItem(int i) {  return itemsList.get(i);  }

  public View getView(final int position, View view, ViewGroup viewGroup) {
    if (view == null) {
      view = inflater.inflate(R.layout.traveller_item, null);
    }
    Traveler currentItem = (Traveler) getItem(position);

    EditText firstNameView = (EditText) view.findViewById(R.id.edtFirstName);
    firstNameView.addTextChangedListener(new TextWatcher() {

      public void afterTextChanged(Editable editable) {
        currentItem.setFirstName(editable.toString());
      }

    });

    return view;
  }
}

有关为例名单itemsList包括5项。当我编辑2-4元都可以,但是当我编辑分配给列表中的所有元素第一个或最后一个元素编辑的值。在dubugger我看到的方法 afterTextChanged 通话5次,位置不同的值。 如何解决这个问题?

For exemple List itemsList consist 5 items. When I edit 2-4 element all ok, but when I edit first or last element edited value assigned to all element in List. In dubugger i saw that method afterTextChanged calls 5 times with different values of position. How to fix it?

推荐答案

getView 方式,参数位置给出了新创建 childView <位置/ STRONG>,而不是点击childView的位置。

in getView method, the parameter position gives the position of the newly created childView, not the clicked childView's position.

用这个来得到正确的位置是:

use this to get the correct position:

final int actual_position = myList.getPositionForView((View) v.getParent());

的onClick(视图v); onClickListener 的任何视图中。在你的情况下,你必须执行 onTextChangedListener 作为该的EditText。

in onClick(View v); of the onClickListener of any View. In you case, you must implement onTextChangedListener for that EditText.

这里:

myList中是在ListView

myList is the ListView

v 是您单击的视图,在这种情况下,父母的childView(myList上)。

v is the View you clicked, in this case the childView of the parent(myList).

这篇关于自定义的ListView适配器。对于错误的EditText TextChangedListener电话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆