从 API 调用时 AutoCompleteTextView 中的对象设置

Object setting in AutoCompleteTextView when called from API

我正在调用一个 API,我从 2 个参数 Id 和 ItemName 接收数据。

但是,当我尝试将名称设置为 AutoCompleteTextView 时,对象的整个地址设置在 AutoCompleteTextView 的下拉列表中,如下所示:

我只想设置名称而不是对象。

我的MainActivity如下:

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();

    }

我的 CustomAdapter class 会是:

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;
    }

}

还有我的JSONParserclass:

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;

    }

}

非常感谢任何帮助

您应该在您的模型中输入此方法。这样它只为您提供名称。

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

要了解有关 toString() 方法的更多信息,请参阅此 link。

toString() in a model or view layer?