javaandroidautocompletetextview

How to get id of selected item in autocompletetextview in android?


I am retrieving values from server for autocompletetextview, I am getting the list of user names along with their user ids, now what I want is when i select any username from list I want to get user id of that username.

Please have look at my code:

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

}

Here i am setting the adapter

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

While I am trying to get user_id as above it gives me error:

"Cannot cast from String to SearchFriends"

Please help me to achieve what I want.


Solution

  • Adapter you are using for et_search using ArrayList<SearchFriends>

    and you are trying to get friend_id which is string or int and you are casting it to SearchFriends which is not possible.

    if you want SearchFriends object then use like this

    SearchFriends object = (SearchFriends)adapter.getItem(position);
    

    Sorry i have not check you adapter

    here is the mistake you are returning string here so you cannot get SearchFriends

    @Override
    public String getItem(int index) {
    
        return items.get(index).getFriend_name();
    
    }
    

    So i will say do this and you are good to go.

    @Override
    public SearchFriends getItem(int index) {
            return items.get(index);    
    }