我正在尝试从适配器中删除项目。当我点击屏幕上的删除图标时,我收到异常。
这是我的代码:
public class MainApplication extends Activity {
String name[] = {"naveen","parveen","deepak"};
String item;
Object mActionMode;
private ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_application);
ListView listView = (ListView) findViewById(R.id.list_view);
arrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, name);
listView.setAdapter(arrayAdapter);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
item = name[position];
if(mActionMode!=null){
return false;
}
mActionMode = MainApplication.this.startActionMode(mactionMode);
return true;
}
});
}
private ActionMode.Callback mactionMode = new ActionMode.Callback() {
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle(item);
MenuInflater menuInflater = mode.getMenuInflater();
menuInflater.inflate(R.menu.menu,menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem ite) {
Toast.makeText(getApplicationContext(),"HH",Toast.LENGTH_LONG).show();
switch (ite.getItemId()){
case R.id.detlte_item :
arrayAdapter.remove(item);
arrayAdapter.notifyDataSetChanged();
mode.finish();
return true;
default:
return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
我在onActionItemClicked
中的这些行中收到错误:
arrayAdapter.remove(item);
arrayAdapter.notifyDataSetChanged();
mode.finish();
为什么我收到此错误?
08-27 10:09:35.621 16245-16245/com.example.naveen.listviewapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.naveen.listviewapp, PID: 16245
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.naveen.listviewapp.MainApplication$2.onActionItemClicked(MainApplication.java:65)
at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onActionItemClicked(PhoneWindow.java:2755)
at com.android.internal.view.StandaloneActionMode.onMenuItemSelected(StandaloneActionMode.java:133)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:546)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
at android.view.View.performClick(View.java:4456)
at android.view.View$PerformClick.run(View.java:18465)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的String item
无法投放到ArrayAdapter
项目。您需要从数组源中删除项目。
name= ArrayUtils.removeElement(name,item);
arrayAdapter.notifyDataSetChanged();
mode.finish();