在 AutoCompleteTextView 的 CustomAdapter 中访问所选项目的值
accessing values of selected item in AutoCompleteTextView's CustomAdapter
我试图通过以列表形式从服务器获取国家/地区列表来填充 autocompletetextview
。
我已经能够用 customAdapter
填充 autocompletetextview
。但是,我很难找到一种方法来访问对象中所选项目的其他值,例如 id
和 defaultCurrencyId
我的国家对象:
public class Country {
private Integer id;
private String name;
private Integer defaultCurrencyId;
private String phonePrefix;
private Integer status;
private Object deletedAt;
private String createdAt;
private String updatedAt;
... standard getter and setter ...
}
我正在使用 name
显示在列表中。我需要获取所选 name
.
的 id
我的自定义 CountryAdapter:
public class CountryAdapter extends ArrayAdapter<Country> {
private final String MY_DEBUG_TAG = "CountryAdapter";
private ArrayList<Country> items;
private ArrayList<Country> itemsAll;
private ArrayList<Country> suggestions;
private int viewResourceId;
public CountryAdapter(Context context, int viewResourceId, ArrayList<Country> items) {
super(context, viewResourceId, items);
this.items = items;
this.itemsAll = (ArrayList<Country>) items.clone();
this.suggestions = new ArrayList<Country>();
this.viewResourceId = viewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(viewResourceId, null);
}
Country country = items.get(position);
if (country != null) {
TextView countryNameLabel = (TextView) v.findViewById(R.id.textViewItem);
if (countryNameLabel != null) {
// Log.i(MY_DEBUG_TAG, "getView Country Name:"+country.getName());
countryNameLabel.setText(country.getName());
}
}
return v;
}
@Override
public Filter getFilter() {
return nameFilter;
}
Filter nameFilter = new Filter() {
@Override
public String convertResultToString(Object resultValue) {
String str = ((Country)(resultValue)).getName();
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null) {
suggestions.clear();
for (Country country : itemsAll) {
if(country.getName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
suggestions.add(country);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Country> filteredList = (ArrayList<Country>) results.values;
if(results != null && results.count > 0) {
clear();
for (Country c : filteredList) {
add(c);
}
notifyDataSetChanged();
}
}
};
}
在我的 Activity:
public void setCountryAdapter(ArrayList<Country> country){
CountryAdapter countryAdapter = new CountryAdapter(getActivity(), R.layout.list_view_row, country);
_actvCountry.setAdapter(countryAdapter);
}
我尝试使用 OnItemSelectedListener
来给出所选项目的位置。但我想知道是否可以访问 Country 对象中的其他变量。因为我为此目的调整了 CustomAdapter。
如果您需要对该案例进行更多详细说明,请告诉我。
谢谢。
使用:setOnItemClickListener
in autoCompleteTextView
并检索自定义对象。卢克:
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
Country country = (Country) parent.getItemAtPosition(position);
String id = country.getId();
//other information
}
});
我试图通过以列表形式从服务器获取国家/地区列表来填充 autocompletetextview
。
我已经能够用 customAdapter
填充 autocompletetextview
。但是,我很难找到一种方法来访问对象中所选项目的其他值,例如 id
和 defaultCurrencyId
我的国家对象:
public class Country {
private Integer id;
private String name;
private Integer defaultCurrencyId;
private String phonePrefix;
private Integer status;
private Object deletedAt;
private String createdAt;
private String updatedAt;
... standard getter and setter ...
}
我正在使用 name
显示在列表中。我需要获取所选 name
.
id
我的自定义 CountryAdapter:
public class CountryAdapter extends ArrayAdapter<Country> {
private final String MY_DEBUG_TAG = "CountryAdapter";
private ArrayList<Country> items;
private ArrayList<Country> itemsAll;
private ArrayList<Country> suggestions;
private int viewResourceId;
public CountryAdapter(Context context, int viewResourceId, ArrayList<Country> items) {
super(context, viewResourceId, items);
this.items = items;
this.itemsAll = (ArrayList<Country>) items.clone();
this.suggestions = new ArrayList<Country>();
this.viewResourceId = viewResourceId;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(viewResourceId, null);
}
Country country = items.get(position);
if (country != null) {
TextView countryNameLabel = (TextView) v.findViewById(R.id.textViewItem);
if (countryNameLabel != null) {
// Log.i(MY_DEBUG_TAG, "getView Country Name:"+country.getName());
countryNameLabel.setText(country.getName());
}
}
return v;
}
@Override
public Filter getFilter() {
return nameFilter;
}
Filter nameFilter = new Filter() {
@Override
public String convertResultToString(Object resultValue) {
String str = ((Country)(resultValue)).getName();
return str;
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
if(constraint != null) {
suggestions.clear();
for (Country country : itemsAll) {
if(country.getName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
suggestions.add(country);
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;
} else {
return new FilterResults();
}
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
ArrayList<Country> filteredList = (ArrayList<Country>) results.values;
if(results != null && results.count > 0) {
clear();
for (Country c : filteredList) {
add(c);
}
notifyDataSetChanged();
}
}
};
}
在我的 Activity:
public void setCountryAdapter(ArrayList<Country> country){
CountryAdapter countryAdapter = new CountryAdapter(getActivity(), R.layout.list_view_row, country);
_actvCountry.setAdapter(countryAdapter);
}
我尝试使用 OnItemSelectedListener
来给出所选项目的位置。但我想知道是否可以访问 Country 对象中的其他变量。因为我为此目的调整了 CustomAdapter。
如果您需要对该案例进行更多详细说明,请告诉我。
谢谢。
使用:setOnItemClickListener
in autoCompleteTextView
并检索自定义对象。卢克:
autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
Country country = (Country) parent.getItemAtPosition(position);
String id = country.getId();
//other information
}
});