2012-06-25 65 views
0

我有从列表活动扩展的类,并包含hashmap的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; 
    } 

} 
+1

我怎么能刷新数组列表后每个去除?我试图使用“registerForContextMenu();” => **为什么你使用registerForContextMenu()?仅供参考,它是有关上下文菜单其实** –

+0

对不起,我把它在问题的错误方式:$ – SWE

+0

@SWE漂亮的颜色:) –

回答

1

去除Hashmap from List<>.对象之后。你只需拨打notifyDataSetChanged()

删除元素:

mylist.remove(position); 

因此,解决办法是这样的:

mylist.remove(position); 
adapter.notifyDataSetChanged(); 
+0

谢谢,但它没有工作 – SWE

+0

任何其他的想法吗? – SWE