如图:
长按事件后产生的上下文菜单为:
问题在于这个上下文菜单的标题不对头,
目前的一个简单的实现代码是
- Java code
public class ActivityMain extends Activity { private ListView lvPerson; //联系人列表 private int titleIndex; //List标题索引 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.list); lvPerson = (ListView)findViewById(R.id.lv_person); final List<HashMap<String, Object>> persons = new ArrayList<HashMap<String, Object>>(); for(int i=0;i<10;i++) { HashMap<String, Object> person = new HashMap<String, Object>(); person.put("personimage", R.drawable.android); person.put("personname", "name"); persons.add(person); } SimpleAdapter simpleAdapter = new SimpleAdapter(ActivityMain.this, persons, R.layout.personitem, new String[]{"personimage", "personname"}, new int[]{R.id.imgv_personImage, R.id.tv_personName}); lvPerson.setAdapter(simpleAdapter); lvPerson.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { ListView listView = (ListView)adapterView; HashMap<ImageView, String> person = (HashMap<ImageView, String>)listView.getItemAtPosition(position); String name = person.get("personname"); //打开联系人详细信息Activity Intent intent = new Intent(ActivityMain.this, ActivityPersonInfo.class); // 参数传递 intent.putExtra("name", name); ActivityMain.this.startActivity(intent); } }); lvPerson.setOnItemLongClickListener(new OnItemLongClickListener() { public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) { titleIndex = position; return false; } }); lvPerson.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { menu.setHeaderTitle(persons.get(titleIndex).toString()); menu.add(0, 0, 0, "test1"); menu.add(0, 1, 1, "test2"); menu.add(0, 2, 2, "test3"); menu.add(0, 3, 3, "test4"); } }); } }
问题就是在于
- Java code
lvPerson.setOnCreateContextMenuListener(new OnCreateContextMenuListener() { public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { menu.setHeaderTitle(persons.get(titleIndex).toString()); menu.add(0, 0, 0, "test1"); menu.add(0, 1, 1, "test2"); menu.add(0, 2, 2, "test3"); menu.add(0, 3, 3, "test4"); } });
setHeaderTitle时我不知道改如何正确得到所选listitem的text(标题),很笨的问题,麻烦高人给个指点,谢谢!
------解决方案--------------------