Div*_*ash 8 android autocompletetextview android-arrayadapter
我已经实现AutoCompleteTextView如下:
MainActivity.java
...
public static String[] myData=new String[]{"Africa (AF)","America (AFM)","Apple (AMP)"};
text=(AutoCompleteTextView)v.findViewById(R.id.first_state);
ArrayAdapter adapter = new ArrayAdapter(getActivity(),R.layout.autocompletetextview_row,R.id.textViewItem,myData);
text.setAdapter(adapter);
text.setThreshold(1);
text.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
selected_station = (String)parent.getItemAtPosition(position);
//TODO Do something with the selected text
}
});
Run Code Online (Sandbox Code Playgroud)
布局AutoCompleteTextView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:padding="10dp" >
<TextView
android:id="@+id/textViewItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="Item name here..."
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
当我输入类似"af .."时,它显示非洲(AF)选择但不是美国(AFM).
数据只是一个示例数据.这不是我AutoCompleteTextView只用于3件物品.
编辑:当我删除括号时,它正常工作.但我需要保留括号以供进一步使用.
BNK*_*BNK 14
在默认的实施过滤器用于ArrayAdapter在搜索词的开头(用空格隔开),我的意思是它使用startsWith.你将需要实现ArrayFilter它使用contains与一起startsWith.
您的问题将通过以下步骤解决:
ArrayAdapter.java从这里下载文件CustomArrayAdapter.java).在该文件中,您将找到一个私有类ArrayFilter.然后,添加valueText.contains(prefixString)并words[k].contains(prefixString)如下所示:
if (valueText.startsWith(prefixString) || valueText.contains(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString) || words[k].contains(prefixString)) {
newValues.add(value);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)使用ArrayAdapter为您定制的AutoCompleteTextView
这是结果截图:
希望这可以帮助!
Aya*_*fov 13
@BNK提供的答案是正确的.但是,我想提供一个类似的解决方案,它不需要整个ArrayAdapter类文件.相反,我们将只扩展该类并覆盖其仅有的两种方法:getView()和getFilter().因此,定义您的自动完成类:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class AutoSuggestAdapter extends ArrayAdapter
{
private Context context;
private int resource;
private List<String> items;
private List<String> tempItems;
private List<String> suggestions;
public AutoSuggestAdapter(Context context, int resource, List<String> items)
{
super(context, resource, 0, items);
this.context = context;
this.resource = resource;
this.items = items;
tempItems = new ArrayList<String>(items);
suggestions = new ArrayList<String>();
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (convertView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(resource, parent, false);
}
String item = items.get(position);
if (item != null && view instanceof TextView)
{
((TextView) view).setText(item);
}
return view;
}
@Override
public Filter getFilter()
{
return nameFilter;
}
Filter nameFilter = new Filter()
{
@Override
public CharSequence convertResultToString(Object resultValue)
{
String str = (String) resultValue;
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
if (constraint != null)
{
suggestions.clear();
for (String names : tempItems)
{
if (names.toLowerCase().contains(constraint.toString().toLowerCase()))
{
suggestions.add(names);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
}
else
{
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
{
List<String> filterList = (ArrayList<String>) results.values;
if (results != null && results.count > 0)
{
clear();
for (String item : filterList)
{
add(item);
notifyDataSetChanged();
}
}
}
};
}
Run Code Online (Sandbox Code Playgroud)
以XML格式定义自动完整视图,例如:
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter some text ..."/>
Run Code Online (Sandbox Code Playgroud)
并使用它:
AutoCompleteTextView autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
List <String> stringList = new ArrayList<String>();
stringList.add("Black");
stringList.add("White");
stringList.add("Yellow");
stringList.add("Green");
stringList.add("Blue");
stringList.add("Brown");
stringList.add("Orange");
stringList.add("Pink");
stringList.add("Violet");
stringList.add("Cyan");
stringList.add("LightBlue");
AutoSuggestAdapter adapter = new AutoSuggestAdapter(this, android.R.layout.simple_list_item_1, stringList);
autoComplete.setAdapter(adapter);
// specify the minimum type of characters before drop-down list is shown
autoComplete.setThreshold(1);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6410 次 |
| 最近记录: |