Android使用onItemClick侦听器识别listView [英] Android Identify listView using onItemClick listener

查看:101
本文介绍了Android使用onItemClick侦听器识别listView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中有两个使用相同OnItemClickListenerListViews.有什么方法可以识别我现在按下的ListViews元素?我使用了以下代码:

I have two ListViews in my activity that uses same OnItemClickListener. Is there any way to identify which ListViews element I am pressing now? I have used this code:

@Override
public void onItemClick(AdapterView<?> list, View view, int position,
        long id) {


    if (view.getId() == R.id.listDictionary) {
    // TODO Auto-generated method stub


    Intent intent = new Intent(MainActivity.this, WordActivity.class);
    DictionaryListElement ele = (DictionaryListElement) dictionaryList
            .getAdapter().getItem(position);
    intent.putExtra("word", ele.getWord());

    startActivity(intent);
    } else if (view.getId() == R.id.listFavourites) {
        Intent intent = new Intent(MainActivity.this, WordActivity.class);
        String ele = (String)favouritesList.getAdapter().getItem(position);
        intent.putExtra("word", ele);
        startActivity(intent);

    }
}

但是它不起作用.我认为它正在获取每个按下的元素的ID,而不是ListViews

But it is not working. I think it is getting id of each pressed element not ListViews

推荐答案

您应使用ListView的ID(此处ListView作为AdapterView传递给onItemClick()),而不是View的ID因为此ViewListView项.

You should use the ID of ListView (here ListView is passed as AdapterView to onItemClick()), not the ID of View as this View is a ListView item.

if(list.getId() == R.id.listDictionary) {
    // item in dictionary list is clicked
} else if (list.getId() == R.id.listFavourites) {
   // item in favourite list is clicked
}

这篇关于Android使用onItemClick侦听器识别listView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆