android - 删除元素后如何刷新数组列表?

标签 android listactivity android-adapter

我有类从 listactivity 扩展而来,并且包含 hashmap 的数组列表 我可以从数据库中删除该元素,但在切换到另一个 Activity 并返回之前我无法查看更新。

如何在每次删除后刷新数组列表?

代码:

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;
    }

}

最佳答案

删除 Hashmap from List<>. 的对象后.你只要调用notifyDataSetChanged()

删除元素:

mylist.remove(position);

所以解决方案是这样的:

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

关于android - 删除元素后如何刷新数组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11186364/

相关文章:

android - 自定义 EditText 默认光标位置

使用模拟器时 Android Studio 2.3.3 Logcat "Unable to connect to the editor...";该怎么办?

java - 在java中通过正则表达式分隔数字和数字

android - 如何从 Android 中的 ListActivity 中获取多个选定项目

java - setListAdapter 不在 ListActivity 中显示我的字符串数组

android - 在自定义 ListView 中覆盖或添加单行

android - 如何在android中将布局设置为RecyclerView

java - 关于如何优化这段代码有什么想法吗?我似乎想不出任何有效的办法

android - 我怎样才能在 Android 上从 ListActivity 创建 DialogFragment?

android - 在 onitemclick android 中设置微调项的选定值