Search code examples
androidlistviewandroid-listviewlistadapter

Change a TextView value in a custom ListView


Suppose 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 modify a mobile phone number once clicked on the list item?


Solution

  • In the method onItemClick() get the PhoneBook element corresponding to the position(the position parameter) of the row clicked, update the value and then notify the adapter that the data has changed with a call to the method 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());
                }
            });