【问题标题】:accessing values of selected item in AutoCompleteTextView's CustomAdapter访问 AutoCompleteTextView 的 CustomAdapter 中选定项目的值
【发布时间】:2017-06-15 17:51:20
【问题描述】:

我试图通过从服务器以列​​表形式获取国家/地区列表来填充autocompletetextview。 我已经能够用customAdapter 填充autocompletetextview。但是,我很难找到一种方法来访问对象中所选项目的其他值,例如 iddefaultCurrencyId

我的国家对象:

public class Country {

private Integer id;
private String name;
private Integer defaultCurrencyId;
private String phonePrefix;
private Integer status;
private Object deletedAt;
private String createdAt;
private String updatedAt;

... standard getter and setter ...

}

我正在使用name 在列表中显示。我需要获取所选nameid

我的自定义CountryAdapter

    public class CountryAdapter extends ArrayAdapter<Country> {
    private final String MY_DEBUG_TAG = "CountryAdapter";
    private ArrayList<Country> items;
    private ArrayList<Country> itemsAll;
    private ArrayList<Country> suggestions;
    private int viewResourceId;

    public CountryAdapter(Context context, int viewResourceId, ArrayList<Country> items) {
        super(context, viewResourceId, items);
        this.items = items;
        this.itemsAll = (ArrayList<Country>) items.clone();
        this.suggestions = new ArrayList<Country>();
        this.viewResourceId = viewResourceId;
    }

    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);
        }
        Country country = items.get(position);
        if (country != null) {
            TextView countryNameLabel = (TextView) v.findViewById(R.id.textViewItem);
            if (countryNameLabel != null) {
//              Log.i(MY_DEBUG_TAG, "getView Country Name:"+country.getName());
                countryNameLabel.setText(country.getName());
            }
        }
        return v;
    }

    @Override
    public Filter getFilter() {
        return nameFilter;
    }

    Filter nameFilter = new Filter() {
        @Override
        public String convertResultToString(Object resultValue) {
            String str = ((Country)(resultValue)).getName();
            return str;
        }
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            if(constraint != null) {
                suggestions.clear();
                for (Country country : itemsAll) {
                    if(country.getName().toLowerCase().startsWith(constraint.toString().toLowerCase())){
                        suggestions.add(country);
                    }
                }
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
                return filterResults;
            } else {
                return new FilterResults();
            }
        }
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            ArrayList<Country> filteredList = (ArrayList<Country>) results.values;
            if(results != null && results.count > 0) {
                clear();
                for (Country c : filteredList) {
                    add(c);
                }
                notifyDataSetChanged();
            }
        }
    };
}

在我的活动中:

public void setCountryAdapter(ArrayList<Country> country){

        CountryAdapter countryAdapter = new CountryAdapter(getActivity(), R.layout.list_view_row, country);
        _actvCountry.setAdapter(countryAdapter);

}

我尝试使用OnItemSelectedListener 给出所选项目的位置。但我想知道是否可以访问 Country Object 中的其他变量。因为我为此目的调整了 CustomAdapter。

如果您需要对此案进行更多详细说明,请告诉我。

谢谢。

【问题讨论】:

    标签: java android arraylist custom-adapter


    【解决方案1】:

    autoCompleteTextView 中使用:setOnItemClickListener 并检索自定义对象。喜欢:

      autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
            Country  country  = (Country) parent.getItemAtPosition(position);
            String id = country.getId();
            //other information
        }
    });
    

    【讨论】:

    • 谢谢这是确切的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多