我正在使用一个应用程序,我正在使用AutoCompleteTextView并且面临一些问题。请查看以下问题的详细信息。
数据中包含以下值:
1)Manish Logan Jain
2)M. J.(Logan Fern)
3)洛根
的问题:
1)当用户搜索Manish时,Manish Logan Jain会显示为建议。但是当用户输入Logan Jain时,则不会返回任何结果。
2)当用户输入Logan时,我希望第二个值显示为建议,但是目前,建议列表没有显示任何内容。
3)当用户输入ogan时,我希望显示建议3。目前,它没有显示。
AutoCompleteView xml:
AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/enter_user_name" >
<requestFocus />
</AutoCompleteTextView>
填充数据的Java代码:
List<String> namesList = new ArrayList<String>(stops);
namesList.add("Manish Logan Jain");
namesList.add("Logan");
namesList.add("M. J. (Logan Fern)");
ArrayAdapter<String> namesSuggestion = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, namesList);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
textView.setAdapter(namesSuggestion);
textView.setThreshold(1);
有人遇到类似的问题吗?如果是,那么可能的解决方案是什么?
答案 0 :(得分:2)
使用实现Filterable的自定义适配器。在getFilter方法中,根据您的要求使用String.contains()。
请检查this link.
答案 1 :(得分:2)
为您的ACTV使用CursorAdapter并调用setFilterQueryProvider(FilterQueryProvider)进行自定义过滤(使用MatrixCursor过滤数据)
编辑:示例FilterQueryProvider
class FQP extends LinkedList<String> implements FilterQueryProvider {
@Override
public Cursor runQuery(CharSequence constraint) {
if (constraint == null) {
return null;
}
Log.d("TAG", "runQuery " + constraint);
String lowerConstraint = constraint.toString().toLowerCase();
String[] columns = {
"_id", "name"
};
int id = 0;
MatrixCursor c = new MatrixCursor(columns);
for (String name : this) {
String lowerName = name.toLowerCase();
if (lowerName.indexOf(lowerConstraint) != -1) {
c.newRow().add(id++).add(name);
}
}
return c;
}
};
使用onCreate中的以下内容对其进行测试:
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
AutoCompleteTextView actv = new AutoCompleteTextView(this);
String[] from = {"name"};
int[] to = {android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to);
FQP fqp = new FQP();
fqp.add("Manish Logan Jain");
fqp.add("Logan");
fqp.add("M. J. (Logan Fern)");
adapter.setFilterQueryProvider(fqp);
actv.setAdapter(adapter);
actv.setThreshold(1);
ll.addView(actv);
setContentView(ll);
答案 2 :(得分:0)
你在寻找什么是不能直接完成的。但我建议你使用MultiCompleteTextView。
答案 3 :(得分:0)
试用以下代码:
public class AutoCompleteAdapter extends ArrayAdapter<Address> implements Filterable {
private LayoutInflater mInflater;
private Geocoder mGeocoder;
private StringBuilder mSb = new StringBuilder();
public AutoCompleteAdapter(final Context context) {
super(context, -1);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mGeocoder = new Geocoder(context);
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final TextView tv;
if (convertView != null) {
tv = (TextView) convertView;
} else {
tv = (TextView) mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
}
tv.setText((createFormattedAddressFromAddress(getItem(position))));
return tv;
}
private String createFormattedAddressFromAddress(final Address address) {
mSb.setLength(0);
final int addressLineSize = address.getMaxAddressLineIndex();
for (int i = 0; i < addressLineSize; i++) {
mSb.append(address.getAddressLine(i));
if (i != addressLineSize - 1) {
mSb.append(", ");
}
}
return mSb.toString();
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(final CharSequence constraint) {
List<Address> addressList = null;
if (constraint != null) {
try {
addressList = mGeocoder.getFromLocationName((String) constraint, 5,23.0,72.0,23.9,72.9);
} catch (IOException e) {
}
}
if (addressList == null) {
addressList = new ArrayList<Address>();
}
final FilterResults filterResults = new FilterResults();
filterResults.values = addressList;
filterResults.count = addressList.size();
return filterResults;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(final CharSequence contraint, final FilterResults results) {
clear();
for (Address address : (List<Address>) results.values) {
add(address);
}
if (results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
@Override
public CharSequence convertResultToString(final Object resultValue) {
return resultValue == null ? "" : ((createFormattedAddressFromAddress((Address) resultValue).split(", Ahmedabad")[0].length()<6)?createFormattedAddressFromAddress((Address) resultValue).split(", Gujarat")[0]:createFormattedAddressFromAddress((Address) resultValue).split(", Ahmedabad")[0]);
}
};
return myFilter;
}
}
答案 4 :(得分:0)
通过将输入字符串视为前缀来返回默认结果字符串集。这是在ArrayFilter对象中实现的。代码就像
/**
* <p>An array filter constrains the content of the array adapter with
* a prefix. Each item that does not start with the supplied prefix
* is removed from the list.</p>
*/
private class ArrayFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();
if (mOriginalValues == null) {
synchronized (mLock) {
mOriginalValues = new ArrayList<T>(mObjects);
}
}
if (prefix == null || prefix.length() == 0) {
synchronized (mLock) {
ArrayList<T> list = new ArrayList<T>(mOriginalValues);
results.values = list;
results.count = list.size();
}
} else {
String prefixString = prefix.toString().toLowerCase();
final ArrayList<T> values = mOriginalValues;
final int count = values.size();
final ArrayList<T> newValues = new ArrayList<T>(count);
for (int i = 0; i < count; i++) {
final T value = values.get(i);
final String valueText = value.toString().toLowerCase();
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString)) {
newValues.add(value);
} else {
final String[] words = valueText.split(" ");
final int wordCount = words.length;
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {
newValues.add(value);
break;
}
}
}
}
results.values = newValues;
results.count = newValues.size();
}
return results;
}
如果要将返回字符串更改为包含输入字符串的集合,则需要A customed ArrayFilter。