I am attempting to populate a autocompletetextview
by getting a list of countries from server in form of list.
I have been able to populate the autocompletetextview
with customAdapter
. However,I have had difficulty finding a way to access other values of the selected item in object such as id
, and defaultCurrencyId
my Country object:
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 ...
}
I am using name
to show in the list. and i need to get the id
of the selected name
.
my Custom 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();
}
}
};
}
In my Activity:
public void setCountryAdapter(ArrayList<Country> country){
CountryAdapter countryAdapter = new CountryAdapter(getActivity(), R.layout.list_view_row, country);
_actvCountry.setAdapter(countryAdapter);
}
I attempted to use OnItemSelectedListener
which gives the position of the selected item. but i was wondering if it is possible to access other variables in Country Object. as I adapted the CustomAdapter for this purpose.
Please let me know if you need more elaboration on the case.
Thanks.
use: setOnItemClickListener
in autoCompleteTextView
and retreive the custom object. Like:
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
}
});