私はこの数日間髪を引っ張ってきました。ユーザーがキーを入力し、オートコンプリートの提案が値であるアンドロイドでオートコンプリートテキストビューをセットアップしようとしていますが、これを約10の異なる方法で試しました. BaseAdapter、SimpleAdapter、そして現在は ArrayAdapter を使用しています。デバッガーを介して、結果セットに問題がないことに気付きましたが、コードの publishResults() セクションで何をすべきかわかりません。最初の引数は、次の XML を使用したカスタム autocompletetextview コントロールです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/txtInnerView2" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
クラスは次のようになります。
public class NewArrayAdapter<T> extends ArrayAdapter implements Filterable {
ArrayList<String> allWords;
ArrayList<String> resultWords;
String value[] = { "Awesome", "Bear", "Cat", "Dog" };
String key[] = { "A", "B", "C", "D" };
public NewArrayAdapter(Context context, int resource, int textViewResourceId) {
super(context, resource, textViewResourceId);
// TODO Auto-generated constructor stub
allWords = new ArrayList<String>();
resultWords = new ArrayList<String>();
}
@Override
public Filter getFilter() {
Filter custom_filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults f = new FilterResults();
if (constraint != null) {
ArrayList<String> lstResults = new ArrayList<String>();
for (int x = 0; x < key.length; x++) {
if (key[x].startsWith(constraint.toString().toUpperCase())) {
lstResults.add(value[x]);
}
}
f.values = lstResults;
f.count = lstResults.size();
}
return f;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
resultWords.clear();
if (results.count > 0) {
resultWords.addAll((Collection<? extends String>) results.values);
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return custom_filter;
}
}
コンストラクターの int、public NewArrayAdapter(Context context, int resource, int textViewResourceId, List objects) 2 番目の引数はオートコンプリート テキスト ビュー、3 番目は入れ子になった TextView、4 番目はリストへの参照であり、最終的にはどうなるかを推測することしかできません結果セットである必要がありますが、明らかにそうではありません...これは私を夢中にさせています。誰か提案はありますか? 私の主な問題は、結果が値ではなくキーに基づいている必要があることです。たとえば、「a」と入力すると、ここでやろうとしていることの「tiddlywinks」の結果を意味する可能性があります。