【问题标题】:Android refresh ListViewAndroid刷新ListView
【发布时间】:2016-05-17 13:25:40
【问题描述】:

我知道关于这个问题有类似的问题,但没有一个对我有用。

我正在制作一个具有ListView 的闹钟应用程序,其中显示了存储在数据库中的所有闹钟。当用户长按其中一个ListView 项目时,会显示一个对话框,确认他们确实要删除选定的警报,单击“是”按钮后,警报将被删除。

问题是我的ListView 仅在我开始另一个活动后才会刷新,然后回到ListView 所在的那个,我想知道我需要做什么才能在警报响起时立即刷新它已删除。

PS.: 我已经试过adapter.notifyDataSetChanged();

这是我的代码:

UITools.adaptAlarmsListView(this, listView, R.layout.alarm_listview_item);
listView.setOnItemLongClickListener(
            new AdapterView.OnItemLongClickListener()
            {
                @Override
                public boolean onItemLongClick(AdapterView<?> adapterView,
                                               View view,
                                               final int position, long l)
                {
                    final boolean[] deletedFlag = {false}; // Tells if the alarm has been deleted
                    UITools.showDialogue(HomeActivity.this,
                            getString(R.string.delete),
                            getString(R.string.delete_question),
                            R.drawable.bin, getString(R.string.no),
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(
                                        DialogInterface dialogInterface, int i)
                                {
                                    // Do nothing
                                }
                            }, getString(R.string.yes),
                            new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(
                                        DialogInterface dialogInterface, int i)
                                {
                                    AlarmDAO.delete(getBaseContext(),
                                            position + 1);
                                    UITools.showToast(getBaseContext(),
                                            getString(R.string.deleted),
                                            Toast.LENGTH_SHORT);
                                    deletedFlag[0] = true;
                                }
                            });
                    if (deletedFlag[0])
                    {
                        listView.setAdapter(null);
                        UITools.adaptAlarmsListView(HomeActivity.this,
                                listView, R.layout.alarm_listview_item);
                    }
                    return true;
                }
            }
    );

UITools.adaptAlarmsListView:

/**
 * Adapts the alarms ListView
 * @param context - Context
 * @param listView - ListView
 * @param listViewItemId - int
 */
public static void adaptAlarmsListView(Context context, ListView listView,
                                 int listViewItemId)
{
    Alarm[] alarms = AlarmDAO.getAlarms(context);
    AlarmAdapter adapter = new AlarmAdapter(context, listViewItemId, alarms);
    listView.setAdapter(adapter);
}

【问题讨论】:

  • 你需要发布你的代码。
  • 只需重新运行最初加载它的代码即可。
  • @jzarsuelo,已发布

标签: android listview dataset refresh


【解决方案1】:
If your are using a fragment, just create an interface on that fragment 
and over the interface in the activity that hosts the fragment and
replace the present fragment with itself. or
create a method the re-initializes the listview, it adapter and the list.
if this is not clear enough, paste your code so i can check where the problem is.

【讨论】:

  • 不,我没有使用片段,我只是编辑了我的问题,现在发布了代码
  • 在 AlarmAdapter 类中,在添加任何新数据之前清除构造函数中的 ArrayList。然后在 listView.setAdapter(adapter); 之前adapter.notifyDataSetChanged();
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
  • 2011-01-08
  • 2016-01-17
  • 2016-04-22
相关资源
最近更新 更多