title: how to get id of selected item in autocompletetextview in android
body: <p>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.</p>
  
  <p>Please have look at my code:</p>
  
  <p><strong>SearchFriendsAdapter.java</strong></p>
  
  <pre><code>public class SearchFriendsAdapter extends ArrayAdapter&lt;String&gt; {
  
  private ArrayList&lt;SearchFriends&gt; items;
  public static SearchFriends searchFriends;
  public static ArrayList&lt;String&gt; friends_id;
  
  boolean iNetAvailable;
  Context context = getContext();
  
  public SearchFriendsAdapter(Activity context, String nameFilter) {
      super(context, R.layout.row_search_friend);
      items = new ArrayList&lt;SearchFriends&gt;();
  }
  
  @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&lt;SearchFriends&gt; 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 &amp;&amp; results.count &gt; 0) {
                  notifyDataSetChanged();
              } else {
                  notifyDataSetInvalidated();
              }
          }
      };
      return myFilter;
  }
  
  }
  </code></pre>
  
  <p><strong>SearchFriendsAsyncTask.java</strong>  </p>
  
  <pre><code> public class SearchFriendsAsyncTask {
  
  
  public SearchFriendsAsyncTask() {
      super();
  }
  
  public List&lt;SearchFriends&gt; getParseJsonWCF(Context mContext, String sName){
      List&lt;SearchFriends&gt; ListData = new ArrayList&lt;SearchFriends&gt;();
  
      SearchFriendsModel searchFriendsModel = null;
  
      String webUrl = Constant.URL_SEARCH_FRIEND;
  
      try{
          List&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;();
          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(); 
  
  
      }
  
  }
  </code></pre>
  
  <p><strong>SearchModel.java</strong>  </p>
  
  <pre><code>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;
  }
  
  }
  </code></pre>
  
  <p><strong>SearchFriendsModel.java</strong>  </p>
  
  <pre><code>public class SearchFriendsModel {
  
  private String response;
  private String message;
  private List&lt;SearchFriends&gt; 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&lt;SearchFriends&gt; getFriends() {
      return friends;
  }
  public void setFriends(List&lt;SearchFriends&gt; friends) {
      this.friends = friends;
  }
  
  }
  </code></pre>
  
  <p>Here i am setting the adapter   </p>
  
  <pre><code>et_search.setAdapter(new SearchFriendsAdapter(this, et_search.getText().toString()));
      et_search.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; 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();
  
          }
      });
  </code></pre>
  
  <p>while I am trying to get user_id as above it gives me error "Cannot cast from String to SearchFriends"</p>
  
  <p>Please help me to achieve what I want.
  Thanks in advance.</p>