2012-06-25 129 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