0
AutoCompleteAdapter類:AutoCompleteTextView有時顯示的建議,但有時不
public class AutoCompleteAdapter extends ArrayAdapter<String> {
private LayoutInflater inflater;
private List<String> mData;
private int resourceId;
public AutoCompleteAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mData = objects;
resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null){
convertView = inflater.inflate(resourceId, null);
}
textView = (TextView)convertView.findViewById(R.id.textView1);
textView.setText(mData.get(position));
System.out.println("getView:" + mData);
return convertView;
}
@Override
public Filter getFilter() {
Filter custom_filter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults f = new FilterResults();
ArrayList<String> matchItems = new ArrayList<String>();
if (constraint != null){
for (String item : MainActivity.COUNTRIES){
if (item.toLowerCase().contains(constraint.toString().toLowerCase())){/////if contain the string
matchItems.add(item);
}
}
}
f.values = matchItems;
return f;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<String> matchItems = (ArrayList<String>) results.values;
mData.clear();
mData.addAll(matchItems);
System.out.println("publishResults ===" + mData); //data can be printed out here every time
notifyDataSetChanged();
}
};
return custom_filter;
}
}
MainActivity類別:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteAdapter adapter = new AutoCompleteAdapter(this, R.layout.row, new ArrayList<String>());
final AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.autoCompleteTextView1);
textView.setAdapter(adapter);
}
public static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
activity_main.xml中:
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/abc"
android:layout_marginRight="16dp"
android:layout_marginTop="50dp"
android:ems="10"
android:text=""
android:completionThreshold="1"
android:textColor="@android:color/black"
android:textColorHint="@android:color/black">
<requestFocus />
</AutoCompleteTextView>
row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
現在我的名單是「比利時」,「法國」,「意大利」,「德國」,「西班牙」,如果我輸入「馬」,「德」將在下拉列表顯示,但當我再輸入一個字符「n」變成「man」時,下拉消失,此後,如果我繼續輸入一個字符「y」變爲「多」,則「德國」再次顯示。即使找到匹配,AutoCompleteTextView
的下拉菜單也會顯示開啓和關閉狀態。我的代碼有什麼問題?
謝謝!我差點把我所有的頭髮都摘下來。我終於找出問題所在。我的問題只發生在平臺2.2中,但當我在平臺4.0中測試時,問題消失了。但是我仍然需要支持2.2及以上的版本,所以最後我發現在我寫入這個語句filterResults.count = resultList.size();後,它就可以工作了。這是多麼愚蠢......我已經通過API文檔讀了很多次,但它從來沒有提到這個... – GMsoF
什麼是adapterview在這裏? – 2014-11-07 04:47:08