我使用模型类来获取响应并从中获取值。
我想从自动完成下拉列表中选择值(名称)时获取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列表中选择名称时,我想获得代码
答案 0 :(得分:0)
只要AutoCompleteTextView
的项目为AutoCustomerModel
,您展开的适配器就可以替换为ArrayAdapter<AutoCustomerModel>
。完成必要的修改后,您可以使用adapter.getItem(position).getContactId()
将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
}