How to start intent activity when context menu clicked?

2.6k views Asked by At

I already had success on create the floating context menu when I long click one item on list view. However, when I click the menu on context menu there is nothing happening. I want to make it to start a new activity. Is there something wrong?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity_view);

    dataSource = new DBDataSource(this);

    dataSource.open();

    values = dataSource.getAllMahasiswa();

    ArrayAdapter<Mahasiswa> adapter = new ArrayAdapter<Mahasiswa>
    (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);           

 }
1

There are 1 answers

2
keshav On BEST ANSWER

Use this

@Override
public boolean onContextItemSelected(MenuItem item)
{
        switch (item.getItemId())
        {
        case R.id.pick_camera:

            break;
        }
    return super.onContextItemSelected(item);
}

with

@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 menu folder of resource.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/pick_camera"
        android:title="Use Camera">
    </item>

</menu>