Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3459464
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:06:33+00:00 2026-05-18T10:06:33+00:00

I have a list activity, and I chose to manually add the first item

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:06:34+00:00Added an answer on May 18, 2026 at 10:06 am

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list activity which display all running applications in the device. it's
I have managed to reload the list item in a list when the activity
I have an activity which shows some List entries. When I click on a
I have an Android activity with a list. Each item can be clicked and
I have an activity with a listView, which uses a custom row layout defined
I have a ListView that opens another activity when an item row is clicked
I have a list of objects called Activity: class Activity { public Date activityDate;
So i have an activity that imports a list of names from a .txt
I have a popup window displaying when I click an item in my list
In my application, I have 2 list activities which can start one or the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.