使用Android中的上下文菜单从ListView中删除项目?

时间:2012-03-20 21:47:09

标签: android listview menu arraylist contextmenu

我正在尝试在Android中创建listmanager应用程序。我创建了一个ListView和一个ArrayList,我可以使用一个按钮和一个EditText添加项目。然后我创建了一个带有xml文件的上下文菜单,我可以删除列表项,添加新的,编辑它们等等。问题是我不知道如何在我的contextmenu的onContextItemSelected()方法中删除我的项目。更确切地说,我不知道如何引用此方法中的listitems,与所有其他操作相同,但我希望在成功删除时可以执行它们。 (抱歉我的英文不好,因为我使用它已经有一段时间了。:))

这是我的main.xml:      

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/linear"
    android:layout_weight="1"
    android:background="#000000"
    android:drawSelectorOnTop="true" />

<LinearLayout 
    android:id="@+id/linear"
    android:layout_alignParentBottom="true"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/entry"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_weight="1"/>
    <Button
        android:text="Hozzáad"
        android:id="@+id/bHozzaad"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>

这是我的contextmenu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/editItem"
      android:title="edit"/>

<item android:id="@+id/markItem"
      android:title="mark" />

<item android:id="@+id/deleteItem"
      android:title="delete" />

<item android:id="@+id/permItem"
      android:title="permanently delete" />

<item android:id="@+id/copyItem"
      android:title="copy" />

<item android:id="@+id/moveItem"
      android:title="move"/>
</menu>

这是我的主要java:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  final ArrayList<String> listaelemek = new ArrayList<String>();

  final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, listaelemek);
  setListAdapter(adapter);
  adapter.add("first item");

  setContentView(R.layout.main);

  ListView lv = getListView();
  lv.setTextFilterEnabled(true);
  registerForContextMenu(lv);   

  lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}
  });

  final Button hozzaad = (Button) findViewById(R.id.bHozzaad);
  hozzaad.setOnClickListener(new View.OnClickListener() 
        {
      public void onClick(View v) 
            {
            EditText entry = (EditText) findViewById(R.id.entry);
            listaelemek.add(entry.getText().toString());
            adapter.notifyDataSetChanged();
            }
        }
  );
}

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

@Override
public void onCreateContextMenu(android.view.ContextMenu menu, View v, ContextMenuInfo menuInfo)    
{
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.contextmenu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
 case R.id.editItem:
//do something 
return true;
 case R.id.markItem:
//do something
 return true;
 case R.id.deleteItem:
//here's my question mark :)
 return true;
 case R.id.permItem:
//do something
return true;
case R.id.copyItem:
//do something
return true;
case R.id.moveItem:
//do something
return true;
default:
return super.onContextItemSelected(item);
}
}

希望你能帮助我:)。

2 个答案:

答案 0 :(得分:11)

此代码应该完成这项工作:

adapter.remove(adapter.getItem(info.position));

答案 1 :(得分:1)

我在我的程序中写了上面的适配器代码,当我运行程序&amp;当时使用上下文菜单删除项目强制关闭窗口

这是我的代码

public class ShowContextMenu扩展了ListActivity {

ArrayAdapter<String> adapter;



/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

   final String []s=(getResources().getStringArray(R.array.names));

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

    setListAdapter(adapter);


    registerForContextMenu(getListView());
}

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context_menu, menu);
}






public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    String[] names = getResources().getStringArray(R.array.names);

    switch(item.getItemId()) {

    case R.id.edit:
        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.edit) + 
                " context menu option for " + names[(int)info.id],                          
                Toast.LENGTH_SHORT).show();



        return true;

    case R.id.save:
        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.save) + 
                " context menu option for " + names[(int)info.id],
                Toast.LENGTH_SHORT).show();
        return true;

    case R.id.delete:

//在此处插入代码

adapter.remove(adapter.getItem(info.position));


        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.delete) + 
                " context menu option for " + names[(int)info.id],
                Toast.LENGTH_SHORT).show();

        return true;

    case R.id.view:

        Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.view) + 
                " context menu option for " + names[(int)info.id],
                Toast.LENGTH_SHORT).show();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

}