我从listactivity扩展了类,并包含了hashmap的arraylist 我可以从数据库中删除该元素,但在切换到另一个活动并返回之前,我无法查看更新。
每次删除后如何刷新arraylist?
代码:
public class RemoveEvent extends ListActivity {
DBAdapter DB = new DBAdapter(this);
ArrayList<HashMap<String, String>> mylist;
RemoveArrayAdapter n;
SimpleAdapter mSchedule;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mylist = new ArrayList<HashMap<String, String>>();
mSchedule = new SimpleAdapter(this, mylist, R.layout.row, new String[] {
"NoEnent", "Date", "Time" }, new int[] { R.id.TRAIN_CELL,
R.id.FROM_CELL, R.id.TO_CELL });
DB.open();
Cursor c = DB.selectId();
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("NoEnent", c.getString(0));
map.put("Date", c.getString(1));
map.put("Time", c.getString(2));
mylist.add(map);
c.moveToNext();
}
mSchedule.notifyDataSetChanged();
n = new RemoveArrayAdapter(this, mylist);
setListAdapter(n);
}
public void are_u_sure(int position) {
final int p = position;
AlertDialog.Builder message = new AlertDialog.Builder(this);
message.setMessage("Are you sure you want to remove this event?");
message.setCancelable(false);
message.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(
@SuppressWarnings("unused") final DialogInterface dialog,
@SuppressWarnings("unused") final int id) {
HashMap<String, String> ob;
String s[] = new String[3];
ob = (HashMap<String, String>) mylist.get(p);
Iterator myVeryOwnIterator = ob.keySet().iterator();
int i = 0;
while (myVeryOwnIterator.hasNext()) {
String key = (String) myVeryOwnIterator.next();
String value = (String) ob.get(key);
s[i] = value;
i++;
}
DB.open();
DB.del_spec(s[0], s[1], s[2]);
}
});
message.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
@SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = message.create();
alert.show();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// get selected items
are_u_sure(position);
}
}
RemoveArrayAdapter的代码:
public class RemoveArrayAdapter extends ArrayAdapter<HashMap<String, String>>
{
private final Context context;
private final ArrayList<HashMap<String, String>> values;
public View vv;
public RemoveArrayAdapter(Context context, ArrayList<HashMap<String, String>> values) {
super(context, R.layout.removeevent, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.removeevent, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.TRAIN_CELL);
TextView textView2 = (TextView) rowView.findViewById(R.id.FROM_CELL);
TextView textView3 = (TextView) rowView.findViewById(R.id.TO_CELL);
ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
textView.setText(values.get(position).get("NoEnent"));
textView2.setText(values.get(position).get("Date"));
textView3.setText(values.get(position).get("Time"));
// I'm guessing you want to modify the Logo?!? if yes pass another ArrayList to this adapter
//contaning the info to set the ImageView
return rowView;
}
}
答案 0 :(得分:1)
Hashmap from List<>.
的对象后。您只需致电notifyDataSetChanged()
删除元素:
mylist.remove(position);
所以解决方案就像:
mylist.remove(position);
adapter.notifyDataSetChanged();