Changing TextView value in custom ListView
Let's assume we have this example:
http://techdroid.kbeanie.com/2009/07/custom-listview-for-android.html
with source code available here:
http://code.google.com/p/myandroidwidgets/source/browse/trunk/Phonebook/src/com/abeanie/
How can we change the mobile phone number after clicking on the list item?
+3
source to share
1 answer
In the method, onItemClick()
get the element PhoneBook
corresponding to the position (position parameter) of the clicked row, update the value, and then notify the adapter that the data changed with a method call 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());
}
});
+5
source to share