使用新数据从simpleAdapter更新listview

时间:2011-03-15 22:47:24

标签: android listview simpleadapter

我已经从找到的here教程中实现了一个包含多个TextView的自定义列表视图,但一切都运行良好,但我希望能够使用新数据动态更新文本视图。

例如,我在列表视图中的项目上设置了单击侦听器,单击时会显示一个对话框,提示用户输入数据。我可以通过调用populateList()获取新数据并更新数组,但在重新启动活动之前,新数据不会显示在textviews中。如何在解除对话框后立即告诉simpleAdapter获取新数据并显示它?

编辑:代码显示:

on 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);
                   }        
                }
         });

打开对话框:

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

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

调用notifyDataSetChanged的TimePicker对话框:

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

        }

1 个答案:

答案 0 :(得分:3)

你试过吗

adapter.notifyDataSetChanged();