我正在尝试从中删除项目后刷新列表视图,但我没有找到所需的结果。当我重新加载活动时,项目将被删除,但在我按下删除按钮时不会删除。这是我的代码:
public void deletelist()
{
mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<favtitle.size();i++)
{
map = new HashMap<String, Object>();
map.put("train",favtitle.get(i));
map.put("value",favloc.get(i));
map.put("employer",favemp.get(i));
mylist.add(map);
}
mSchedule = new SimpleAdapter(this, mylist, R.layout.listdelete,
new String[] {"train","value","employer"}, new int[] {R.id.dept,R.id.jobloc,R.id.employer});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
lv.setCacheColorHint(Color.WHITE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
String str3 = favtitle.get(position);
db.delete("Favorites7","title= '"+str3+"'",null);
db.close();
//Data Base Select
db = openOrCreateDatabase(
"ClassifiedAds.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
//End-----
cur= db.query("Favorites7",
null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
favtitle.add(cur.getString(1));
favloc.add(cur.getString(3));
favemp.add(cur.getString(2));
lat.add(cur.getString(7));
log.add(cur.getString(8));
cur.moveToNext();
}
cur.close();
//mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<favtitle.size();i++)
{
map = new HashMap<String, Object>();
map.put("train",favtitle.get(i));
System.out.println("==="+favtitle.get(i));
map.put("value",favloc.get(i));
System.out.println("==="+favloc.get(i));
map.put("employer",favemp.get(i));
System.out.println("==="+favemp.get(i));
mylist.add(map);
}
mSchedule.notifyDataSetChanged();
mSchedule = new SimpleAdapter(Favorites.this, mylist, R.layout.listdelete,
new String[] {"train","value","employer"}, new int[] {R.id.dept,R.id.jobloc,R.id.employer});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
lv.setCacheColorHint(Color.WHITE);
}
});
}
任何帮助将不胜感激。 提前完成。
答案 0 :(得分:1)
使用 notifyDataSetChanged()方法在您要删除记录的情况下使用listview,只需在删除记录后添加 listview.notifyDataSetChanged()代码。 希望这能解决你的问题。
答案 1 :(得分:0)
您是否尝试过invalidateViews,它会重新绘制列表视图。
答案 2 :(得分:0)
我的问题解决了。我必须做的是,每当arraylist重新加载我必须清除前一个。 像:
数组列表名称 .clear(); 在将项添加到hashmap之前。