如何定义单击哪个ListView?

时间:2012-01-27 07:43:06

标签: java android listview

我一直在开发Android应用程序,每个视图有3个ListView和一个ContextMenu

@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo info) {

    menu.add(Menu.NONE, CONTEXT_MENU_ITEM_DELETE, Menu.NONE, "Delete");
    super.onCreateContextMenu(menu, view, info);
}

点击捕获注册:

this.registerForContextMenu(mFirstCategory);    
this.registerForContextMenu(mSecondCategory);
this.registerForContextMenu(mMainCategory);

mFirstCategory,mSecondCategory,mMainCategory是ListViews 。我也有点击行的方法:

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterContextMenuInfo info=(AdapterContextMenuInfo)item.getMenuInfo();
    String name = null;
    switch (info.targetView.getId()) {

    case (R.id.listViewFirst): name="First";
    case (R.id.listViewSecond): name="Second";
    case (R.id.listViewMain): name="Main";

    }

    Toast.makeText(this, name+"_"+String.valueOf(info.position), Toast.LENGTH_LONG).show();

    return super.onContextItemSelected(item);
}

我需要定义单击ListViewListView点击的位置(行)。我的“开关/案例”块无效。请告诉我,我该怎么办?

2 个答案:

答案 0 :(得分:1)

不确定这是否是您定义为“切换/案例块无法正常工作”,但似乎您忘记在每个break;之后致电case - 所以name始终为{ {1}}。

Main

答案 1 :(得分:0)

targetView是ListView中的单行。它的父级将是你的ListView:

final ListView listView = (ListView) info.targetView.getParent();  

然后做:

switch (listView.getId())