I am trying to refresh my list view after deleting items from it, but i am not geting the desired result. The items are getting deleted when i reload the activity but not at the time i press the delete button. Here is my code:
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();
开发者_StackOverflow社区 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);
}
});
}
Any help would be appreciated. Thanx in advance.
Use notifyDataSetChanged() method with the listview within the condition where you are deleting the record , just after deleting the record put the listview.notifyDataSetChanged() code. hope this will solve your problem.
Did you tried invalidateViews, it will redrawn you list view.
My problem is solved. What i had to do was that whenever the arraylist was reloading i had to clear the previous one. Like:
array-list name.clear(); before adding items into the hashmap.
精彩评论