Android uses intent to start the activity by clicking the context menu

Source: Internet
Author: User

Android uses intent to start the activity by clicking the context menu

I have added a floating navigation menu to the app. I hope to start a new activity when I click the navigation menu, but the following code cannot be completed. Please take a look.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 28 29 30 31 32 33 34 35 36 38 39 40 41 42 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_view); dataSource = new DBDataSource(this); dataSource.open(); values = dataSource.getAllMahasiswa(); ArrayAdapter adapter = new ArrayAdapter ( this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); listView=(ListView) findViewById(android.R.id.list); registerForContextMenu(listView); } public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, 1000, 0, Edit); menu.add(0, 1001, 0, Hapus); } public boolean onContextMenuItemSelected(MenuItem item){ switch (item.getItemId()) { case 1000: Intent i = new Intent(ActivityView.this, Tes.class); startActivity(i); break; case 1001: //not yet break; } return super.onContextItemSelected(item); }
Solution

 

There are two methods to complete android development. The following code is used:
Code 1
@ Override
Public boolean onContextItemSelected (MenuItem item)
{
Switch (item. getItemId ())
{
Case R. id. pick_camera:

Break;
}
Return super. onContextItemSelected (item );
}
Code 2
@ Override
Public void onCreateContextMenu (ContextMenu menu, View v, ContextMenuInfo menuInfo)
{

GetMenuInflater (). inflate (R. menu. image_options, menu );
Super. onCreateContextMenu (menu, v, menuInfo );
}

Create image_options.xml in the menu resource path
Create image_options.xml in menu folder of resource.

 

Android: id = @ + id/pick_camera
Android: title = Use Camera>

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.