我似乎遇到了Android ListView的問題。我試圖從ListView中刪除選定的項目。刪除方法正在工作,並將允許我根據需要從列表中刪除儘可能多的項目,但每當滾動到列表的底部時(至少刪除1個項目後),都會出現一個關閉應用程序的錯誤。刪除項目後的Android ListView問題
這裏是我的代碼:
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;
}
任何想法?提前致謝。
什麼是錯誤信息? – Brandon 2010-11-08 19:11:43
使用logcat向我們顯示錯誤消息的完整堆棧跟蹤。 – 2010-11-08 19:46:31