你能告诉我如何使用基础适配器在android中制作自定义过滤器吗? 我做了一个简单的自动完成演示。我拿了2250个字符串,其中包含名称和代码,例如" Alexandra Palace-(AAP)",名字是给定和它在括号内的代码。我的问题是我需要使用代码而不是按名称来过滤它。换句话说,如果我在输入字段中输入任何内容,它会过滤元素均值名称的起始字符我需要使用代码进行过滤在支架内。
代码:
package com.firstgroup.global;
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)",
"Mount Florida-(MFL)",
"Mansfield-(MFT)",
"Metheringham-(MGM)",
"Marston Green-(MGN)",
"Minehead-(MHD)",
"Merstham-(MHM)",
"Market Harborough-(MHR)",
"Meadowhall-(MHS)",
"Manchester Airport-(MIA)",
"Micheldever-(MIC)",
"Millfield (T & W Metro)-(MIF)",
"Mills Hill-(MIH)",
"Mitcham Junction-(MIJ)",
"Micklefield-(MIK)",
"Mill Hill Broadway-(MIL)",
"Moreton-in-Marsh-(MIM)",
"Milliken Park-(MIN)",
"Mirfield-(MIR)",
"Mistley-(MIS)",
"Milton Keynes Central-(MKC)",
"Melksham-(MKM)",
"Market Rasen-(MKR)",
"Marks Tey-(MKT)",
"Millbrook (Bedfordshire)-(MLB)",
"Mouldsworth-(MLD)",
"Milford (Surrey)-(MLF)",
"Mallaig-(MLG)",
"Mill Hill (Lancashire)-(MLH)",
"Millom-(MLM)",
"Milngavie-(MLN)",
"MILESPL-(MLP)",
"Milnrow-(MLR)",
"Melrose (Bus)-(MLS)",
"Malton-(MLT)",
"Marlow-(MLW)",
"Morley-(MLY)",
"Melton Mowbray-(MMO)",
"Markinch-(MNC)",
"Manea-(MNE)",
"Manningtree-(MNG)",
"Menston-(MNN)",
"Manor Park-(MNP)",
"Manor Road-(MNR)",
"MINZHBF-(MNZ)",
"Mobberley-(MOB)",
"Moorgate-(MOG)",
"Monifieth-(MON)",
};
}
当我输入" lwy" 时,它不会显示" MNCRLWY-(LWY)" ,您能否告诉我我将如何实现这一目标?
这是我的代码..
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);
customAutosuggestAdapter:
public class CustomAutocompletAdapter extends BaseAdapter implements Filterable{
String[] autolistArray;
private Context context;
public CustomAutocompletAdapter( Context context, String[] autolistArray){
this.autolistArray=autolistArray;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@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);
final String stationNameAndCodeValue = autolistArray[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");
// has
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("-----------", "performFiltering");
FilterResults results = new FilterResults(); // Holds the
// results of a
// filtering
// operation in
// values
/********
*
* If constraint(CharSequence that is received) is null returns
* the mOriginalValues(Original) values else does the Filtering
* and returns FilteredArrList(Filtered)
*
********/
Locale locale = Locale.getDefault();
constraint = (String) constraint
.toString().toLowerCase(locale);
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
} else {
}
return results;
}
@Override
public CharSequence convertResultToString(Object resultValue) {
// TODO Auto-generated method stub
//convert object to string
Log.d("-----------", "convertResultToString");
return "";
}
};
return myFilter;
}
}
没有显示自动建议。你能告诉我怎么做到这一点吗?平均过滤功能? 我想在输入字段时输入过滤器方法,它从代码中过滤而不是从名称...
答案 0 :(得分:1)
像这样更改您的适配器
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() {
return autolistArray.size();
}
@Override
public Object getItem(int position) {
return autolistArray.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
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;
}
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) {
autolistArray = (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);
}
Locale locale = Locale.getDefault();
if (constraint == null || constraint.length() == 0) {
// set the Original result to return
results.count = objects.size();
results.values = objects;
} else {
constraint = (String) constraint.toString().toLowerCase(locale);
Pattern logEntry = Pattern.compile("-\\((.*?)\\)");
for (int i = 0; i < objects.size(); i++) {
String name = objects.get(i);
// System.out.println(name);
Matcher matchPattern = logEntry.matcher(name);
String subText = "";
while (matchPattern.find()) {
subText = matchPattern.group(1);
}
if (subText.toLowerCase(locale).contains(constraint)) {
filteredArrList.add(name);
}
}
System.out.println(filteredArrList);
// set the Filtered result to return
results.count = filteredArrList.size();
results.values = filteredArrList;
}
return results;
}
};
@Override
public Filter getFilter() {
return myFilter;
}
}