gpt4 book ai didi

java - 访问 AutoCompleteTextView 的 CustomAdapter 中所选项目的值

转载 作者:行者123 更新时间:2023-12-02 12:51:48 25 4
gpt4 key购买 nike

我正在尝试通过从服务器以列​​表形式获取国家/地区列表来填充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在列表中显示。我需要获取所选名称id

我的自定义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();
}
}
};
}

在我的 Activity 中:

public void setCountryAdapter(ArrayList<Country> country){

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

}

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

如果您需要进一步详细说明此案例,请告诉我。

谢谢。

最佳答案

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

关于java - 访问 AutoCompleteTextView 的 CustomAdapter 中所选项目的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44573886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com