访问AutoCompleteTextView的CustomAdapter中所选项的值

时间:2017-06-15 17:51:21

标签: java android arraylist custom-adapter

我试图通过以列表形式从服务器获取国家/地区列表来填充autocompletetextview。 我已经能够使用autocompletetextview填充customAdapter。但是,我很难找到一种方法来访问对象中其他值,例如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在列表中显示。我需要获得所选id的{​​{1}}。

我的自定义 CountryAdapter

name

在我的活动中:

    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); } 来表示所选项目的位置。但我想知道是否有可能访问国家对象中的其他变量。因为我为此目的调整了CustomAdapter。

如果您需要更详细的说明,请告诉我。

感谢。

1 个答案:

答案 0 :(得分:1)

private int UnsetAllBitsExceptMSB(int x) { int y = 1; while (y <= x) { y*=2; } return y / 2; } 中使用setOnItemClickListener并检索自定义对象。像:

autoCompleteTextView