user1832478
user1832478

Reputation: 559

Getting text from list using Context Menu

I've been trying to get text from a row on my list using context menu. So far I was able to get the text back which was name using the id from my row.xml file. But when I select another row on my list it will get the same name back contained in the first row of the list and not the one I selected.

 public boolean onContextItemSelected(MenuItem item)
 {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();

    if(item.getTitle()=="Get")
    {
        TextView textView = (TextView) this.findViewById(R.id.names); 
        String text = textView.getText().toString(); 

        //Then pass value retrieved from the list to the another activity 
    }
 }

row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView 
    android:id="@+id/names"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

<TextView 
    android:id="@+id/pNumbers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

Upvotes: 0

Views: 1276

Answers (2)

Robby Pond
Robby Pond

Reputation: 73484

That's not the proper way to do that. You should use AdapterContextMenuInfo.position to get the index of the data in the list, and then pass that data to the new Activity.

AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();    
switch(item.getItemId()) { // using the id is the proper way to determine the menu item
    case R.id.menu_get:
        Object data = mListAdapter.getItem(info.position);
        // do something interesting with the data
        return true;
}

Upvotes: 2

madlymad
madlymad

Reputation: 6530

Have you tested info.position

Then from adapter position you can retrieve all list item data.

Upvotes: 0

Related Questions