2

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(); 

        }
dell116
  • 5,835
  • 9
  • 52
  • 70

1 Answers1

4

Have you tried

adapter.notifyDataSetChanged();
slund
  • 6,367
  • 2
  • 26
  • 19
  • @Robby Pond : it's dirty, but it's coming. – dell116 Mar 15 '11 at 23:04
  • @dell116 - Sorry I didn't catch this right away. The SimpleAdapter is designed for static data only and will not update short of creating a whole new SimpleAdapter instance when your data changes. see additional answer here http://stackoverflow.com/questions/3313347/how-to-update-simpleadapter-in-android – slund Mar 16 '11 at 00:21
  • @slund - Don't apologize, thank you for your input. Need input! (Short Circuit movie reference) Is there any other way I could use the same tutorial, but with an ArrayAdapter or something else? This is really starting to frustrate me. – dell116 Mar 16 '11 at 00:55
  • @slund - You can call notifyDataSetChanged() on a SimpleAdapter. My issue was that when I called the populateList() method I wasn't re-setting the variable values correctly, leading me to believe that nothing had changed. The above code works fine with a proper call to populateList() that re-assigns the varibles in there with new data. – dell116 Mar 16 '11 at 19:13
  • @dell116 - excellent! Glad you got it working. And I too have learned something. – slund Mar 16 '11 at 20:15