0
我試圖通過從列表形式的服務器獲取國家列表來填充autocompletetextview
。 我已經能夠使用customAdapter
填充autocompletetextview
。在AutoCompleteTextView的CustomAdapter中訪問所選項目的值
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
在列表中顯示:不過,我已經很難找到一種方法來訪問對象,如id
選定項目的其他價值,並defaultCurrencyId
我的國家目標。我需要獲得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();
}
}
};
}
在我活動:
public void setCountryAdapter(ArrayList<Country> country){
CountryAdapter countryAdapter = new CountryAdapter(getActivity(), R.layout.list_view_row, country);
_actvCountry.setAdapter(countryAdapter);
}
我試圖使用OnItemSelectedListener
這給選擇項目的位置。但我想知道是否可以訪問Country Object中的其他變量。因爲我爲此調整了CustomAdapter。
請讓我知道,如果你需要更詳細的案件。
謝謝。
謝謝你的確切解決方案。 – Poorya