删除项目后Android ListView出现问题

时间:2010-11-08 18:44:59

标签: android listview

我似乎遇到了Android ListView的问题。我正在尝试从ListView中删除所选项目。删除方法正常工作,将允许我根据需要从列表中删除任意数量的项目,但每当我滚动到列表的底部(删除至少1项后),我得到一个关闭应用程序的错误。

这是我的代码:

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;

        }

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

发生了什么错误?无论如何你的代码有点难看,这应该用另一种方式编写。您应该使用适配器,因为它就像ListView和实际数据之间的桥梁。 BasicAdapter具有通知数据更改的特殊方法,只需继承自己的适配器并通过适配器直接操作数据。

答案 1 :(得分:0)

我认为在我的适配器中向Array添加项目是个问题。

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;
    }


}

作品!