1

Already had the ability to delete a listview item using an onItemLongClick method but I'd rather use a floating context menu to do this.

Below is the code I currently have for the floating context menu. I followed the documentation which helped me set it up and then tried to search for a similar example to what I'm doing but couldn't find anything appropriate.

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

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()) {
            case R.id.edit:

                return true;
            case R.id.delete:

                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }

This is the code I had to delete the items in my listview before I decided to switch to a floating context menu

public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
        {
            String temp = paymentTitle.get(position).toString();
            paymentTitle.remove(position);
            paymentDate.remove(position);
            reminderDate.remove(position);
            reminderTime.remove(position);
            paymentVal.remove(position);

            mDatabase = new MOSDatabase(this);

            SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
            readableDB.delete("PaymentTable", "PTITLE=?",
                    new String[]{temp});

            aa.notifyDataSetChanged();

            return false;
        }

If someone could advise me on how to get this floating context menu working I'd be really grateful. I don't have the edit method done just yet, it's what I have to do after I get this completed.

Mark O'Sullivan
  • 10,138
  • 6
  • 39
  • 60
  • looking for this http://stackoverflow.com/questions/17698596/checkable-relative-layout-as-item-in-multiselect-list/17698673#17698673? – Raghunandan Aug 21 '13 at 17:15

1 Answers1

5

If I understand correctly, you can get the index of the item in the ListView at the click position by using the following code:

AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;

Using position, you can reuse the code of onItemLongClick pretty much as is:

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    int position = info.position;
    switch (item.getItemId()) {
        case R.id.edit:

            return true;
        case R.id.delete: {
            String temp = paymentTitle.get(position).toString();
            paymentTitle.remove(position);
            paymentDate.remove(position);
            reminderDate.remove(position);
            reminderTime.remove(position);
            paymentVal.remove(position);

            mDatabase = new MOSDatabase(this);

            SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
            readableDB.delete("PaymentTable", "PTITLE=?",
                    new String[]{temp});

            aa.notifyDataSetChanged();
            }
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

You might want to look at the answer to this question.

Community
  • 1
  • 1
jbl
  • 191
  • 7
  • `item.getMenuInfo()` is returning null value .. Can you help ? – Vinay Bhargav Jul 17 '14 at 11:24
  • I've got it working finally, I was using custom GridView in which `getContextMenuInfo()` method was not implemented. I've posted [here](http://vinaybhargav.wordpress.com/2014/07/20/android-floating-context-menu-for-listviewgridview/) in case if someone need a sample. – Vinay Bhargav Jul 20 '14 at 08:35