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>