我正在从服务器检索值以供自动完成文本视图使用,我获得了包含用户名称及其用户ID的用户列表。现在,当我从列表中选择任何用户名时,我想获取该用户名的用户ID。
请查看我的代码:
SearchFriendsAdapter.java
当我尝试获取用户ID时,出现了错误“无法将字符串转换为SearchFriends”。请帮助我实现我的目标。提前致谢。
请查看我的代码:
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();
}
});
当我尝试获取用户ID时,出现了错误“无法将字符串转换为SearchFriends”。请帮助我实现我的目标。提前致谢。