从删除的ListView项目在Android上的ContextMenu? [英] Deleting items from ListView with a contextmenu in Android?

查看:124
本文介绍了从删除的ListView项目在Android上的ContextMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个Android的应用程序li​​stmanager。我做了一个ListView和一个ArrayList到,我可以用一个按钮和一个EditText添加项目。然后,我做了一个上下文菜单的XML文件,我可以删除与listItems中,添加新的,编辑等的问题是,我不知道该怎么对我的ContextMenu的onContextItemSelected()方法中删除我的项目。更多precisely,我不知道如何来指listItems中的这种方法,同样与所有其他业务里面,但我希望我能做到他们当我与删除成功。 (对不起,我的英语不好,它已经有一段时间,因为我正确地使用它。))

I'm trying to make a listmanager application in Android. I made a ListView, and an ArrayList into which I can add items with a button and an EditText. Then I made a context menu with an xml file, which I can delete listitems with, add new ones, edit them etc. The problem is that I don't know how to delete my items within the onContextItemSelected() method of my contextmenu. More precisely, I don't know how to refer to the listitems inside of this method, same with all the other operations, but I hope I can do them when I succeeded with the deleting. (Sorry for my bad english, it's been a while since I properly used it. :))

下面是我的main.xml:
     

Here's my 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:

Here's my 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:

And here is my main 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);
}
}

希望你能帮助我:)

Hope you can help me :)

推荐答案

这code应该做的工作:

This code should do the job:

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

这篇关于从删除的ListView项目在Android上的ContextMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆