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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:15:50+00:00 2026-05-18T08:15:50+00:00

this is the class. i want to add context menu to it on longpress

  • 0

this is the class. i want to add context menu to it on longpress but as a newbie i find i difficult. Please help.

 private ListView lvUsers;
 private ArrayList<UserBO> mListUsers;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.select);


        mListUsers = getUsers();
        lvUsers = (ListView) findViewById(R.id.lv_user);
        lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));                

    }

    public ArrayList<UserBO> getUsers(){          

  DBAdapter dbAdapter=DBAdapter.getDBAdapterInstance(this);
  try {
   dbAdapter.createDataBase();
  } catch (IOException e) {
   Log.i("*** select ",e.getMessage());
  }
     dbAdapter.openDataBase();  
  String query="SELECT * FROM user;";
  ArrayList<ArrayList<String>> stringList = dbAdapter.selectRecordsFromDBList(query, null);
  dbAdapter.close();

  ArrayList<UserBO> usersList = new ArrayList<UserBO>();
  for (int i = 0; i < stringList.size(); i++) {
   ArrayList<String> list = stringList.get(i);
   UserBO user = new UserBO();
   try {
    user.id = Integer.parseInt(list.get(0));
    user.name = list.get(1);
    user.age = list.get(2);
   } catch (Exception e) {
    Log.i("***" + Select.class.toString(), e.getMessage());
   }
   usersList.add(user);
  }
  return usersList;
 }

 // ***ListAdapter***
 private class ListAdapter extends ArrayAdapter<UserBO> {  // --CloneChangeRequired
  private ArrayList<UserBO> mList;  // --CloneChangeRequired
  private Context mContext;

  public ListAdapter(Context context, int textViewResourceId,ArrayList<UserBO> list) { // --CloneChangeRequired
   super(context, textViewResourceId, list);
   this.mList = list;
   this.mContext = context;
  }

  public View getView(int position, View convertView, ViewGroup parent){
   View view = convertView;
   try{
   if (view == null) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.list_item, null);  // --CloneChangeRequired(list_item)
   }
   final UserBO listItem = mList.get(position);  // --CloneChangeRequired    
   if (listItem != null) {
    // setting list_item views      
    ( (TextView) view.findViewById(R.id.tv_id) ).setText( listItem.getId()+"");
    ( (TextView) view.findViewById(R.id.tv_name) ).setText( listItem.getName() );
    ( (TextView) view.findViewById(R.id.tv_age) ).setText( listItem.getAge()+"" );

   }}catch(Exception e){
    Log.i(Select.ListAdapter.class.toString(), e.getMessage());    
   }
   return view;
  }

 }
  • 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-18T08:15:50+00:00Added an answer on May 18, 2026 at 8:15 am

    This should do the trick. Don’t forget to: registerForContextMenu(lvUsers);

    /** Called when the activity is first created. */
         @Override
         public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.select);
    
            mListUsers = getUsers();
            lvUsers = (ListView) findViewById(R.id.lv_user);
            lvUsers.setAdapter(new ListAdapter(this, R.id.lv_user, mListUsers));  
            **registerForContextMenu(lvUsers);**      
            }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
      super.onCreateContextMenu(menu, v, menuInfo);
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.context_menu, menu);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      switch (item.getItemId()) {
      case R.id.edit:
        editNote(info.id);
        return true;
      case R.id.delete:
        deleteNote(info.id);
        return true;
      default:
        return super.onContextItemSelected(item);
      }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/edit"
              android:title="@string/edit" />
        <item android:id="@+id/delete"
              android:title="@string/delete" />
    </menu>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this program is for class to display the database in Listview.i want to add
I want to put a context menu on a listview. This seems very simple
I wrote this n-array tree class. I want to write a method to add
This is the class I want to implement the onClickListener in: private void updateUserListView(DatabaseHandler
I have a context menu on a ListView. I would like to add a
I want to add context menu to left click of mouse button pressed over
I have subclassed a UITableViewCell and within this class I want to get it's
I have this class which I want to pass around Windows as LPARAM parameter.
I want to serialize this class: public class CarDisplay { public string Name {
i want to serialize this class, to store it on the sdcard of 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.