如何获得AutoCompleteTextView适配器的正确的ID [英] How to get correct ID of AutoCompleteTextView adapter

查看:187
本文介绍了如何获得AutoCompleteTextView适配器的正确的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新Android开发,我碰到了,我觉得很难解决的一个问题。我试图找出如何正确使用 AutoCompleteTextView 小部件。我想创建一个 AutoCompleteTextView ,使用从Web服务的XML数据。我设法得到它的工作,但我defenitely不满意的输出。

我想放的HashMap 使用id =>名称对进入AutoCompleteTextView并获得点击的项目的ID。当我点击自动完成过滤的输出,我想填充自动完成框,我也设法去上班下面的列表。

迄今所做的:


  • 自动完成非常适用于简单的的ArrayList ,所有的数据过滤
    正确

  • onItemClick 事件触发后点击正确

  • parent.getItemAtPosition(位置)返回正确的字符串
    再点击的项目presentation

事件 onItemClick(适配器视图父母,视图V,INT位置,长的ID)不表现为我想。我怎样才能找出点击项目的未过滤的排列位置?将过滤的位置是我不感兴趣的。

另外的问题:


  • 如何处理 HashMaps这样 AutoCompleteTextView 收藏

  • 如何在 onItemClick 活动的权利的itemId

我没有在这个问题上非常广泛的研究,但没有发现这会回答我的问题的任何有价值的信息。


解决方案

  

事件onItemClick(适配器视图父母,视图V,INT位置,长
  ID)不表现为我想。


这是一个正常的情况下过滤适配器时。尽管适配器保持来自其的角度到初始未滤波的数据的引用它有一个单一的集在其上的基础的数据(不管是初始的一个或从一个滤波操作造成)。但这不应该提出任何问题。使用默认的SDK适配器(或子类),在 onItemClick()你的位置为< STRONG>目前名单上的适配器为主。然后,您可以使用的getItem()来获取该数据项位置(同​​样并不如最初的事宜或过滤)。

 字符串数据=的getItem(位置);
INT realPosition = list.indexOf(数据); //如果你想知道的未经过滤的位置

这将为列出工作,地图(假设你使用 SimpleAdapter )。而对于一个地图你总是有增加一个额外的按键设置在初始列表中的位置未过滤的选项。

如果你使用自己的适配器沿 AutoCompleteTextView 你可以使 onItemClick()给你正确 ID (位置但不能更改)。​​

 公共类SpecialAutoComplete扩展AutoCompleteTextView {    公共SpecialAutoComplete(上下文的背景下){
        超级(上下文);
    }    @覆盖
    公共无效onFilterComplete(诠释计数){
        //当适配器完成了过滤器,这将被称为
        //操作和它通知AutoCompleteTextView
        长[] = realIds新长[计数] //这将举行真正的IDS从我们的地图
        的for(int i = 0; I&LT;计数;我++){
            最终的HashMap&LT;字符串,字符串&GT;项目=(HashMap的&LT;字符串,字符串&GT;)getAdapter()
                    .getItem(ⅰ);
            realIds [I] = Long.valueOf(item.get(ID)); //从经过滤项的id
        }
        //更新与真正的IDS适配器,使其具有正确的数据
        ((SimpleAdapterExtension)getAdapter())setRealIds(realIds)。
        super.onFilterComplete(计数);
    }
}

和适配器:

 公共类SimpleAdapterExtension扩展SimpleAdapter {    私人名单,LT ;?扩展地图&LT;字符串,字符串&GT;&GT; MDATA;
    私人长期[] mCurrentIds;    公共SimpleAdapterExtension(上下文的背景下,
            名单,LT ;?扩展地图&LT;字符串,字符串&GT;&GT;数据,INT资源,
            的String []从,INT []到){
        超(背景下,数据,资源,从,到);
        MDATA =数据;
    }    @覆盖
    众长getItemId(INT位置){
        //这将被用于得到提供给onItemClick回调的ID
        返回mCurrentIds [位置]
    }    @覆盖
    公共布尔hasStableIds(){
        返回true;
    }    公共无效setRealIds(长[] realIds){
        mCurrentIds = realIds;
    }}

如果您还实现了过滤器类适配器,那么你可以得到从那里的ID,而无需重写 AutoCompleTextView 类。

I am new to Android development and I ran into a problem which I find difficult to solve. I am trying to figure out how to use an AutoCompleteTextView widget properly. I want to create a AutoCompleteTextView, using XML data from a web service. I managed to get it to work, but I am defenitely not pleased with the output.

I would like to put a HashMap with id => name pairs into the AutoCompleteTextView and get the id of the clicked item. When I click on the autocomplete filtered set output, I want to populate a list underneath the autocompletion box, which I also managed to get to work.

Done so far:

  • autocomplete works well for simple ArrayList, all data filtered correct
  • onItemClick event fires properly after click
  • parent.getItemAtPosition(position) returns correct String representation of the clicked item

The event onItemClick(AdapterView parent, View v, int position, long id) does not behave as I would like. How can I figure out the unfiltered array position of the clicked item? The position of the filtered one is the one I am not interested in.

Further questions:

  • How to handle HashMaps or Collections in AutoCompleteTextView
  • How to get the right itemId in the onItemClick event

I did very extensive research on this issue, but did not find any valuable information which would answer my questions.

解决方案

The event onItemClick(AdapterView parent, View v, int position, long id) does not behave as I would like.

This is a normal situation when filtering an adapter. Although the adapter keeps a reference to the initial unfiltered data from its point of view it has a single set of data on which is based(no matter if is the initial one or resulted from a filter operation). But this shouldn't raise any problems. With the default sdk adapters(or with a subclass), in the onItemClick() you get the position for the current list on which the adapter is based. You could then use getItem() to get data item for that position(again it doesn't matter if initial or filtered).

String data = getItem(position);
int realPosition = list.indexOf(data); // if you want to know the unfiltered position

this will work for lists and Maps(assuming that you use the SimpleAdapter). And for a Maps you always have the option of adding an additional key to set the unfiltered position in the initial list.

If you use your own adapter along with an AutoCompleteTextView you could make the onItemClick() give you the right id(the position however you can't change).

public class SpecialAutoComplete extends AutoCompleteTextView {

    public SpecialAutoComplete(Context context) {
        super(context);
    }

    @Override
    public void onFilterComplete(int count) {
        // this will be called when the adapter finished the filter
        // operation and it notifies the AutoCompleteTextView
        long[] realIds = new long[count]; // this will hold the real ids from our maps
        for (int i = 0; i < count; i++) {
            final HashMap<String, String> item = (HashMap<String, String>) getAdapter()
                    .getItem(i);
            realIds[i] = Long.valueOf(item.get("id")); // get the ids from the filtered items
        }
        // update the adapter with the real ids so it has the proper data
        ((SimpleAdapterExtension) getAdapter()).setRealIds(realIds);
        super.onFilterComplete(count);
    }


}

and the adapter:

public class SimpleAdapterExtension extends SimpleAdapter {

    private List<? extends Map<String, String>> mData;
    private long[] mCurrentIds;

    public SimpleAdapterExtension(Context context,
            List<? extends Map<String, String>> data, int resource,
            String[] from, int[] to) {
        super(context, data, resource, from, to);
        mData = data;
    }

    @Override
    public long getItemId(int position) {       
        // this will be used to get the id provided to the onItemClick callback
        return mCurrentIds[position];
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    public void setRealIds(long[] realIds) {
        mCurrentIds = realIds;
    }

}

If you also implement the Filter class for the adapter then you could get the ids from there without the need to override the AutoCompleTextView class.

这篇关于如何获得AutoCompleteTextView适配器的正确的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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