在自定义ListView中更改TextView值

时间:2012-03-25 08:36:58

标签: android listview android-listview listadapter

假设我们有这个例子:

http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html

源代码可在此处获取:

http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/

点击列表项后,我们如何修改手机号码?

1 个答案:

答案 0 :(得分:5)

在方法onItemClick()中,获取与单击的行的位置(位置参数)对应的PhoneBook元素,更新该值,然后通过调用向适配器通知数据已更改方法notifyDataSetChanged()

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View view, int position, long index) {
                // make the adapter a field in your class (or final)
                PhoneBook element = (PhoneBook) adapter.getItem(position);
                //modify the PhoneBook element
                element.setPhone("555-555-555");
                // notify the adapter that something has changed
                adapter.notifyDataSetChanged();
                showToast(listOfPhonebook.get(position).getName());
            }
        });