Android的 - SeparatedListAdapter - 如何获得准确的onClick项目持何立场? [英] Android - SeparatedListAdapter - How to get accurate item position on onClick?

查看:133
本文介绍了Android的 - SeparatedListAdapter - 如何获得准确的onClick项目持何立场?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我才能有由部分隔开一个ListView使用夏基的SeparatedListAdapter类。

I'm using Sharkey's SeparatedListAdapter class in order to have a ListView that is separated by sections.

类的伟大工程,但我遇到的问题是在我的 onListItemClick()处理程序。如果我有以下切片列表:

The class works great, but the problem I'm having is in my onListItemClick() handler. If I have the following sectioned list:

---------
| A     |
---------
| Alex  |
| Allan |
---------
| B     |
---------
| Barry |
| Bart  |
| Billy |
| Brian |
---------
| C     |
---------
etc...

当您想点击一个项目在列表中,你得到的位置的项目,然后用它做什么。

When you want to click on an item in the list, you get the position of the item and then do something with it.

在我的情况,我的名单是人名的动态列表。该名字来源于被放入一个列表℃的数据库; CustomClass> 再进名单。这是重要的,我知道这个项目的位置,我按一下,因为我用的是项目位置确定有关的人在列表对象的其他信息。

In my case, my list is a dynamic list of people's names. The names come from a database that is put into a List<CustomClass> before going into list. It's important for me to know the position of the item that I click on because I use the item position to determine other information about the person in the List object.

现在,问题是这样的: 当您单击列表中的项目,头算作项目,即使它们是不可点击。在上面的例子中,在列表中的位置如下

Now, the problem is this: When you click an item in the list, the headers count as items, even though they are unclickable. In the above example, the positions in the list are as follows

Alex = 1
Allan = 2
Barry = 4
Bart = 5
Billy = 6
Brian = 7

但在我原来的列表对象的顺序是,像这样

But in my original List object the order is like so

Alex = 0
Allan = 1
Barry = 2
Bart = 3
Billy = 4
Brian = 5

所以当我点击一个项目,像亚历克斯,我的code运行的onClick 处理程序,确定亚历克斯在位置 1 中,然后检索来自列表代替亚历克斯的Allans信息。

So whenever I click on an item, like Alex, my code runs the onClick handler, determines that Alex is at position 1 in the and then retrieves Allans info from the List instead of Alex's.

你看到的delima在这里?什么方法,我应该拿这个问题?

Do you see the delima here? What is the approach I should take to this problem?

推荐答案

您可以使用 setTag()的ListView你的每一个项目。这可以通过将这个标签来完成您返回从视图之前getView()函数的 listAdapter 。可以说,你的清单适配器返回 convertView ,然后在列表中的适配器。

You could use setTag() for your every item in the listView. This can be done by adding this tag before you return the view from getView() function in the listAdapter. lets say your list adapter return convertView ,then in the list adapter.

    public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null)
            {
                convertView = mLayoutInflater.inflate(R.layout.morestores_list_item, null);
                holder = new ViewHolder();

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            // populate holder here, that is what you get in onItemClick.
                    holder.mNameofItem = "NameofaPerson";
            return convertView;
        }

static class ViewHolder {
        TextView mName;
        Button mAction;
        ImageView mLogo;
            // use this class to store the data you need, and use appropriate data types.
            String mNameofItem.
    }

当过你的列表项的点击,使用

when ever you get a click on the list item, use

public void onItemClick(AdapterView<?> arg0, View convertView, int arg2,
            long arg3) {
                ViewHolder holder = (ViewHolder) convertView.getTag();
                // holder has what ever information you have passed.
                String nameofItemClicked = holder.mNameofItem;
                // access the information like this.
    }

此方法不需要在列表中的项目的位置,只是想让你知道不同的做法。

This approach doesn't need the position of the item in the list, just wanted to let you know different approach.

HTH。

这篇关于Android的 - SeparatedListAdapter - 如何获得准确的onClick项目持何立场?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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