我想從列表視圖中使用列表中的刪除按鈕刪除列表項目。刪除按鈕是我的應用程序必須的。從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)
請發佈您的logcat,以便您的應用崩潰的原因可以瞭解到 – onkar 2013-03-12 11:02:38
@onkar請檢查logcat ... – 2013-03-12 11:08:32
this line data.remove(position);數據是empy檢查之前刪除是空的或不行! – Dinesh 2013-03-12 11:11:50