android 通过上下文菜单获取列表视图项
android get listview item by contextmenu
在我的应用程序中,我在 Listview
上注册了 ContextMenu,我希望通过上下文菜单单击 Listview
项。例如,如果我在列表视图中有两行具有这种结构:
public class StructReceiveSms{
public int userID;
public String username;
}
我的适配器可以在列表视图中显示用户名。现在我在下面的代码中可以在列表视图上定义 conext 菜单:
public class FragmentSmsReceiveMaster extends Fragment {
private static final Boolean DEBUG = true;
public ArrayAdapter adapter;
private ArrayList<StructReceiveSms> receiveSmsArray;
.
.
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.
.
.
smsView = (ListView) view.findViewById(R.id.listView);
smsView.setAdapter(adapter);
registerForContextMenu(smsView);
.
.
.
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
String[] menuItems = getResources().getStringArray(R.array.SmsMasterContextMenu);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int menuItemIndex = item.getItemId();
String listItemName = adapter.getItem(info.position) + "";
/* GET CLICKED LISTVIEW ITEM AFTER CHOOSE CONTEXTMENU MENU ITEMS */
Toast.makeText(G.currentActivity, listItemName, Toast.LENGTH_SHORT).show();
return true;
}
}
现在,在单击上下文菜单项后,我可以获取用户通过 menuItemIndex
单击的项,但无法获取 onContextItemSelected
函数中的 Listview
项。例如,在第一个项目上打开上下文菜单后,我可以获得 userID
和 username
并显示它。怎么做,谢谢
由于您的适配器的数据列表由 StructReceiveSms
个对象组成,因此 onContextItemSelected()
中的 adapter.getItem(info.position)
调用将 return 打开上下文菜单的列表项,但它将需要转换为 StructReceiveSms
类型。由此,你可以得到你想要的userID
和username
。
public boolean onContextItemSelected(MenuItem item)
{
...
StructReceiveSms listItem = (StructReceiveSms) adapter.getItem(info.position);
String selectedName = listItem.username;
int selectedId = listItem.userID;
...
}
这是假设您没有将 Adapter 的 getItem()
方法覆盖为 return 其他方法,但我想如果您有的话,您会证明这一点。
在我的应用程序中,我在 Listview
上注册了 ContextMenu,我希望通过上下文菜单单击 Listview
项。例如,如果我在列表视图中有两行具有这种结构:
public class StructReceiveSms{
public int userID;
public String username;
}
我的适配器可以在列表视图中显示用户名。现在我在下面的代码中可以在列表视图上定义 conext 菜单:
public class FragmentSmsReceiveMaster extends Fragment {
private static final Boolean DEBUG = true;
public ArrayAdapter adapter;
private ArrayList<StructReceiveSms> receiveSmsArray;
.
.
.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
.
.
.
smsView = (ListView) view.findViewById(R.id.listView);
smsView.setAdapter(adapter);
registerForContextMenu(smsView);
.
.
.
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
String[] menuItems = getResources().getStringArray(R.array.SmsMasterContextMenu);
for (int i = 0; i < menuItems.length; i++) {
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int menuItemIndex = item.getItemId();
String listItemName = adapter.getItem(info.position) + "";
/* GET CLICKED LISTVIEW ITEM AFTER CHOOSE CONTEXTMENU MENU ITEMS */
Toast.makeText(G.currentActivity, listItemName, Toast.LENGTH_SHORT).show();
return true;
}
}
现在,在单击上下文菜单项后,我可以获取用户通过 menuItemIndex
单击的项,但无法获取 onContextItemSelected
函数中的 Listview
项。例如,在第一个项目上打开上下文菜单后,我可以获得 userID
和 username
并显示它。怎么做,谢谢
由于您的适配器的数据列表由 StructReceiveSms
个对象组成,因此 onContextItemSelected()
中的 adapter.getItem(info.position)
调用将 return 打开上下文菜单的列表项,但它将需要转换为 StructReceiveSms
类型。由此,你可以得到你想要的userID
和username
。
public boolean onContextItemSelected(MenuItem item)
{
...
StructReceiveSms listItem = (StructReceiveSms) adapter.getItem(info.position);
String selectedName = listItem.username;
int selectedId = listItem.userID;
...
}
这是假设您没有将 Adapter 的 getItem()
方法覆盖为 return 其他方法,但我想如果您有的话,您会证明这一点。