title: How to get position of a button in a listview?
body: <p>I have a listview with Textview, ImageView and Button ,what i would like is to change the background of the button when its clicked , i hope you understunded my issue .</p>
  
  <p>This is the Adapter .</p>
  
  <pre><code>package com.example.mahdi.chat;
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.support.v4.app.Fragment;
  import android.text.Html;
  import android.text.TextUtils;
  import android.text.format.DateUtils;
  import android.text.method.LinkMovementMethod;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.android.volley.Request;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.VolleyLog;
  import com.android.volley.toolbox.ImageLoader;
  import com.android.volley.toolbox.NetworkImageView;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONException;
  import org.json.JSONObject;
  import org.w3c.dom.Text;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import circleview.CircleImageView;
  
  public class FeedListAdapter extends BaseAdapter {
  
  private static final String TAG = FeedListAdapter.class.getSimpleName();
  
  private Activity activity;
  private LayoutInflater inflater;
  private List&lt;FeedItem&gt; feedItems;
  JSONParser jsonParser = new JSONParser();
  private String URL = "http://10.0.3.2/social/like.php";
  private String user_id;
  private Button like,comment;
  
  private static final String TAG_SUCCESS = "success";
  FeedItem item;
  ImageLoader imageLoader = AppController.getInstance().getImageLoader();
  
  public FeedListAdapter(Activity activity, List&lt;FeedItem&gt; feedItems, String user_id) {
      this.activity = activity;
      this.feedItems = feedItems;
      this.user_id = user_id;
  }
  
  @Override
  public int getCount() {
      return feedItems.size();
  }
  
  @Override
  public Object getItem(int location) {
      return feedItems.get(location);
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  public void refresh(List&lt;FeedItem&gt; newlist) {
      feedItems.addAll(newlist);
      this.notifyDataSetChanged();
  }
  public List&lt;FeedItem&gt; getData() {
      return feedItems;
  }
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
  
      if (inflater == null)
          inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      if (convertView == null)
          convertView = inflater.inflate(R.layout.posts_row, null);
  
      if (imageLoader == null)
          imageLoader = AppController.getInstance().getImageLoader();
  
     final  TextView name = (TextView) convertView.findViewById(R.id.name);
      TextView timestamp = (TextView) convertView
              .findViewById(R.id.time);
      TextView statusMsg = (TextView) convertView
              .findViewById(R.id.status);
      TextView url = (TextView) convertView.findViewById(R.id.url);
      CircleImageView profilePic = (CircleImageView) convertView
              .findViewById(R.id.profile_img);
      FeedImageView feedImageView = (FeedImageView) convertView
              .findViewById(R.id.image);
  
  
      TextView nbr_likes = (TextView)convertView.findViewById(R.id.nbr_likes);
      TextView nbr_comments = (TextView)convertView.findViewById(R.id.nbr_comments);
  
  
      item = feedItems.get(position);
  
  
  
  
      like =(Button)convertView.findViewById(R.id.like);
      comment =(Button)convertView.findViewById(R.id.comment);
  
  
  
  
      like.setOnClickListener(new View.OnClickListener()
      {
  
          @Override
          public void onClick(View v) {
  
              item = feedItems.get(position);
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              params.put("post_id", item.getId());
              params.put("user_id", user_id);
              Toast.makeText(activity, item.getId(), Toast.LENGTH_SHORT).show();
              Toast.makeText(activity, user_id, Toast.LENGTH_SHORT).show();
              // making fresh volley request and getting json
              CustomRequest jsonReq = new CustomRequest(Request.Method.POST,
                      URL,params , new Response.Listener&lt;JSONObject&gt;() {
  
                  @Override
                  public void onResponse(JSONObject response) {
                      VolleyLog.d(TAG, "Response: " + response.toString());
                      if (response != null) {
  
                          parseJsonFeed(response);
  
  
                      }
                  }
              }, new Response.ErrorListener() {
  
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      VolleyLog.d(TAG, "Error: " + error.getMessage());
                  }
              });
  
              // Adding request to volley request queue
              AppController.getInstance().addToRequestQueue(jsonReq);
  
  
  
          }
  
      });
  
  
  
      comment.setOnClickListener(new View.OnClickListener()
      {
  
          @Override
          public void onClick(View v) {
  
  
  
          }
  
  
      });
  
      name.setText(item.getName());
  
     nbr_likes.setText(item.getNbrLikes()+ " Likes");
      nbr_comments.setText(item.getNbrComments() + " Comments");
  
  
      // Converting timestamp into x ago format
      CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
              Long.parseLong(item.getTimeStamp()),
              System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
      timestamp.setText(timeAgo);
  
      // Chcek for empty status message
      if (!TextUtils.isEmpty(item.getStatus())) {
          statusMsg.setText(item.getStatus());
          statusMsg.setVisibility(View.VISIBLE);
      } else {
          // status is empty, remove from view
          statusMsg.setVisibility(View.GONE);
      }
  
      // Checking for null feed url
      if (item.getUrl() != null) {
          url.setText(Html.fromHtml("&lt;a href=\"" + item.getUrl() + "\"&gt;"
                  + item.getUrl() + "&lt;/a&gt; "));
  
          // Making url clickable
          url.setMovementMethod(LinkMovementMethod.getInstance());
          url.setVisibility(View.VISIBLE);
      } else {
          // url is null, remove from the view
          url.setVisibility(View.GONE);
      }
  
      // user profile pic
      profilePic.setImageUrl(item.getProfilePic(), imageLoader);
  
      // Feed image
      if (item.getImge() != null) {
          feedImageView.setImageUrl(item.getImge(), imageLoader);
          feedImageView.setVisibility(View.VISIBLE);
          feedImageView
                  .setResponseObserver(new FeedImageView.ResponseObserver() {
                      @Override
                      public void onError() {
                      }
  
                      @Override
                      public void onSuccess() {
                      }
                  });
      } else {
          feedImageView.setVisibility(View.GONE);
      }
      return convertView;
  }
  
  
  private void parseJsonFeed(JSONObject response) {
      int success;
      try {
          success = response.getInt(TAG_SUCCESS);
          if (success == 1) {
              notifyDataSetChanged();
          } else {
              Toast.makeText(activity, "erreur", Toast.LENGTH_SHORT).show();
          }
  
  
          //listAdapter.addAll(feedItems);
      } catch (JSONException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>FeedItem Class</p>
  
  <pre><code>package com.example.mahdi.chat;
  
  public class FeedItem {
  
  private String id,name, status, image, profile_img, time, url,nbr_likes,nbr_comments;
  
  public FeedItem() {
  }
  
  public FeedItem(String id, String name, String image, String status,
                  String profilePic, String timeStamp, String url,String nbr_likes,String nbr_comments) {
      super();
      this.id = id;
      this.name = name;
      this.image = image;
      this.status = status;
      this.profile_img = profilePic;
      this.time = timeStamp;
      this.url = url;
      this.nbr_likes = nbr_likes;
      this.nbr_comments = nbr_comments;
  }
  
  public String getId() {
      return id;
  }
  
  public void setId(String id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getImge() {
      return image;
  }
  
  public void setImge(String image) {
      this.image = image;
  }
  
  public String getStatus() {
      return status;
  }
  
  public void setStatus(String status) {
      this.status = status;
  }
  
  public String getProfilePic() {
      return profile_img;
  }
  
  public void setProfilePic(String profilePic) {
      this.profile_img = profilePic;
  }
  
  public String getTimeStamp() {
      return time;
  }
  
  public void setTimeStamp(String timeStamp) {
      this.time = timeStamp;
  }
  
  public String getUrl() {
      return url;
  }
  
  public void setUrl(String url) {
      this.url = url;
  }
  
  public String getNbrLikes() {
      return nbr_likes;
  }
  
  public void setNbrLikes(String nbr_likes) {
      this.nbr_likes = nbr_likes;
  }
  
  public String getNbrComments() {
      return nbr_comments;
  }
  
  public void setNbrComments(String nbr_comments) {
      this.nbr_comments = nbr_comments;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The problem is the button Like , i would like to change the drawable of the button when its clicked .Thanks.</p>
  
  <p><strong>EDIT :</strong></p>
  
  <pre><code>package com.example.mahdi.chat;
  
  
  import android.app.Activity;
  import android.app.ProgressDialog;
  import android.content.Context;
  import android.content.Intent;
  import android.graphics.Color;
  import android.os.AsyncTask;
  import android.support.v4.app.Fragment;
  import android.text.Html;
  import android.text.TextUtils;
  import android.text.format.DateUtils;
  import android.text.method.LinkMovementMethod;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import com.android.volley.Request;
  import com.android.volley.Response;
  import com.android.volley.VolleyError;
  import com.android.volley.VolleyLog;
  import com.android.volley.toolbox.ImageLoader;
  import com.android.volley.toolbox.NetworkImageView;
  
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONException;
  import org.json.JSONObject;
  import org.w3c.dom.Text;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import circleview.CircleImageView;
  
  public class FeedListAdapter extends BaseAdapter {
  
  private static final String TAG = FeedListAdapter.class.getSimpleName();
  
  private Activity activity;
  private LayoutInflater inflater;
  private List&lt;FeedItem&gt; feedItems;
  JSONParser jsonParser = new JSONParser();
  private String URL = "http://10.0.3.2/social/like.php";
  private String user_id;
  private Button like,comment;
  
  private static final String TAG_SUCCESS = "success";
  FeedItem item;
  ImageLoader imageLoader = AppController.getInstance().getImageLoader();
  
  public FeedListAdapter(Activity activity, List&lt;FeedItem&gt; feedItems, String user_id) {
      this.activity = activity;
      this.feedItems = feedItems;
      this.user_id = user_id;
  }
  
  @Override
  public int getCount() {
      return feedItems.size();
  }
  
  @Override
  public Object getItem(int location) {
      return feedItems.get(location);
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  public void refresh(List&lt;FeedItem&gt; newlist) {
      feedItems.addAll(newlist);
      this.notifyDataSetChanged();
  }
  public List&lt;FeedItem&gt; getData() {
      return feedItems;
  }
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
  
      if (inflater == null)
          inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      if (convertView == null)
          convertView = inflater.inflate(R.layout.posts_row, null);
  
      if (imageLoader == null)
          imageLoader = AppController.getInstance().getImageLoader();
  
     final  TextView name = (TextView) convertView.findViewById(R.id.name);
      TextView timestamp = (TextView) convertView
              .findViewById(R.id.time);
      TextView statusMsg = (TextView) convertView
              .findViewById(R.id.status);
      TextView url = (TextView) convertView.findViewById(R.id.url);
      CircleImageView profilePic = (CircleImageView) convertView
              .findViewById(R.id.profile_img);
      FeedImageView feedImageView = (FeedImageView) convertView
              .findViewById(R.id.image);
  
  
      TextView nbr_likes = (TextView)convertView.findViewById(R.id.nbr_likes);
      TextView nbr_comments = (TextView)convertView.findViewById(R.id.nbr_comments);
  
  
      item = feedItems.get(position);
  
  
  
  
      like =(Button)convertView.findViewById(R.id.like);
      comment =(Button)convertView.findViewById(R.id.comment);
  
  
  
      like.setTag(position);
  
      like.setOnClickListener(new View.OnClickListener()
      {
  
          @Override
          public void onClick(View v) {
  
              int position = (Integer) v.getTag();
  
              like.setBackgroundColor(Color.BLACK);
  
  
              item = feedItems.get(position);
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              params.put("post_id", item.getId());
              params.put("user_id", user_id);
              Toast.makeText(activity, item.getId(), Toast.LENGTH_SHORT).show();
              Toast.makeText(activity, user_id, Toast.LENGTH_SHORT).show();
              // making fresh volley request and getting json
              CustomRequest jsonReq = new CustomRequest(Request.Method.POST,
                      URL,params , new Response.Listener&lt;JSONObject&gt;() {
  
                  @Override
                  public void onResponse(JSONObject response) {
                      VolleyLog.d(TAG, "Response: " + response.toString());
                      if (response != null) {
  
                          parseJsonFeed(response);
  
  
                      }
                  }
              }, new Response.ErrorListener() {
  
                  @Override
                  public void onErrorResponse(VolleyError error) {
                      VolleyLog.d(TAG, "Error: " + error.getMessage());
                  }
              });
  
              // Adding request to volley request queue
              AppController.getInstance().addToRequestQueue(jsonReq);
  
  
  
          }
  
      });
  
  
  
      comment.setOnClickListener(new View.OnClickListener()
      {
  
          @Override
          public void onClick(View v) {
              item = feedItems.get(position);
              Intent i = new Intent(activity, Comment.class);
              i.putExtra("user_id",item.getId());
              i.putExtra("name",item.getName());
              i.putExtra("image",item.getImge());
              i.putExtra("status",item.getStatus());
              i.putExtra("profile_img",item.getProfilePic());
              i.putExtra("time",item.getTimeStamp());
              i.putExtra("url",item.getUrl());
              i.putExtra("nbr_likes",item.getNbrLikes());
              i.putExtra("nbr_comments",item.getNbrComments());
              activity.startActivity(i);
  
  
          }
  
  
      });
  
      name.setText(item.getName());
  
     nbr_likes.setText(item.getNbrLikes()+ " Likes");
      nbr_comments.setText(item.getNbrComments() + " Comments");
  
  
      // Converting timestamp into x ago format
      CharSequence timeAgo = DateUtils.getRelativeTimeSpanString(
              Long.parseLong(item.getTimeStamp()),
              System.currentTimeMillis(), DateUtils.SECOND_IN_MILLIS);
      timestamp.setText(timeAgo);
  
      // Chcek for empty status message
      if (!TextUtils.isEmpty(item.getStatus())) {
          statusMsg.setText(item.getStatus());
          statusMsg.setVisibility(View.VISIBLE);
      } else {
          // status is empty, remove from view
          statusMsg.setVisibility(View.GONE);
      }
  
      // Checking for null feed url
      if (item.getUrl() != null) {
          url.setText(Html.fromHtml("&lt;a href=\"" + item.getUrl() + "\"&gt;"
                  + item.getUrl() + "&lt;/a&gt; "));
  
          // Making url clickable
          url.setMovementMethod(LinkMovementMethod.getInstance());
          url.setVisibility(View.VISIBLE);
      } else {
          // url is null, remove from the view
          url.setVisibility(View.GONE);
      }
  
      // user profile pic
      profilePic.setImageUrl(item.getProfilePic(), imageLoader);
  
      // Feed image
      if (item.getImge() != null) {
          feedImageView.setImageUrl(item.getImge(), imageLoader);
          feedImageView.setVisibility(View.VISIBLE);
          feedImageView
                  .setResponseObserver(new FeedImageView.ResponseObserver() {
                      @Override
                      public void onError() {
                      }
  
                      @Override
                      public void onSuccess() {
                      }
                  });
      } else {
          feedImageView.setVisibility(View.GONE);
      }
      return convertView;
  }
  
  
  private void parseJsonFeed(JSONObject response) {
      int success;
      try {
          success = response.getInt(TAG_SUCCESS);
          if (success == 1) {
              notifyDataSetChanged();
          } else {
              Toast.makeText(activity, "erreur", Toast.LENGTH_SHORT).show();
          }
  
  
          //listAdapter.addAll(feedItems);
      } catch (JSONException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>}</p>