问题描述
删除项目后,我无法更新我的ListView
. Listview项目已从数据库中删除,但我无法更新适配器,并且它一直在listView中显示已删除的项目.
I cannot update my ListView
after deleting an item. Listview item gets deleted from database but I cannot update my adapter and it keeps showing deleted item in listView.
如果我放入adapter.notiftDataSetChanged();
,它会奇怪地变成红色彩色代码!并显示cannot resolve the method notifyDataSetChanged().
if I put adapter.notiftDataSetChanged();
it turns into red colored code strangely! And shows that cannot resolve the method notifyDataSetChanged().
这是我的代码.
ArrayList<HashMap<String, String>> items = new ArrayList<>();
List<TRListFormat> list = trDb.getAllReminders();
for (TRListFormat val : list) {
HashMap<String, String> map = new HashMap<>();
map.put("title",val.getTitle());
map.put("description", val.getDes());
map.put("date", val.getDate());
map.put("time", val.getTime());
items.add(map);
}
adapter = new SimpleAdapter(this, items, R.layout.tr_list_format,
new String[] { "title", "description", "date", "time" },
new int[] {R.id.tbr_title, R.id.tbr_des, R.id.tbr_date, R.id.tbr_time });
lv = (ListView)findViewById(R.id.tbr_list);
lv.setAdapter(adapter);
我的setOnItemClickListener
,单击列表视图项目时,它会显示3个按钮的AlertDialog
.
my setOnItemClickListener
, it displays AlertDialog
with 3 buttons as the listview item is clicked.
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, int position, final long id) {
AlertDialog.Builder builder = new AlertDialog.Builder(TRList.this);
builder.setTitle("Select the Action");
builder.setMessage("Do anything");
builder.setPositiveButton("Edit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
int remId = (int)id;
Bundle dataBundle = new Bundle();
dataBundle.putInt("remId", remId);
Intent intent = new Intent(getApplicationContext(),TRTimeReminder.class);
intent.putExtras(dataBundle);
startActivity(intent);
}
});
builder.setNeutralButton("Deactivate",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("Delete",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
int remId = (int)id + 1;
TRListFormat format = trDb.getReminder(remId);
String title = format.getTitle();
String des = format.getDes();
String date = format.getDate();
String time = format.getTime();
trDb.deleteReminder(new TRListFormat(remId, title, des, date, time));
Toast.makeText(getApplicationContext(), "Reminder: "+title+"deleted",
Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged(); //this line shows an error I have written above
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
推荐答案
解决方案1 (首选)
notifyDataSetChanged
不适用于SimpleAdapter
.SimpleAdapter
用于静态数据.您应该使用其他类型的适配器,例如ArrayAdapter
.有关更多信息,请此处
notifyDataSetChanged
is not available for SimpleAdapter
.SimpleAdapter
is meant for static data. You should use a different type of adapter, like ArrayAdapter
. More information is here
解决方案2
调用此方法而不是adapter.notifyDataSetChanged();
public void refreshList(){
List<TRListFormat> list = trDb.getAllReminders();
items = new ArrayList<>();
for (TRListFormat val : list) {
HashMap<String, String> map = new HashMap<>();
map.put("title",val.getTitle());
map.put("description", val.getDes());
map.put("date", val.getDate());
map.put("time", val.getTime());
items.add(map);
}
adapter = new SimpleAdapter(this, items, R.layout.tr_list_format,
new String[] { "title", "description", "date", "time" },
new int[] {R.id.tbr_title, R.id.tbr_des, R.id.tbr_date, R.id.tbr_time });
lv.setAdapter(adapter);
}
这篇关于无法使用notifyDataSetChanged更新适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!