问题AutoCompleteTextView和微调使用相同的适配器 [英] Problem with AutoCompleteTextView and Spinner using the same Adapter

查看:128
本文介绍了问题AutoCompleteTextView和微调使用相同的适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储为字符串,在我的应用程序的ArrayAdapter类别列表。这多少是pretty简单。该适配器活动和访问无处不在的领域。它的onCreate()期间值填充。

我有一个项对话框,包含使用该适配器,而且运作非常良好的AutoCompleteTextView。这基本上是新项目添加到列表的对话框。

我也有一个作为我的列表过滤器,并具有一个微调其中创建对话框时,使用相同的ArrayAdapter第二次对话。这里的问题。

如果我使用的过滤器对话框输入对话框前,微调将填充所有来自适配器的项目。效果很好。

任何我每次使用输入对话框中,AutoCompleteTextView正常工作。

问题来了,如果我使用的输入对话框,并在AutoCompleteTextView选择一个项目时,它表明的东西。在弹出的选择建议项目,即使我取消输入对话框,下一次我打开过滤器对话框后,微调最初显示从AutoCompleteTextView(而不是在适配器中的第一项),选择上次的项目和只显示在Spinner的列表单项目,如果点击/感动。解决它的唯一办法是结束应用程序并重新打开它。我没有得到在logcat中的任何错误或任何是很有帮助的。

编辑 - 好吧,我已经删除了previous code与我公司生产的简单的测试用例来取代它。底线是,我想知道这是否是一个错误,或者如果它预期的效果。当在AutoCompleteTextView选择一个建议,我们可以确认链接到它一个ArrayAdapter已滤波应用,以便它的计数的影响,如果该适配器在一个微调访问将是那些过滤下来,所显示出的唯一项。我还添加按钮显示举杯表明计数的影响。在自动完成键入TE,选择测试项目。他们尝试微调或点击按钮查看适配器的数量仅为2

所以,最后的问题是现在......可以适配器的过滤器复位(除通过键入并清除AutoCompleteTextView)?我找不到任何方法来做到这一点。我已经解决该问题在我的实际应用程序通过设置临时卡,复制在主适配器项目和设置的意见,使用临时替代适配器工作。我的手机运行的是2.2的,我已经测试高达在模拟器2.3.3 API级10。

有关main.xml中的XML

 <?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
< AutoCompleteTextView机器人:ID =@ + ID / ACTV
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentTop =真
    机器人:layout_centerHorizo​​ntal =真/>
<微调机器人:ID =@ + ID /飞旋
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerInParent =真/>
<按钮机器人:ID =@ + ID /按钮
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字=检查
    机器人:layout_alignParentBottom =真
    机器人:layout_centerInParent =真/>
< / RelativeLayout的>

在code为MainActivity.java

 公共类MainActivity延伸活动{
    / **当第一次创建活动调用。 * /    AutoCompleteTextView ACTV;
    微调微调;
    Button按钮;    的String [] = adapterList {的Test1,Test2的,Garbage1,Garbage2};    ArrayAdapter<串GT;适配器;    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        适配器=新ArrayAdapter<串GT;(这一点,android.R.layout.simple_dropdown_item_1line,adapterList);        ACTV =(AutoCompleteTextView)findViewById(R.id.actv);
        微调=(微调)findViewById(R.id.spinner);
        按钮=(按钮)findViewById(R.id.button);        actv.setAdapter(适配器);
        spinner.setAdapter(适配器);        button.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(视图v){
                Toast.makeText(MainActivity.this,+ adapter.getCount()+,在适配器项目,Toast.LENGTH_SHORT).show();
            }
        });
    }
}


解决方案

看着 AutoCompleteTextView 源$ C ​​$ C,它过滤一个ArrayAdapter:

<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/AutoCompleteTextView.java#AutoCompleteTextView.performFiltering%28java.lang.CharSequence%2Cint%29\" rel=\"nofollow\">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/AutoCompleteTextView.java#AutoCompleteTextView.performFiltering%28java.lang.CharSequence%2Cint%29

您可能想要删除对离心器的的onClick 回调过滤器:

 过滤器过滤器= adapter.getFilter();
过滤= NULL;

(请参阅 setAdapter AutoCompleteTextView 方法,围绕600线)

<一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/ArrayAdapter.java#ArrayAdapter.getFilter%28%29\" rel=\"nofollow\">http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/ArrayAdapter.java#ArrayAdapter.getFilter%28%29

让我知道,如果它的工作原理

顺便说一句,在适配器会动态改变?你是两个适配器的解决办法可能会比与过滤器搞乱一个更好的选择。如果用户启动一个单词,然后点击微调,取消它,并可以追溯到这个词?现在自动完成将没有任何意义,除非你保存过滤并以某种方式恢复它。结果
我相信,两个适配器(基于相同的字符串数组)是一个更好的解决方案

I have a list of "categories" that are stored as Strings in an ArrayAdapter in my app. This much is pretty simple. The adapter is a field of the Activity and accessible everywhere. It is populated with values during onCreate().

I have a "Entry" Dialog that contains an AutoCompleteTextView which uses this adapter and works very well. This is basically a dialog to add new items to a list.

I also have a second Dialog which acts as a filter for my list and has a single Spinner which when the Dialog is created, uses the same ArrayAdapter. Here's the problem.

If I use the filter Dialog before the entry Dialog, the Spinner is populated with all of the items from the adapter. Works well.

Any and every time I use the entry Dialog, the AutoCompleteTextView works properly.

The problem comes if I use the entry Dialog and select an item in the AutoCompleteTextView when it suggests something. After selecting a suggested item in the popup, even if I cancel the entry Dialog, the next time I bring up the filter Dialog, the Spinner initially shows the item that was last selected from the AutoCompleteTextView (instead of the first item in the adapter), and only displays that single item in the Spinner's list if clicked/touched. The only way to fix it is to end the application and re-open it. I'm not getting any errors or anything in logcat that is helpful.

EDIT - Okay, I've removed the previous code to replace it with a simple test case that I produced. The bottom line is that I would like to know if this is a bug, or if if it is expected results. When a suggestion is selected in an AutoCompleteTextView, I can confirm that the ArrayAdapter that is linked to it has filtering applied so that it's count is affected and the only items shown if the adapter is accessed in a Spinner will be those that were filtered down to. I also added button to display a toast to show that the count is affected. Type "te" in the autocomplete, select either Test entry. They try the spinner or click the button to see the adapter's count is only 2.

So the final question is now... can the adapter's filter be reset (other than by typing in and clearing the AutoCompleteTextView)? I can't find any method to do this. I have worked around the problem in my actual app by setting up a temporary adapter, copying over the main adapters items and setting the views to use the temporary adapter instead. My phone is running 2.2, and I have tested as high as 2.3.3 API level 10 in an emulator.

The xml for main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<AutoCompleteTextView android:id="@+id/actv"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
<Spinner android:id="@+id/spinner"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />
<Button android:id="@+id/button"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:text="Check It"
    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true" />
</RelativeLayout>

The code for MainActivity.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    AutoCompleteTextView actv;
    Spinner spinner;
    Button button;

    String [] adapterList = {"Test1", "Test2", "Garbage1", "Garbage2"};

    ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, adapterList);

        actv = (AutoCompleteTextView) findViewById(R.id.actv);
        spinner = (Spinner) findViewById(R.id.spinner);
        button = (Button) findViewById(R.id.button);

        actv.setAdapter(adapter);
        spinner.setAdapter(adapter);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "" + adapter.getCount() + " Items in Adapter", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

解决方案

Looking at AutoCompleteTextView source code, it does filter the ArrayAdapter:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/AutoCompleteTextView.java#AutoCompleteTextView.performFiltering%28java.lang.CharSequence%2Cint%29

You may want to remove the filter on an onClick callback in the spinner:

Filter filter = adapter.getFilter();
filter = null;

(see the setAdapter method in AutoCompleteTextView, around line 600)

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/widget/ArrayAdapter.java#ArrayAdapter.getFilter%28%29

Let me know if it works

BTW, is the adapter going to change dynamically? You're "workaround" of two adapters might be a better option than messing with the filter. What if the user starts a word, then clicks the spinner, cancels it and goes back to the word? Now the autocompletion will not make sense, unless you save the filter and restore it somehow.
I do believe that two adapters (based on the same string array) is a better solution.

这篇关于问题AutoCompleteTextView和微调使用相同的适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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