我尝试从列表视图中删除单击的项目但是我遇到了运行时错误。
我在R.array.sections
文件中有我的项目。
public class ListCategorieActivity extends Activity{
public static String RISULTATO = "RISULTATO";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] arrayList = getResources().getStringArray(R.array.sections);
setContentView(R.layout.activity_list_categorie);
ListView listview = (ListView) findViewById(R.id.listView1);
final ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,arrayList );
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> a, View v,int position, long id)
{
Bundle bundle = new Bundle();
Intent mIntent = new Intent();
String[] some_array = getResources().getStringArray(R.array.sections);
bundle.putString(RISULTATO,some_array[position]);
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
//try to remove clicked item
String toRemove = adapter.getItem(position);
adapter.remove(toRemove);
finish();
}
});
}
/*public void onItemClick(AdapterView<?> l, View v, int position, long id) {
Bundle bundle = new Bundle();
Intent mIntent = new Intent();
String[] some_array = getResources().getStringArray(R.array.sections);
bundle.putString(RISULTATO,some_array[position]);
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}*/
}
我的错误在哪里?
logcat的:
08-30 18:00:12.403 20111-20111/com.example.utente.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.UnsupportedOperationException
at java.util.AbstractList.remove(AbstractList.java:638)
at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75)
at java.util.AbstractCollection.remove(AbstractCollection.java:229)
at android.widget.ArrayAdapter.remove(ArrayAdapter.java:244)
at com.example.utente.myapplication.ListCategorieActivity$1.onItemClick(ListCategorieActivity.java:43)
at android.widget.AdapterView.performItemClick(AdapterView.java:301)
at android.widget.AbsListView.performItemClick(AbsListView.java:1287)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3080)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:4176)
at android.view.View.dispatchTouchEvent(View.java:7380)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2462)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2195)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2468)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2210)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2177)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1482)
at android.app.Activity.dispatchTouchEvent(Activity.java:2470)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2125)
at android.view.View.dispatchPointerEvent(View.java:7565)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3571)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3503)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4614)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4592)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4696)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:125)
at android.os.Looper.loop(Looper.java:124)
at android.app.ActivityThread.main(ActivityThread.java:4947)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
首先将其从arraylist中删除,然后更新适配器
arraylist.remove(postion) //position of item want to delete
然后 通知适配器或重新加载为工作
答案 1 :(得分:0)
试试这个;
答案 2 :(得分:0)
写
adapter.notifyDataSetChanged();
之后
adapter.remove(toRemove);