0

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);
    }
}
  • As using ContextMenu is certainly a valid option, it is a bit outdated in my opinion. I would recomend to make a layout with three buttons and either overlay it on the item selected with longclick, or use something like [this library](https://github.com/AAkira/ExpandableLayout) to expand this layout below. That's just a suggestion, ContextMenu still is an option. – Radek Kutyłowski Dec 12 '16 at 14:46

1 Answers1

0

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();
Leśniakiewicz
  • 874
  • 1
  • 10
  • 21
  • I think you didn't understand, I've done that already but the points of interest are the "//implement here" lines – Martin Mihalić Dec 12 '16 at 15:18
  • Thanks for your time, but I've already known the procedure for delete, I'm asking how to make window with EDIT action – Martin Mihalić Dec 14 '16 at 17:00
  • In R.id.edit just create AlertDialog with custom layout http://stackoverflow.com/questions/22655599/alertdialog-builder-with-custom-layout-and-edittext-cannot-access-view – Leśniakiewicz Dec 15 '16 at 08:23
  • and in positive button callback just change right item in your listViewAdapter list, then call listViewAdapter.notifyDataSetChanged(); – Leśniakiewicz Dec 15 '16 at 08:25