我做自动完成的一个简单的演示。我采取的字符串,其中有2250项与数组的名称和code 这样的例子亚历山德拉Palace-(AAP),的名字,并给出了它$的bracket.my问题内部C $ c是我需要的名字。在换句话说,如果我输入任何输入字段它元素的起始字符过滤器过滤该用code并不意味着名字我需要筛选code这是括号内。
I make a simple demo of auto complete. I take array of string in which there is 2250 entries with Name and code like that example "Alexandra Palace-(AAP)", first name is given and the it code inside the bracket.my issue is I need to filter this using code not by name .In other word if i type anything in input field it filter with starting characters of element mean name I need to filter with code which is inside the bracket.
code:
当我键入LWY它不会显示的MNCRLWY-(LWY),您可以请告诉我如何做到这一点?
when I type "lwy" it will not show "MNCRLWY-(LWY)", can you please tell how I will achieve this ?
这是我的code ..
here is my code..
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;
}
}
未显示,没有自动提示。你能告诉我如何实现这一目标?均值滤波功能? 这个问题的任何更新
It is not showing no auto suggest. Could you please tell how I will achieve this? Mean filter functionality? any update of this Question
试试这个,
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);
if (name.toLowerCase(locale).contains(constraint)
) {
FilteredArrList.add(model);
}
}
// 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;
}
}