Search code examples
androidjsonapiautocompletetextview

Object setting in AutoCompleteTextView when called from API


I am calling an API where I receive data from 2 parameters Id and ItemName.

However, when I am trying to set the Name to the AutoCompleteTextView, the whole address of the object sets in the dropdown list of the AutoCompleteTextView like this:

enter image description here

I just want to set the name instead of the object.

My MainActivity is as follows:

nameAutoText = (AutoCompleteTextView) view.findViewById(R.id.et_Name);
        nameAutoText.setAdapter(new SuggestionAdapter(getActivity(),nameAutoText.getText().toString()));


nameAutoText.setOnItemClickListener(this);


@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
        DropdownVal dropdownVal = (DropdownVal) nameAutoText.getAdapter().getItem(position);
        String friends_id = dropdownVal.getId();
        Toast.makeText(getActivity(),friends_id,Toast.LENGTH_LONG).show();

    }

My CustomAdapter class would be:

class SuggestionAdapter extends ArrayAdapter<DropdownVal> {

    protected static final String TAG = "SuggestionAdapter";
    private List<DropdownVal> suggestions;
    public SuggestionAdapter(Activity context, String nameFilter) {
        super(context, android.R.layout.simple_dropdown_item_1line);
        suggestions = new ArrayList<DropdownVal>();
    }

    @Override
    public int getCount() {
        return suggestions.size();
    }

    @Override
    public DropdownVal 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) { 
                    List<SuggestGetSet> new_suggestions =jp.getParseJsonWCF(constraint.toString());
                    suggestions.clear();
                    for (int i=0;i<new_suggestions.size();i++) {
                        DropdownVal dropdownVal=new DropdownVal(new_suggestions.get(i).getId(),new_suggestions.get(i).getName());
                        suggestions.add(dropdownVal);
                    } 
                    filterResults.values = suggestions;
                    filterResults.count = suggestions.size();
                    notifyDataSetChanged();
                }
                return filterResults;
            }

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

}

And my JSONParser class:

class JsonParse {


    public JsonParse() {
    }


    public List<SuggestGetSet> getParseJsonWCF(String sName) {
        List<SuggestGetSet> ListData = new ArrayList<SuggestGetSet>();
        try {
        String temp = sName.replace(" ", "%20");
        URL js = new URL(API_URL + temp);
        URLConnection jc = js.openConnection();
        BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));
        String line = reader.readLine();
        //JSONObject jsonResponse = new JSONObject(line);
        //JSONArray jsonArray = jsonResponse.getJSONArray("results");
        JSONArray jsonArray = new JSONArray(line);
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject r = jsonArray.getJSONObject(i);

            ListData.add(new SuggestGetSet(r.getString("ItemMasterId"), r.getString("ItemName")));
        }
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
        return ListData;

    }

}

Any help is highly appreciated


Solution

  • You should enter this method in your model. So that it provides you only the name.

      @Override
    public String toString() {
        return Name ;
    }
    

    To learn more about toString() method Please see this link.

    toString() in a model or view layer?