[英]Problem with Android ListView after Items are removed
I seem to be having a problem with Android ListView. 我似乎对Android ListView有问题。 I'm trying to delete the selected item from ListView. 我正在尝试从ListView删除选定的项目。 The delete method is working and will allow me to delete as many items from my list as needed, but whenever I scroll to the bottom of the list (after atleast 1 item has been deleted) I get an Error that closes the Application. delete方法可以正常工作,可以让我根据需要从列表中删除任意多个项目,但是每当我滚动到列表的底部(删除了至少1个项目之后),我都会收到错误消息,该错误会关闭应用程序。
Here is my code: 这是我的代码:
public class MainActivity extends Activity {
private String[] mRoute;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mRoute = getResources().getStringArray(R.array.countries);
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, mRoute);
list.setAdapter(adapter);
registerForContextMenu(list);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int menuItemIndex = item.getItemId();
String[] menuItems = getResources().getStringArray(R.array.menu);
String menuItemName = menuItems[menuItemIndex];
String listItemName = mRoute[info.position];
// Remove String From List
List<String> routeArrayString = new ArrayList<String>(Arrays.asList(mRoute));
routeArrayString.remove(listItemName);
mRoute = routeArrayString.toArray(mRoute);
//Generate new ListView
ListView list = (ListView)findViewById(R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, mRoute);
list.setAdapter(adapter);
registerForContextMenu(list);
return true;
}
Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。
What error happens? 发生什么错误? Anyway your code is little bit ugly and this should be written in another way. 无论如何,您的代码有点难看,应该以另一种方式编写。 You should work with your adapter because it is like a bridge between you ListView and actual data. 您应该使用适配器,因为它就像ListView和实际数据之间的桥梁。 The BasicAdapter has special methods for notifying about data changes, just inherit your own adapter and manipulate with data directly through adapter. BasicAdapter具有通知数据更改的特殊方法,只需继承您自己的适配器并直接通过适配器处理数据即可。
I think adding items to the Array in my adapter was the problem. 我认为向适配器中的阵列添加项目是个问题。
public class MainActivity extends ListActivity {
private ArrayAdapter<String> dataAdapter;
private static final int REMOVE_ITEM = 1;
private String[] mRoute = {"One", "Two", "Three", "Four", "Five"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dataAdapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.itemName);
for (int i = 0; i < mRoute.length; i++) {
dataAdapter.add(mRoute[i]);
}
setListAdapter(dataAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
Resources resource = getApplicationContext().getResources();
menu.add(Menu.NONE,REMOVE_ITEM, REMOVE_ITEM, "REMOVE ITEM");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case REMOVE_ITEM:
dataAdapter.remove(dataAdapter.getItem(dataAdapter.getCount() - 1));
break;
}
return false;
}
}
Works! 作品!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.