Aditya Vyas - Lakhan Aditya Vyas - Lakhan - 1 year ago 142
JSON Question

How to get autocompletetextview id?

I follow this example to use autocmpletetextview in my project,i want to get id when user select any item,can anyone tell how to get id..

following is json if click on ab then i want to get 1,if i click on abc i want to get 2..


public class MainActivity extends Activity {
private AutoCompleteTextView acTextView;
private String idtest;

protected void onCreate(Bundle savedInstanceState) {
acTextView = (AutoCompleteTextView) findViewById(;

final SuggestionAdapter adapter=new SuggestionAdapter(this, acTextView.getText().toString());

acTextView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
JsonParse jps=new JsonParse();

/* List<SuggestGetSet> list =jps.getParseJsonWCF(acTextView.getText().toString());

for(int i = 0;i<list.size();i++)



SuggestGetSet selectedSuggestGetSet =

Toast.makeText(getApplicationContext(), selectedSuggestGetSet+acTextView.getText().toString(), Toast.LENGTH_SHORT).show();



public class SuggestionAdapter extends ArrayAdapter<String> {

protected static final String TAG = "SuggestionAdapter";
public List<String> suggestions;
private List<SuggestGetSet> new_suggestions;

public SuggestionAdapter(Activity context, String nameFilter) {
super(context, android.R.layout.simple_dropdown_item_1line);
suggestions = new ArrayList<String>();

public int getCount() {
return suggestions.size();

public String getItem(int index) {
return suggestions.get(index);

public Filter getFilter() {
Filter myFilter = new Filter() {

protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
JsonParse jp=new JsonParse();
if (constraint != null) {
// A class that queries a web API, parses the data and
// returns an ArrayList<GoEuroGetSet>
new_suggestions =jp.getParseJsonWCF(constraint.toString());
for (int i=0;i<new_suggestions.size();i++) {


// Now assign the values and count to the FilterResults
// object
filterResults.values = suggestions;
filterResults.count = suggestions.size();
return filterResults;

protected void publishResults(CharSequence contraint,
FilterResults results) {
if (results != null && results.count > 0) {
} else {
return myFilter;

public List<SuggestGetSet> getAllUpdatedSuggestion(){
return this.new_suggestions;



Answer Source

Put one method in adapter

public Int getItemId(int index) {
    return suggestions.get(index).getId();
Then access it in onItemClick. You will getId from there by adapter.getItemId(position);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download