AutoCompleteAdapter类:
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>
现在我的名单是&#34;比利时&#34;,&#34;法国&#34;,&#34;意大利&#34;,&#34;德国&#34;,&#34;西班牙&#34 ; ,如果我输入&#34; ma&#34;,&#34;德国&#34;将在下拉列表中显示,但是当我再输入一个字符时,&#34; n&#34;如果我再继续输入一个字符&#34; y&#34;成为&#34;很多&#34;,&#34;德国&#34;再次显示。即使找到匹配项,AutoCompleteTextView
的下拉列表也会显示为开启和关闭。我的代码有什么问题?
答案 0 :(得分:2)
// try this
**activity_main.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="wrap_content">
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
**auto_complete_list-item.xml**
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
**MainActivity**
public class MainActivity extends Activity {
private AutoCompleteTextView autoComplete;
public static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
autoComplete.setAdapter(new PlacesAutoCompleteAdapter(this, R.layout.auto_complete_list_item));
autoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String str = (String) adapterView.getItemAtPosition(position);
autoComplete.setText(str);
}
});
}
private class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> resultList;
public PlacesAutoCompleteAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public int getCount() {
return resultList.size();
}
@Override
public String getItem(int index) {
return resultList.get(index);
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
final FilterResults filterResults = new FilterResults();
if (constraint != null) {
// Retrieve the autocomplete results.
ArrayList<String> matchList = new ArrayList<String>();
for (int i =0;i<COUNTRIES.length;i++){
if(COUNTRIES[i].toLowerCase().contains(constraint)){
matchList.add(COUNTRIES[i]);
}
}
resultList = matchList;
filterResults.values = resultList;
filterResults.count = resultList.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return filter;
}
}
}