35

I am developing an android application.I will have a listview and i have set a context menu to appear when a listview item is long-pressed.How do i get the item from the listview item selected(say text from a listview textview) after an action from the contextmenu is chosen so i can process it? Here is some code:

protected void onCreate(Bundle savedInstanceState) {
    -------
    lv1 = (ListView) findViewById(R.id.listings);

    registerForContextMenu(lv1);
    lv1.setOnItemClickListener(this);

}

And the onCreateContextMenu:

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.watch:
        String name = "";
        return true;
    case R.id.buy:
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

I want to get text from a textview in a list item.How do i achieve that?

mungaih pk
  • 1,771
  • 8
  • 30
  • 56
  • So there are 2 list views, 1 being the main list and the other being the 3 items that show up when you long click. I don't see where the long click is being called. – Zypps987 Nov 24 '16 at 17:19

7 Answers7

70

you should register the LISTVIEW for the context menu.

Here's the source.

for onCreate():

 registerForContextMenu(lv);

And to access the selected item during long click:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
   if (v.getId() == R.id.lv) {
       ListView lv = (ListView) v;
       AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
       YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);

       menu.add("One");
       menu.add("Two");
       menu.add("Three");
       menu.add(obj.name);
   }
}
Violet Giraffe
  • 31,078
  • 43
  • 182
  • 317
Piyush
  • 24,288
  • 6
  • 40
  • 72
  • 14
    Piyush Gupta I am really grateful for your assistance.You dont know how much you have assisted me.I owe you a lot man.I had real trouble in this one,took me a week before posting it here.I appreciate it a lot.Thanks. – mungaih pk Sep 09 '13 at 15:28
  • Can you tell me what is the `YourObject` here? – Mohammad Areeb Siddiqui Nov 02 '14 at 18:20
  • @MohammadAreebSiddiqui this would be your own implementation of a class object to hold your data. – mungaih pk Nov 04 '14 at 07:38
  • @piyush please help me in this http://stackoverflow.com/questions/30449205/how-to-add-horizontal-scroll-view-and-a-listview-in-android?noredirect=1#comment48980553_30449205 – Pooja Dubey May 26 '15 at 04:35
36

To get the item from the ListView item selected refer to ContextMenuInfo object (see last implemented method below). Full solution as follows:

1) register ListView for context menu in ListActivity class

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    getListView().setAdapter(mAdapter);
    registerForContextMenu(getListView());
}

1a) if you have complex View on your list you might need to enable long click on each list view in Adapter class

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        RelativeLayout layout = (RelativeLayout) LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
        itemLayout = layout;
        itemLayout.setLongClickable(true);
    }
    // ...
    return view;
}

2) implement onCreateContextMenu() and onContextItemSelected() in ListActivity class

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    String title = ((MyItem) mAdapter.getItem(info.position)).getTitle();
    menu.setHeaderTitle(title);

    menu.add(Menu.NONE, MENU_CONTEXT_DELETE_ID, Menu.NONE, DELETE_TEXT);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_CONTEXT_DELETE_ID:
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Log.d(TAG, "removing item pos=" + info.position);
        mAdapter.remove(info.position);
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}
Kamil Seweryn
  • 2,002
  • 2
  • 16
  • 23
2

1) First we use

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.add("View Selected Text");
}

2) list--is ref if ListView

registerForContextMenu(list);

3)

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    if(item.getTitle().equals("View Selected Text"))
    {
        AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
        Contact c=array.get(menuInfo.position);
        Toast.makeText(List.this, "Selected String is :-"+c.toString(), Toast.LENGTH_SHORT).show();
    }
}
Tom
  • 3,023
  • 1
  • 24
  • 33
ashish
  • 217
  • 1
  • 11
1

first get list using id

Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());

String packname = context.getPackageName();

Intent LaunchIntent = getActivity().getPackageManager().getLaunchIntentForPackage(packageName);
if(LaunchIntent != null){
    startActivity(LaunchIntent);
}
else {
    Toast.makeText(getActivity().getBaseContext(),"APPLICATION IN NOT AVAILABEL", Toast.LENGTH_SHORT).show();
}
Yury
  • 19,958
  • 7
  • 55
  • 85
murala
  • 39
  • 5
0

use these methods, onCreateContextMenu and onContextItemSelected\

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    if (v.getId() == R.id.listview) {
        menu.setHeaderTitle("Delete");
        menu.add(Menu.NONE, 0, 0, "Delete from list");
    }
}

/**
 * Responding to context menu selected option
 * */

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
            .getMenuInfo();
    int menuItemIndex = item.getItemId();
    // check for selected option
    if (menuItemIndex == 0) {
        // user selected delete
        // delete the listrow
        ..(in your onitemclicklistener there is a parameter called as'postition' use this position and use some method to delete the data corresponding to the position value )
        // reloading same activity again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

    return true;
}
Maulik Sheth
  • 574
  • 3
  • 10
0

Write this in your longPressListener with the listview you use:

 ListView list = (ListView) findViewById(android.R.id.list);
 registerForContextMenu(list);

And this are the methods:

  @Override
   public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    Adapter adapter = getListAdapter();
    Object item = adapter.getItem(info.position);



    menu.setHeaderTitle("Choose");
    menu.add(0, v.getId(), 0, "Delete");

}


@Override
public boolean onContextItemSelected(MenuItem item) {

    if (item.getTitle() == "Delete") {
        deleteContact(item.getItemId());
    } else if (...) {
        // code
    } else {
        return false;
    }
    return true;

}

 public void deleteContact(int id){

// your code what to do for the clicked item
  }
silvia_aut
  • 1,445
  • 6
  • 19
  • 32
0

The above answers are very accurate and to the point for the case provided. That being said, I was brought here with using a convertView for my listview and am answering for those who are also brought here with this case.

If your LISTVIEW is using convertView and inflating a separate layout (say list_MyItem.xml), directly modify the list_MyItem.xml to have:

android:longClickable="true"

For example, if the listview is being populated with buttons modify the button as such:

<Button
    android:id="@+id/myButton"
    .
    .
    .
    android:longClickable="true"
/>
T.Woody
  • 1,014
  • 2
  • 11
  • 24