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 3356168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:28:57+00:00 2026-05-18T02:28:57+00:00

I’ve been working at this all day, and I’m really close but just can’t

  • 0

I’ve been working at this all day, and I’m really close but just can’t get this to work. I have a button that pulls up an AlertDialog populated with saved entries that include Name and Price. Right now, I can click an item in the Dialog and have it automatically fill in the Name and Price fields in my activity. I want to also be able to long press an item and receive an option to delete it. This is my first try at an Android app, and a lot of this is repurposed from the Notepad Tutorial. Two things I can’t figure out:

1) Is my registerForContextMenu sufficient/correct?

2) What am I doing wrong with my onCreateContextMenu?

Thanks.

savedItems.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {

      cDbHelper.open();
      mNotesCursor = cDbHelper.fetchAllSaved();
            startManagingCursor(mNotesCursor);

            // Create an array of names and corresponding prices from db
            String[] from = new String[]{SavedItemsDbAdapter.KEY_NAME, SavedItemsDbAdapter.KEY_PRICE};

            // and an array of the fields we want to bind those fields to
            int[] to = new int[]{R.id.text1, R.id.text2};

            // Now create a simple cursor adapter and set it to display
            SimpleCursorAdapter saved = 
                 new SimpleCursorAdapter(NewEntry.this, R.layout.saved_row, mNotesCursor, from, to);

      // Build an AlertDialog to hold this list
            AlertDialog.Builder builder = new AlertDialog.Builder(NewEntry.this);
      builder.setTitle("Choose from list");
      // IS THIS SUFFICIENT TO REGISTER FOR CONTEXT MENU?
      registerForContextMenu(v);
      builder.setAdapter(saved, new DialogInterface.OnClickListener() {

       // When an item from the list is clicked, it automatically populates the name and price fields in activity
       @Override
       public void onClick(DialogInterface dialog, int item) {
 Cursor c = mNotesCursor;
       c.moveToPosition(item);
 Intent i = new Intent(NewEntry.this, NewEntry.class);
       i.putExtra("name", c.getString( 
         c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_NAME)));
       i.putExtra("price", c.getString(
         c.getColumnIndexOrThrow(SavedItemsDbAdapter.KEY_PRICE)));
       startActivityForResult(i, ACTIVITY_AUTO);
       finish();
}

      // TRYING AND FAILING TO SET UP A CONTEXT MENU - the goal is to be able to long press, 
      // have a "Delete?" option pop up, which will delete the entry when clicked 
      @Override
      public void onCreateContextMenu(ContextMenu menu, View v,
    ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add(0, DELETE_ID, 0, R.string.menu_delete);
   }

   public boolean onContextItemSelected(MenuItem item) {

    switch(item.getItemId()) {
       case DELETE_ID:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        mDbHelper.deleteItem(info.id);
        return true;
    }
    return false;
   }
      });

      AlertDialog alert = builder.create();

         alert.show();

        }

     });

    }
  • 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-18T02:28:58+00:00Added an answer on May 18, 2026 at 2:28 am

    Is my registerForContextMenu sufficient/correct?

    You are registering a context menu for whatever savedItems is. If that is what you want the context menu on, then you are OK.

    If your goal is to have a context menu on the items in the list in the dialog, your approach is wrong. You will not be able to use AlertDialog.Builder. You will need to create a custom subclass of AlertDialog, so you can override onCreateContextMenu() there.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.