update listview from simpleAdapter with new data

dell116 picture dell116 · Mar 15, 2011 · Viewed 9.8k times · Source

I've implemented a custom listview with multiple TextViews from a tutorial found here and all is working very well, but I'd like to be able to update the textviews on the fly with new data.

For example, I set a click listener on the items in the listview, and when clicked a dialog is shown that prompts the user to enter data. I can get the new data by calling populateList() and update the the array, but the new data isn't displayed in the textviews until the activity is restarted. How can I tell the simpleAdapter to get the new data and display it as soon as the dialog is dismissed?

EDIT: CODE SHOWN:

In onCreate:

lv = (ListView) findViewById(R.id.list);
adapter = new SimpleAdapter(
            this,
            list,
            R.layout.listtextview,
            new String[] {"Title","Desc"},
            new int[] {R.id.settingsListItem,R.id.settingsListDesc}
            );
    populateList(settingsList, settingsListDetails);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener
        (new OnItemClickListener()
            {
                public void onItemClick(AdapterView<?> parent, View    
                view,int position, long id) 
                {   
                   if (position == 0)
                                       {
                     showDialog(WAKE_TIME);
                   }        
                }
         });

to bring up the dialog:

public Dialog onCreateDialog(int id) 
{
    switch(id) {
        case WAKE_TIME :

   return new TimePickerDialog(this,WakeTimeSetListener,     
   wakeHour, wakeMinute, false);
}

the TimePicker dialog where notifyDataSetChanged is called:

private TimePickerDialog.OnTimeSetListener WakeTimeSetListener = new     
TimePickerDialog.OnTimeSetListener() 
{
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) 
        { 
            wakeHour = hourOfDay;            
            wakeMinute = minute;

            wakeHourText = Integer.toString(hourOfDay);
            wakeMinuteText = Integer.toString(minute);

            String preftime = hourOfDay + ":" + minute;

            SimpleDateFormat df = new SimpleDateFormat("HH:mm"); 
            SimpleDateFormat dfOut = new SimpleDateFormat("hh:mm a");

            try 
            {
                wakeDate = df.parse(preftime);
            } 
            catch (ParseException e1) 
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }  


                populateList(settingsList, settingsListDetails);

                adapter.notifyDataSetChanged(); 

        }

Answer

slund picture slund · Mar 15, 2011

Have you tried

adapter.notifyDataSetChanged();