我做了一个自动建议的演示。我有名称(字符串)和代码(字符串)。我的代码大约有2000个名字。所以我采用字符串数组并将其格式化为 name-(code)示例" Alexandra Palace-(AAP)" ,我的问题是我需要使用代码而不是按名称进行过滤。实际上,当我输入输入字段时,它与名字不带代码。但我需要用代码过滤。
示例 当我输入" lwy"它不会显示" MNCRLWY-(LWY)" ,您能告诉我将如何实现这一目标吗?
我试试这个
public class GlobalList {
public static String[] stationList={
"MNCRLWY-(LWY)",
"Lympstone Commando-(LYC)",
"Lydney-(LYD)",
"Lye-(LYE)",
"Lympstone Village-(LYM)",
"Lymington Pier-(LYP)",
"Lymington Town-(LYT)",
"Lazonby & Kirkoswald-(LZB)",
"Leeds, Whitehall (Bus)-(LZZ)",
"Macclesfield-(MAC)",
"Maghull-(MAG)",
"Maidenhead-(MAI)",
"Malden Manor-(MAL)",
"Manchester Piccadilly-(MAN)",
"Martins Heron-(MAO)",
"Margate-(MAR)",
"Manors-(MAS)",
"Matlock-(MAT)",
"Mauldeth Road-(MAU)",
"Mallow-(MAW)",
"Maxwell Park-(MAX)",
"Maybole-(MAY)",
"Millbrook (Hampshire)-(MBK)",
"Middlesbrough-(MBR)",
"Moulsecoomb-(MCB)",
"Metro Centre-(MCE)",
"March-(MCH)",
"Marne La Vallee-(MCK)",
"Morecambe-(MCM)",
"Machynlleth-(MCN)",
"Manchester Oxford Road-(MCO)",
"Manchester Victoria-(MCV)",
"Maidstone Barracks-(MDB)",
"Maidstone East-(MDE)",
"Midgham-(MDG)",
"Middlewood-(MDL)",
"Maiden Newton-(MDN)",
"Morden South-(MDS)",
"Maidstone West-(MDW)",
"MAERDY-(MDY)",
"Meols Cop-(MEC)",
"Meldreth-(MEL)",
"Menheniot-(MEN)",
"Meols-(MEO)",
"Meopham-(MEP)",
"Merthyr Tydfil-(MER)",
"Melton-(MES)",
"Merthyr Vale-(MEV)",
"Maesteg (Ewenny Road)-(MEW)",
"Mexborough-(MEX)",
"Merryton-(MEY)",
"Morfa Mawddach-(MFA)",
"Minffordd-(MFD)",
"Minffordd-(MFF)",
"Milford Haven-(MFH)",
};
}
CustomAdapter :
public class CustomAutocompletAdapter extends BaseAdapter implements Filterable{
private String stationNameAndCodeValue ;
ArrayList<String> autolistArray;
ArrayList<String> objects;
private Context context;
public CustomAutocompletAdapter( Context context, String[] autolistArray){
this.autolistArray=new ArrayList<String>();
for(int i=0;i<autolistArray.length;i++){
this.autolistArray.add(autolistArray[i]);
} this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return autolistArray.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return autolistArray.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = convertView;
if (v == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = mInflater.inflate(R.layout.custom_row_adapter, null);
}
final TextView stationNameAndCode = (TextView) v
.findViewById(R.id.item_selectStationName);
stationNameAndCodeValue = autolistArray.get(position);
stationNameAndCode.setText(stationNameAndCodeValue);
return v;
}
@Override
public Filter getFilter() {
// TODO Auto-generated method stub
Filter myFilter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
System.out.println("Constraint " + constraint);
Log.d("-----------", "publishResults");
if (results.count > 0 && results != null) {
objects = (ArrayList<String>) results.values;
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("-----------", "performFiltering");
FilterResults results = new FilterResults();
List<String> FilteredArrList = new ArrayList<String>();
if (objects == null) {
objects = new ArrayList<String>(autolistArray); // saves
}
Locale locale = Locale.getDefault();
constraint = (String) constraint
.toString().toLowerCase(locale);
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = objects.size();
results.values = objects;
} else {
for (int i = 0; i < objects.size(); i++) {
String name= objects.get(i);
String newName = name.substring(name.indexOf('('),name.length()-1);
if (newName.toLowerCase(locale).contains(constraint))
{
FilteredArrList.add(name);
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
}
return results;
}
@Override
public CharSequence convertResultToString(Object resultValue) {
// TODO Auto-generated method stub
//convert object to string
Log.d("-----------", "convertResultToString");
return "";
}
};
return myFilter;
}
}
主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_station);
autocompleteView = (AutoCompleteTextView) findViewById(R.id.item_autoComplete);
STATION_LIST = new String[GlobalList.stationList.length
+ GlobalExtendStationList.stationList.length];
System.arraycopy(GlobalList.stationList, 0, STATION_LIST, 0,
GlobalList.stationList.length);
System.arraycopy(GlobalExtendStationList.stationList, 0,
STATION_LIST, GlobalList.stationList.length,
GlobalExtendStationList.stationList.length);
autosuggestAdapter = new CustomAutocompletAdapter(this,STATION_LIST);
autocompleteView.setAdapter(autosuggestAdapter);
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity=""
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose station"
android:layout_marginLeft="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<AutoCompleteTextView
android:id="@+id/item_autoComplete"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:ems="10"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="AutoCompleteTextView" >
<requestFocus />
</AutoCompleteTextView>
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
当您检查整个字符串(名称)中的搜索字符串时,如果它在名称中的任何位置找到了serching字符串,则它会添加到resut中。
所以使用这个
String newName = name.subString(indexOf('('),name.lastIndexOf(')'));
if (newName.toLowerCase(locale).contains(constraint))
{
FilteredArrList.add(name);
}
而不是
if (name.toLowerCase(locale).contains(constraint))
{
FilteredArrList.add(name);
}