从Android中的ListView中删除项目

时间:2013-03-12 11:00:26

标签: android android-listview

我正在尝试使用列表中的删除按钮从列表视图中删除列表项。我的应用程序必须使用删除按钮。

数据存储在ArrayList<HashMap<String, String>>中,因此当我单击删除按钮时,数组列表中的数据将被删除,然后调用notifyDataSetChanged()。但是在此之后我得到了一个索引超出范围的异常,因为我的列表视图中只有一个项目。

请告诉我,如何在删除数据时阻止我的应用程序崩溃并显示空列表。

这是我的代码请看一下。

View vi = convertView;
if (convertView == null)
            vi = inflater.inflate(R.layout.notification_list, null);
        TextView name = (TextView)vi.findViewById(R.id.NotifScreenName);
        TextView message = (TextView)vi.findViewById(R.id.NotifScreenText);
        TextView time = (TextView)vi.findViewById(R.id.NotifScreenTime);
        ImageView image = (ImageView)vi.findViewById(R.id.NotifScreenPic);
        Button accept = (Button)vi.findViewById(R.id.NotifScreenAccept);
        Button decline = (Button)vi.findViewById(R.id.NotifScreenDecline);
        name.setText(data.get(position).get("name"));
        message.setText(data.get(position).get("text"));
        long dv = Long.valueOf(data.get(position).get("sentTime"))*1000;
        Date df = new java.util.Date(dv);
        String date = new SimpleDateFormat("MMM dd, hh:mm a").format(df);
        time.setText(date);
        imageLoader.DisplayImage(data.get(position).get("image"), image);
        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });
        decline.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                data.remove(position);
                notifyDataSetChanged();
            }
        });

这是Logcat输出:

03-12 11:04:32.784: E/AndroidRuntime(1150): FATAL EXCEPTION: main
03-12 11:04:32.784: E/AndroidRuntime(1150): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-12 11:04:32.784: E/AndroidRuntime(1150):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at java.util.ArrayList.get(ArrayList.java:304)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at com.pnf.myevent.ListAdapter.getView(ListAdapter.java:95)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.AbsListView.obtainView(AbsListView.java:2143)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.ListView.fillSpecific(ListView.java:1320)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.ListView.layoutChildren(ListView.java:1630)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.AbsListView.onLayout(AbsListView.java:1994)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.View.layout(View.java:14003)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:985)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.View.layout(View.java:14003)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:985)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.View.layout(View.java:14003)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:985)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.View.layout(View.java:14003)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.View.layout(View.java:14003)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.View.layout(View.java:14003)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.View.layout(View.java:14003)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewGroup.layout(ViewGroup.java:4375)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.Choreographer.doFrame(Choreographer.java:532)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.os.Handler.handleCallback(Handler.java:725)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.os.Looper.loop(Looper.java:137)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 11:04:32.784: E/AndroidRuntime(1150):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

在您的拒绝变量的onClickListener中调用data.remove(position)之前,请检查数据是否为空。

编辑:应该是data.get(position),您应该在创建视图时进行检查。

答案 1 :(得分:0)

请尝试此代码

data.remove(position-1);

答案 2 :(得分:0)

我做了类似的事情但使用onContextItemSelected。

public boolean onContextItemSelected(MenuItem item) {
    int itemId = item.getItemId();
    AdapterContextMenuInfo aInfo = (AdapterContextMenuInfo) item.getMenuInfo();
    planetsList.remove(aInfo.position)
    aAdpt.notifyDataSetChanged();
    return true;
}

其中planetList是一个ArrayList,而aAdpt是我的自定义适配器。 您可以在我的博客herehere上获得更多信息。 希望这能帮助你。