我发现了一些与我发布的内容相关的帖子,但没有一个帖子告诉我如何使用从数据库填充的小复杂listview来完成它。这就是我填充列表视图的方式:
final ListView lv = (ListView) getView().findViewById(R.id.mylistview);
String[] from = { "sender", "subject", "file_name" };
int[] to = { R.id.sender, R.id.subject, R.id.file_name };
ListAdapter adapter = new SimpleAdapter(getActivity(),mylist,
R.layout.list_item,from,to);
lv.setAdapter(adapter);
registerForContextMenu(lv);
到目前为止,代码工作正常,列表视图正在按预期填充,现在长按一下我正在显示上下文菜单,如:
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
if (v.getId()==R.id.mylistview)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
String[] menuItems = getResources().getStringArray(R.array.menu);
for (int i = 0; i<menuItems.length; i++)
{
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}
现在我想将R.id.sender设置为上下文菜单的标题怎么做?谢谢你的帮助。
答案 0 :(得分:0)
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
HashMap<String , String>h=new HashMap<String, String>();
if (v.getId()==R.id.mylistview)
{
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
String[] menuItems = getResources().getStringArray(R.array.menu);
h=(HashMap<String, String>) lv.getItemAtPosition(info.position);
String s=h.get("sender");
menu.setHeaderTitle(s);
for (int i = 0; i<menuItems.length; i++)
{
menu.add(Menu.NONE, i, i, menuItems[i]);
}
}
}