我一直在尝试使用上下文菜单从列表中的行中获取文本。到目前为止,我能够使用我的row.xml文件中的id获取文本名称。但是当我在列表中选择另一行时,它将返回包含在列表第一行中的相同名称,而不是我选择的那一行。
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>
答案 0 :(得分:2)
这不是正确的方法。您应该使用AdapterContextMenuInfo.position获取列表中数据的索引,然后将该数据传递给新活动。
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;
}
答案 1 :(得分:0)
您是否测试过info.position
然后从适配器位置,您可以检索所有列表项数据。