简体   繁体   English

在ListView项上实施操作(添加/编辑/删除)

[英]Implementing actions (add/edit/delete) on ListView items

I am sort of new to Android since I've done only some basic applications, but I'm more familiar with Java. 由于我只做过一些基本的应用程序,所以我对Android有点陌生,但是我对Java更加熟悉。 I've been working on an application which should provide LoginActivity with two attributes that generate one Pair connection ( IP address and port ) at the top of the screen and a ListView of all already known connections (which were occasionally used before) right under the button "Connect". 我一直在开发一个应用程序,该应用程序应为LoginActivity提供两个属性,这些属性在屏幕顶部生成一个成对的连接( IP地址端口 ),并在列表的正下方显示所有已知连接的ListView (以前曾使用过)。按钮“连接”。

Snippet 片段

My question is, how to implement some actions like add/edit/delete etc. when holding on a finger little longer on certain item in the ListView? 我的问题是,当手指在ListView中的某些项目上停留更长的时间时,如何执行诸如添加/编辑/删除等操作? How to make some menu for modification/deletion of already known connections? 如何制作一些菜单来修改/删除已知连接?

Here's the code that I already got for the ListView: 这是我已经为ListView获得的代码:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    if (v.getId()==R.id.list_view) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_list, menu);
    }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId()) {
        case R.id.edit:
            //implement here
            return true;
        case R.id.delete:
            //implement here
        default:
            return super.onContextItemSelected(item);
    }
}

Maybe you have forgotten register your context menu to listview? 也许您忘记了将上下文菜单注册到ListView?

registerForContextMenu(lv);

EDIT 编辑

ok, now I understand. 好,现在我明白了。 Maybe this will help: 也许这会有所帮助:

 case R.id.delete:

     int index = info.position;
     listView.remove(index);
     listViewAdapter.notifyDataSetChanged();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM