简体   繁体   中英

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. 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".

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? How to make some menu for modification/deletion of already known connections?

Here's the code that I already got for the 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?

registerForContextMenu(lv);

EDIT

ok, now I understand. Maybe this will help:

 case R.id.delete:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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