Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set specific context menu depending on which item is long-pressed in a ListActivity?

I have a list activity, and I chose to manually add the first item which is "add new item...".

I have registered the context menu for the whole listview, using registerForContextMenu(getListView()); straight into the onCreate.

When the context menu is build, the system calls onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo). The View v is the listView, and I cannot find a way to know which item in the listview is being long-pressed.

I could create a xml layout, with a layout for the "add new item..." and add a listview after, which would be populated by the activity, and that would react to the context menu, but I'm sure there is a way to solve this problem without any xml layout.

I have tried to register each view inside my listview using registerForContextMenu, which works, however the listview doesn't respond to touch anymore.

Here is my activity code listing:

public class AMain extends ListActivity {
    private List<String> pregList;
    private List<Long> pregIds;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        pregList = new ArrayList<String>();
        pregIds = new ArrayList<Long>();

        registerForContextMenu(getListView());
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        // TODO: hide the menu for the 1st item!!
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Logger.d("id = "+info.id);
        switch (item.getItemId()) {
        case R.id.menu_show:
            showPregnancy((int) info.id);
            return true;

        case R.id.menu_edit:
            editPregnancy((int) info.id);
            return true;

        case R.id.menu_delete:
            //TODO: do the deletion
            return true;

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

    protected void onStart() {
        super.onStart();

        clearPregList();
        loadPregList();
        getListView().setAdapter(new PregnancyListAdapter(this));
    }

    void clearPregList() {
        pregList.clear();
        pregIds.clear();
    }

    void loadPregList() {
        PregnanciesDbAdapter db = new PregnanciesDbAdapter(this);
        db.open();
        Cursor c = db.getPregnancies();

        if (c != null) {
            do {
                pregList.add(c.getString(c.getColumnIndex(PregnanciesDbAdapter.KEY_PREG_NOM)));
                pregIds.add(c.getLong(c.getColumnIndex(PregnanciesDbAdapter.KEY_PREG_ROWID)));
            } while (c.moveToNext());
            c.close();
        }

        db.close();
    }

    private class PregnancyListAdapter extends BaseAdapter {
        private Context context;

        public PregnancyListAdapter(Context ctx) {
            context = ctx;
        }

        @Override
        public int getCount() {
            return pregList.size()+1;
        }

        @Override
        public Object getItem(int position) {
            if (position == 0) { // add button
                return getString(R.string.addPreg);
            } else {
                return pregList.get(position-1);
            }
        }

        @Override
        public long getItemId(int position) {
            if (position == 0) {
                return -1;
            }
            return pregIds.get(position-1);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout itemLayout;

            itemLayout= (LinearLayout) LayoutInflater.from(context).inflate(R.layout.homelist_item_pregnancy, parent, false);

            ImageView logo = (ImageView) itemLayout.findViewById(R.id.logo);
            TextView pregName = (TextView) itemLayout.findViewById(R.id.pregName);

            if (position == 0) {
                itemLayout.setFocusable(false);
                itemLayout.setFocusableInTouchMode(false);
                pregName.setText(getString(R.string.addPreg));
            } else {
                logo.setVisibility(View.INVISIBLE);
                pregName.setText(pregList.get(position-1));
            }

            itemLayout.setId(position);

            return itemLayout;
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        if (position == 0) {
            startActivity(prepareIntent(AShowPregnancy.class, 0));
        } else {
            showPregnancy(position-1);
        }
    }

    void showPregnancy(int pregId) {
        startActivity(prepareIntent(AShowPregnancy.class, pregId));
    }

    void editPregnancy(int pregId) {
        startActivity(prepareIntent(ANewPregnancy.class, pregId));
    }

    Intent prepareIntent(Class<?> className, int pregId) {
        Intent i = new Intent(this, className);

        if (pregId > 0) {
            PregnanciesDbAdapter db = new PregnanciesDbAdapter(this);
            db.open();
            Pregnancy p = db.load(pregIds.get(pregId));
            db.close();
            i.putExtra(C.EXTRA_PREGNANCY, p);
        }

        return i;
    }
}

Thanks for reading. Hope you can help.

like image 272
Benoit Duffez Avatar asked Oct 13 '22 20:10

Benoit Duffez


1 Answers

Oh, god, again. I found out myself how to do it, and it was easier than easy.

AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
// info.position is the position in the ListView

I hate myself :)

like image 136
Benoit Duffez Avatar answered Oct 18 '22 01:10

Benoit Duffez