Good time of day! I want to implement an AutoCompeleteView with a hint in the form of a drop-down list. I take the data using JSON. Here is the link. It seems everything works, but when I click on the list I want to get another id and currency from json. How do I implement a Class Adapter for this?

public class SuggestionAdapter extends ArrayAdapter<String> { public String LOG_TAG = "my_log"; private List<String> suggestions; private List<String> idsuggestions;// public SuggestionAdapter(Context context, String filter) { super(context, R.layout.dropdown_item,R.id.textView11);//android.R.layout.simple_dropdown_item_1line suggestions = new ArrayList<String>(); idsuggestions = new ArrayList<String>();// } @Override public int getCount() { return suggestions.size(); } @Override public String getItem(int index) { return suggestions.get(index); } @Override public Filter getFilter() { Filter myFilter = new Filter() { @Override 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> List<SuggestGetSet> new_suggestions =jp.getParseJsonWCF(constraint.toString()); suggestions.clear(); idsuggestions.clear();// for (int i=0;i<new_suggestions.size();i++) { suggestions.add(new_suggestions.get(i).getName()); idsuggestions.add(new_suggestions.get(i).getId());// } // Now assign the values and count to the FilterResults // object filterResults.values = idsuggestions;// filterResults.values = suggestions; filterResults.count = suggestions.size(); //Log.d(LOG_TAG, "id: " + idsuggestions);// Log.d(LOG_TAG, "name: " + suggestions);// } return filterResults; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { if (results != null && results.count > 0) { notifyDataSetChanged(); } else { notifyDataSetInvalidated(); } } }; return myFilter; } } 
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Make an adapter of type ArrayAdapter<SuggestGetSet> . In the constructor, pass the ready list, and in the method

 getView(int position, View convertView, ViewGroup parent) 

print the name like this

 ((TextView)view).setText(list.getItem(position).getName) 

It should look something like this.

  public class SuggestionAdapter extends ArrayAdapter<SuggestGetSet> { private int viewResourceId; public SuggestionAdapter(Context context, int textViewResourceId, List<SuggestGetSet> objects) { super(context, textViewResourceId, objects); viewResourceId = textViewResourceId; } @Override public int getCount() { return suggestions.size(); } @Override 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); } SuggestGetSet item = this.getItem(position); if (item != null) { TextView textView = (TextView) v; textView.setText(item.getName()); } return v; } } 

Then hang handler

 private OnItemClickListener listener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { SuggestGetSet item = (SuggestGetSet)parent.getAdapter().getItem(position); //some work } }; 

    There are two ways:

    1. Hang onClickListener on the list. It will have the position of the pressed element. Using it, you can get data from the adapter using the getItem list method (int position)
    2. Override the getView adapter method and in it hang the onClickListener on the View element. The position is also passed to the getView method, which allows you to get data from the adapter using the getItem (int position) list method.
    • Thank. I understood everything .. - Ilfar_sif