我正在从服务器检索autocompletetextview的值,我得到用户名列表及其用户ID, 现在我想要的是当我从列表中选择任何用户名时,我想获得该用户名的用户ID。
请查看我的代码:
SearchFriendsAdapter.java
public class SearchFriendsAdapter extends ArrayAdapter<String> {
private ArrayList<SearchFriends> items;
public static SearchFriends searchFriends;
public static ArrayList<String> friends_id;
boolean iNetAvailable;
Context context = getContext();
public SearchFriendsAdapter(Activity context, String nameFilter) {
super(context, R.layout.row_search_friend);
items = new ArrayList<SearchFriends>();
}
@Override
public int getCount() {
return items.size();
}
@Override
public String getItem(int index) {
return items.get(index).getFriend_name();
}
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(R.layout.row_search_friend, null);
}
searchFriends = items.get(position);
if (searchFriends != null) {
TextView tv_friend_name = (TextView) v.findViewById(R.id.search_friend_txt_friend_name);
TextView tv_friend_email = (TextView) v.findViewById(R.id.search_friend_txt_friend_email);
SimpleDraweeView iv_friend_pic = (SimpleDraweeView)v.findViewById(R.id.search_friend_img_friend_pic);
tv_friend_name.setText(searchFriends.getFriend_name());
tv_friend_email.setText(searchFriends.getFriend_email_id());
iv_friend_pic.setImageURI(Uri.parse(searchFriends.getFriend_profile_pic()));
}
return v;
}
@Override
public Filter getFilter() {
Filter myFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
iNetAvailable = Utility.isNetworkAvaliable(context);
SearchFriendsAsyncTask sf = new SearchFriendsAsyncTask();
if (constraint != null) {
List<SearchFriends> new_suggestions = sf.getParseJsonWCF(context, constraint.toString());
items.clear();
items.addAll(new_suggestions);
filterResults.values = items;
filterResults.count = items.size();
}
return filterResults;
}
@Override
protected void publishResults(CharSequence contraint,
FilterResults results) {
if (results != null && results.count > 0) {
notifyDataSetChanged();
} else {
notifyDataSetInvalidated();
}
}
};
return myFilter;
}
}
SearchFriendsAsyncTask.java
public class SearchFriendsAsyncTask {
public SearchFriendsAsyncTask() {
super();
}
public List<SearchFriends> getParseJsonWCF(Context mContext, String sName){
List<SearchFriends> ListData = new ArrayList<SearchFriends>();
SearchFriendsModel searchFriendsModel = null;
String webUrl = Constant.URL_SEARCH_FRIEND;
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair(Constant.USER_ID,
Utility.getAppPrefString(mContext ,Constant.USER_ID)));
nameValuePairs
.add(new BasicNameValuePair("name", sName));
String response = Utility.postRequest(webUrl, nameValuePairs);
JSONObject jObject = new JSONObject(response);
Log.v("SEARCH FRIENDS RESPONSE : ", jObject.toString());
searchFriendsModel = (SearchFriendsModel) new Gson().fromJson(
jObject.toString(), SearchFriendsModel.class);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return searchFriendsModel.getFriends();
}
}
SearchModel.java
public class SearchFriends {
String friend_user_id;
String friend_name;
String friend_email_id;
String friend_profile_pic;
public SearchFriends(String friend_user_id, String friend_name,
String friend_email_id, String friend_profile_pic) {
super();
this.friend_user_id = friend_user_id;
this.friend_name = friend_name;
this.friend_email_id = friend_email_id;
this.friend_profile_pic = friend_profile_pic;
}
public String getFriend_user_id() {
return friend_user_id;
}
public void setFriend_user_id(String friend_user_id) {
this.friend_user_id = friend_user_id;
}
public String getFriend_name() {
return friend_name;
}
public void setFriend_name(String friend_name) {
this.friend_name = friend_name;
}
public String getFriend_email_id() {
return friend_email_id;
}
public void setFriend_email_id(String friend_email_id) {
this.friend_email_id = friend_email_id;
}
public String getFriend_profile_pic() {
return friend_profile_pic;
}
public void setFriend_profile_pic(String friend_profile_pic) {
this.friend_profile_pic = friend_profile_pic;
}
}
SearchFriendsModel.java
public class SearchFriendsModel {
private String response;
private String message;
private List<SearchFriends> friends;
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<SearchFriends> getFriends() {
return friends;
}
public void setFriends(List<SearchFriends> friends) {
this.friends = friends;
}
}
我在这里设置适配器
et_search.setAdapter(new SearchFriendsAdapter(this, et_search.getText().toString()));
et_search.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
SearchFriendsAdapter adapter = ((SearchFriendsAdapter)
et_search.getAdapter());
String friend_id = ((SearchFriends)adapter
.getItem(position)).getFriend_user_id();
}
});
当我试图获得上面的user_id时,它给了我错误&#34;无法从String转换为SearchFriends&#34;
请帮助我实现我想要的。 提前谢谢。
答案 0 :(得分:5)
在适配器中更改此内容
@Override
public SearchFriends getItem(int index) {
return items.get(index);
}
et_search.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
SearchFriends freindPOJO= adapter
.getItem(position);
String friends_id = freindPOJO.getFriend_user_id()
}
});
答案 1 :(得分:4)
您用于 et_search 的适配器
ArrayList<SearchFriends>
并且您正在尝试 friend_id string或int ,并且您将其投射到 SearchFriends ,这是不可能的。
如果你想要 SearchFriends 对象,那就像这样使用
SearchFriends object = (SearchFriends)adapter.getItem(position);
抱歉,我没有检查你的适配器
这是你在这里返回字符串的错误所以你不能得到 SearchFriends
@Override
public String getItem(int index) {
return items.get(index).getFriend_name();
}
所以我会说这样做,你很高兴。
@Override
public SearchFriends getItem(int index) {
return items.get(index);
}
答案 2 :(得分:0)
Narendra Kumar的答案很好。
这是Kotlin版本的示例,其中一个ID为AutoCompleteTextView
的{{1}}和一个基于atvUser
个对象的ArrayAdapter
的{{1}}:
ArrayList
我为此分配了适配器:
User
之所以有效,是因为我在separated file中声明了一个扩展函数:
atvUser.onItemClickListener = AdapterView.OnItemClickListener {
parent, _, position, _ ->
userId = (parent.getItemAtPosition(position) as User).id
}
希望有帮助。
答案 3 :(得分:0)
只需调用您的adapter.getItem(position),您将获得所需的输出。 例如skillLevelAdapter.getItem(position).getName()保存了我整整一整天。