AutoCompleteTextView - 我正在使用autocompletetextview,它显示来自API的值(返回名称和代码)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AutoCompleteTextView - 我正在使用autocompletetextview,它显示来自API的值(返回名称和代码)相关的知识,希望对你有一定的参考价值。

我使用模型类来获取响应并从中获取值。

从AutoComplete下拉列表中选择值(名称)时,我想获取ContactId(代码)?

我的代码

在OnCreate

edtName.setAdapter(new AutoCustomAdapter());

AdapterClass

private class AutoCustomAdapter extends ArrayAdapter<String> {

        public AutoCustomAdapter() {
            super(context, android.R.layout.simple_dropdown_item_1line);
            suggestions = 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<AutoCustomerModel> new_suggestions = jp.getParseJsonWCF(constraint.toString());
                        suggestions.clear();
                        for (int i=0;i<new_suggestions.size();i++) {
                            suggestions.add(new_suggestions.get(i).getName());

                            strClientId = new_suggestions.get(i).getContactId();
                            Log.e("SUGGEST",""+suggestions);
                        }

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

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

JsonParse()

公共类JsonParse {

public JsonParse(){}

public List<AutoCustomerModel> getParseJsonWCF(String sName)
{
    List<AutoCustomerModel> ListData = new ArrayList<AutoCustomerModel>();
    try {
        String temp=sName.replace(" ", "%20");
        URL js = new URL("https://xxxxxxxxxURLxxxxxxx"+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(Constant.TAG_contactList);
        for(int i = 0; i < jsonArray.length(); i++){
            JSONObject custoList = jsonArray.getJSONObject(i);
            ListData.add(new AutoCustomerModel(custoList.getString("ContactId"),custoList.getString("Name")));
        }
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return ListData;
}

}

通过这个我开始输入时在下拉列表中获得名称。

模特课

public class AutoCustomerModel {

    String ContactId, Name;

    public AutoCustomerModel(String contactId, String name) {
        ContactId = contactId;
        Name = name;
    }

    public String getContactId() {
        return ContactId;
    }

    public void setContactId(String contactId) {
        ContactId = contactId;
    }

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }
}

我想要的是什么?

我想在从Drop列表中选择Name时获取Code

答案

只要您的AutoCompleteTextView项目是AutoCustomerModel,您扩展的适配器可以替换为ArrayAdapter<AutoCustomerModel>。做了必要的修改后,你可以通过adapter.getItem(position).getContactId()获得contactId

List<String>替换List<AutoCustomerModel>。因此,无论何时您需要任何项目的任何字段,都可以使用suggestions列表。 (suggestions.get(position).getContactId()

扩展ArrayAdapter<AutoCustomerModel>

片段进行必要的修改

class AutoCustomAdapter extends ArrayAdapter<AutoCustomerModel> { // String -> AutoCustomerModel
    public AutoCustomAdapter() {
        super(context, android.R.layout.simple_dropdown_item_1line);
        suggestions = new ArrayList<AutoCustomerModel>(); // String -> AutoCustomerModel
    }

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

    @Override
    public AutoCustomerModel  getItem(int index) { // String -> AutoCustomerModel
        return suggestions.get(index);
    }


    // .... the rest of code
}

以上是关于AutoCompleteTextView - 我正在使用autocompletetextview,它显示来自API的值(返回名称和代码)的主要内容,如果未能解决你的问题,请参考以下文章

AutoCompleteTextView

AutoCompleteTextView - 我正在使用autocompletetextview,它显示来自API的值(返回名称和代码)

如何在 AutoCompleteTextView 中定位光标

AutoCompleteTextView

AutoCompleteTextView 项目选择触发事件

AutoCompleteTextView - 禁用过滤