我有一个“类别”列表,它们以字符串的形式存储在我的应用程序的ArrayAdapter中。这很简单。适配器是活动的一个字段,可以在任何地方访问。它在onCreate()期间使用值填充。
我有一个"Entry“对话框,其中包含一个使用此适配器且工作正常的AutoCompleteTextView。这基本上是一个向列表中添加新项目的对话框。
我还有第二个对话框,它充当我的列表的过滤器,并且有一个单独的微调器,当创建对话框时,它使用相同的ArrayAdapter。问题就在这里。
如果我在entry对话框之前使用filter对话框,微调器将填充来自适配器的所有项。效果很好。
每次我使用入口对话框时,AutoCompleteTextView都能正常工作。
如果我使用条目对话框并在AutoCompleteTextView中选择一个条目,当它提示某些东西时,问题就来了。在弹出窗口中选择了建议的项目后,即使我取消了条目对话框,下次打开过滤器对话框时,微调器最初也会显示上次从AutoCompleteTextView中选择的项目(而不是适配器中的第一个项目),如果单击/触摸,微调器只会在微调器列表中显示该项目。修复它的唯一方法是结束应用程序,然后重新打开它。我在logcat中没有得到任何错误或任何有用的东西。
编辑-好的,我已经删除了前面的代码,用我生成的一个简单的测试用例来代替它。底线是,我想知道这是一个bug,还是它是预期的结果。当在AutoCompleteTextView中选择一个建议时,我可以确认链接到它的ArrayAdapter应用了过滤,因此它的计数会受到影响,如果在微调器中访问适配器,则显示的唯一项将是那些被过滤到的项。我还添加了一个按钮来显示吐司,以表明计数受到影响。在autocomplete中键入"te“,然后选择任一Test条目。他们尝试微调或单击按钮查看适配器的计数仅为2。
所以最后一个问题是...是否可以重置适配器的筛选器(除了键入并清除AutoCompleteTextView之外)?我找不到任何方法来做这件事。我已经在我的实际应用程序中解决了这个问题,方法是设置一个临时适配器,复制主要适配器项,并将视图设置为使用临时适配器。我的手机运行的是2.2,并且我在模拟器中测试了高达2.3.3API level 10。
用于main.xml的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>
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();
}
});
}
}
发布于 2011-05-12 23:54:56
查看AutoCompleteTextView
源代码,它确实过滤了ArrayAdapter:
您可能希望在微调器中删除onClick
回调的滤镜:
Filter filter = adapter.getFilter();
filter = null;
(请参阅AutoCompleteTextView
中的setAdapter
方法,在第600行附近)
如果有效,请让我知道
顺便说一句,适配器会动态变化吗?你的“变通”两个适配器可能是一个更好的选择,而不是混乱的过滤器。如果用户开始一个单词,然后单击微调器,取消它并返回到该单词,该怎么办?现在,除非您保存筛选器并以某种方式恢复它,否则自动完成将没有意义。
我确实认为两个适配器(基于相同的字符串数组)是更好的解决方案。
https://stackoverflow.com/questions/5944314
复制相似问题