如何使用上下文菜单从listactivity中的listview中删除项目

时间:2013-12-21 21:02:37

标签: listview contextmenu listactivity

我有一个简单的应用程序,用于在单击选项菜单上的添加按钮时将项目添加到列表视图,然后当我长时间单击添加到列表视图中的其中一个项目然后选择重命名时或者删除并在列表视图中将该项目重命名或删除。我可以硬连接到listview中的一个项目,但我无法弄清楚在上下文菜单出现之前如何选择哪个项目,然后重命名或删除所选项目。

public class MainActivity_v3 extends ListActivity {

private int optionLastClickedId = -1;
private int optionClickedId = -1;
private boolean bSortByNum = true;

ArrayList<String> listItems=new ArrayList<String>();

ArrayAdapter<String> adapter;

AdapterContextMenuInfo info;

int internalPosition;

int clickCounter=0;

protected void onListItemClick(ListView l, View v, int position, long id){
    Toast toast = Toast.makeText(null, "you clicked " + position, 
Toast.LENGTH_LONG);
    toast.show();

}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) 
menuInfo;
    internalPosition = info.position; 

    final MenuInflater inflater = getMenuInflater(); // why is this final
    if(v == getListView()){
        inflater.inflate(R.menu.list_menu_v3, menu);
    }else {
    inflater.inflate(R.menu.main_menu_v3, menu);
    }
}

public boolean onContextItemSelected(MenuItem item){

    if(item.getMenuInfo() != null && item.getMenuInfo() instanceof 
AdapterContextMenuInfo){
        info = (AdapterContextMenuInfo) item.getMenuInfo();

    }

    switch(item.getItemId()){
    case R.id.mi_delete:
        this.removeItems(internalPosition);

        break;
    case R.id.mi_rename:
        this.renameItems(internalPosition, "test1");

        break;
    case R.id.mi_cancel:
        break;
    }

    adapter.notifyDataSetInvalidated(); // test - remove later

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

    int itemID = item.getItemId();

    switch(itemID){
    // switch(item.getItemId()){

            case R.id.add:
                this.addItems(getListView());
                break;

            case R.id.sort:
                Toast.makeText(this, "Sort", 
Toast.LENGTH_LONG).show();
                //adapter.sort();

    }

    return super.onOptionsItemSelected(item);

}

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu_v3, menu);
    return true;
}

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main_v3);

    View textView = findViewById(R.id.text);
    registerForContextMenu(textView);
    registerForContextMenu(getListView());

    adapter=new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1,
        listItems);
    setListAdapter(adapter);


} // end - 0nCreate --------------------------------------

public void addItems(View v) {
    listItems.add("Clicked : "+clickCounter++);
    adapter.notifyDataSetChanged();
}

public void removeItems(int selectedItem){
    listItems.remove(selectedItem);
    //listItems.remove(v);
    adapter.notifyDataSetChanged();
}

public void renameItems(int index, String object){
    //listItems.set(v., object);
    listItems.set(index, object);
    adapter.notifyDataSetChanged();
}

public boolean onPrepareOptionsMenu(Menu menu){

    MenuItem item = menu.findItem(optionLastClickedId);

    if(item != null){
        item.setEnabled(true);
    }
    item = menu.findItem(optionClickedId);
    if(item != null){
        item.setEnabled(false);
    }
    optionLastClickedId = optionClickedId;

    return super.onPrepareOptionsMenu(menu);
}

}

1 个答案:

答案 0 :(得分:0)

只需从ListView的Adapter中删除数据对象即可。可以从internalPosition找到要删除的项目。检查以下代码,了解如何查找internalPosition。

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        final int internalPosition = info.position; 
   }