在Xamarin中,当我有一个带有下拉列表的AutoCompleteTextView
或MultiAutoCompleteTextView
时,是否可以搜索出现在字符串项目中间的字符串出现?
例如,如果我有以下字符串数组:
String[] countries = new string[] { "Item1", "Item2", "Item3", "Item4", "Item5" };
我在AutoCompleteTextView
或MultiAutoCompleteTextView
中输入“Ite”,将显示上面的所有项目。
我的问题与以下字符串数组有关:
String[] countries = new string[] { "1abcd", "2abdc", "1234", "Item42abcd", "Item5" };
是否可以在AutoCompleteTextView
或MultiAutoCompleteTextView
中键入“abcd”,并显示以下内容:
"1abcd", "2abdc", "Item42abcd"
答案 0 :(得分:0)
您必须自己创建自己的适配器和过滤器,默认适配器与startsWith
一起使用,您必须更改它contains
。
这里有完整的例子。
public class AutoCompleteAdapter extends ArrayAdapter<String> implements Filterable {
private ArrayList<String> fullList;
private ArrayList<String> mOriginalValues;
private ArrayFilter mFilter;
private MainActivity mActivity;
public AutoCompleteAdapter(MainActivity activity, int resource, List<String> objects) {
super(activity, resource, objects);
fullList = (ArrayList<String>) objects;
mOriginalValues = new ArrayList<String>(fullList);
mActivity = activity;
}
@Override
public int getCount() {
return fullList.size();
}
@Override
public String getItem(int position) {
return fullList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return super.getView(position, convertView, parent);
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView v = (TextView) super.getView(position, convertView, parent);
if (v == null) {
v = new TextView(mActivity);
}
v.setText(fullList.get(position));
v.setTextColor(mActivity.getResources().getColor(R.color.textColor));
v.setTextSize(mActivity.getResources().getDimension(R.dimen.my_orders_spinner_text_size));
v.setPadding(
(int)mActivity.getResources().getDimension(R.dimen.my_orders_spinner_text_padding),
(int)mActivity.getResources().getDimension(R.dimen.my_orders_spinner_text_padding),
(int)mActivity.getResources().getDimension(R.dimen.my_orders_spinner_text_padding),
(int)mActivity.getResources().getDimension(R.dimen.my_orders_spinner_text_padding));
return v;
}
private class ArrayFilter extends Filter {
private Object lock;
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (lock) {
mOriginalValues = new ArrayList<String>(fullList);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (lock) {
ArrayList<String> list = new ArrayList<String>(mOriginalValues);
results.values = list;
results.count = list.size();
}
} else {
final String prefixString = prefix.toString().toLowerCase();
ArrayList<String> values = mOriginalValues;
int count = values.size();
ArrayList<String> newValues = new ArrayList<String>(count);
for (int i = 0; i < count; i++) {
String item = values.get(i);
if (item.toLowerCase().contains(prefixString)) {
newValues.add(item);
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if(results.values!=null){
fullList = (ArrayList<String>) results.values;
}else{
fullList = new ArrayList<String>();
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
}
}
答案 1 :(得分:0)
实施
public class AutoCompleteString extends Activity implements OnItemClickListener,
OnItemSelectedListener {
// Initialize variables
AutoCompleteTextView textView=null;
private ArrayAdapter<String> adapter;
//These values show in autocomplete
String item[]={
"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"
};
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.auto_complete_string);
// Initialize AutoCompleteTextView values
// Get AutoCompleteTextView reference from xml
textView = (AutoCompleteTextView) findViewById(R.id.Months);
//Create adapter
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, item);
textView.setThreshold(1);
//Set adapter to AutoCompleteTextView
textView.setAdapter(adapter);
}
}
答案 2 :(得分:0)
我尝试保持我的代码跨平台兼容,所以我通常使用基于ObservableCollection的适配器,在Android中它实现了IListAdapter和ISpinnerAdapter。这是为了避免大型数据集的性能问题。这里有一个基本功能的演示代码,只需使用EditText和一个基本的ArrayAdapter:
[Activity (Label = "SearchBox", MainLauncher = true)]
public class MainActivity : Activity
{
private readonly string[] countries = { "1abcd", "2abdc", "1234", "Item42abcd", "Item5" };
protected override void OnCreate(Bundle bundle)
{
base.OnCreate (bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
var editText = this.FindViewById<EditText> (Resource.Id.editText1);
var listView = this.FindViewById<ListView> (Resource.Id.listView1);
listView.Adapter = new ArrayAdapter (this, Resource.Layout.ListItem, countries);
editText.TextChanged += (sender, e) => listView.Adapter =
new ArrayAdapter (
this,
Resource.Layout.ListItem,
countries.Where (a => a.Contains (editText.Text)).ToArray ());
}
}