如何在快速操作中获取listview itemid

时间:2012-03-25 10:42:40

标签: android listview quickaction

我将使用quickaction进行列表视图。 我使用此示例进行快速操作: http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/

但我如何从listviewitem获取ID以在setOnActionItemClickListener / onItemClick中使用它?

这里的代码:

ListView mList = (ListView) findViewById(android.R.id.list);
mList.setAdapter(entryAdapter);

//Add action item
ActionItem addAction = new ActionItem();
addAction.setTitle("Add");
addAction.setIcon(getResources().getDrawable(R.drawable.ic_add));

//Show action item
ActionItem accAction = new ActionItem();
accAction.setTitle("Accept");
accAction.setIcon(getResources().getDrawable(R.drawable.ic_accept));

final QuickAction mQuickAction  = new QuickAction(this);

mQuickAction.addActionItem(addAction);
mQuickAction.addActionItem(accAction);


//setup the action item click listener
mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
    public void onItemClick(QuickAction quickAction, int pos, int actionId) {
    if (pos == 0) { //Add item selected
        Toast.makeText(getApplicationContext(), "Add item selected", Toast.LENGTH_SHORT).show();
    } else if (pos == 1) { //Accept item selected
        Toast.makeText(getApplicationContext(), "Accept item selected", Toast.LENGTH_SHORT).show();
    }
     }
});

mList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    mQuickAction.show(view);
    }
});

2 个答案:

答案 0 :(得分:1)

您可以在onItemClickListener中获取id和位置并将其保存在变量中: 在onCreate之前添加:

int myId;

并将onItemClickListener更改为:

mList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    myId=id;
    mQuickAction.show(view);
    }
});

然后你可以使用myId作为你代码中点击项的id。

答案 1 :(得分:0)

创建ListView时,必须为其提供用于显示数据的列表或项目数组。 onItemClick方法为您提供所单击项目的编号。通过查询此位置数据的ListView支持数据结构,您可以确切地找到所点击的项目。