我正在尝试制作一个包含名字的列表.此列表应该是可修改的(添加,删除,排序等).但是,每当我尝试更改ArrayAdapter中的项目时,程序都会崩溃,并显示java.lang.UnsupportedOperationException错误.这是我的代码:
ListView panel = (ListView) findViewById(R.id.panel);
String[] array = {"a","b","c","d","e","f","g"};
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, array);
adapter.setNotifyOnChange(true);
panel.setAdapter(adapter);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
adapter.insert("h", 7);
}
});
Run Code Online (Sandbox Code Playgroud)
我尝试了插入,删除和清除方法,但没有一个工作.有人会告诉我我做错了什么吗?
在我的代码中,我有一个ListActivity.列表项的上下文菜单选项之一是"删除",这将打开确认操作的对话框.我打算通过首先删除数据库中的项目数据然后从中删除它来实现此功能ArrayAdapter.它是从ArrayAdapter我得到的UnsupportedOperationException... 中删除它...
public void onClick(DialogInterface dialog, int id)
{
asynchronousDeleteEntry(CONTEXT_SELECTED_ID);
dialog.dismiss();
//I -know- that the adapter will always be an object
//of ArrayAdapter<JournalEntry> because this is the only type
//I ever call setListAdapter with. Debugging confirms this
@SuppressWarnings("unchecked")
final ArrayAdapter<JournalEntry> adapter = (ArrayAdapter<JournalEntry>)
journalViewerListActivity.this.getListAdapter();
//EXCEPTION OCCURS HERE
adapter.remove(adapter.getItem(CONTEXT_SELECTED_POSITION));
//refreshes the ListView to show the new items
adapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)
任何帮助赞赏.谢谢!