I am creating an E-commerce app to display a list of products in a listView. Each list item has a title, image and buttons named as like, dislike and favorite.
When the user presses the like button on a particular list item, the image of the button should change to "liked" and a particular API is called. I have done this inside the onClickListener inside the getView() method using ArrayAdapter class.
Like this.
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
likeButton= (Button)
listItemView.findViewById(R.id.like_button);
//likeButton.setClickable(currentProduct.getmLikeButton());
dislikeButton = (Button)
listItemView.findViewById(R.id.dislike_button);
favoritesButton = (Button)
listItemView.findViewById(R.id.favorite_button);
}
TextView productNameView = (TextView)
listItemView.findViewById(R.id.product_name);
ImageView productImageView = (ImageView)
listItemView.findViewById(R.id.product_image);
currentProduct = getItem(position);
likeButton.setTag(position);
dislikeButton.setTag(position);
favoritesButton.setTag(position);
likeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position=(Integer)v.getTag();
currentProduct = getItem(position);
productId = currentProduct.getProductId();
likeButton = (Button) v.findViewById(R.id.like_button);
Uri baseUri = Uri.parse(UNBXD_UACTION);
Uri.Builder uriBuilder = baseUri.buildUpon();
uriBuilder.appendQueryParameter("uaction", "like");
uriBuilder.appendQueryParameter("user", "8222");
uriBuilder.appendQueryParameter(PRODUCT_ID,productId);
uriBuilder.appendQueryParameter(MATERIALL, "true");
//uriBuilder.appendQueryParameter(USER_ID, "5");
uriBuilder.appendQueryParameter(UNDO, "false");
Intent intent = new
Intent(Intent.ACTION_WEB_SEARCH, uriBuilder.build());
intent.getData();
likeButton.setBackgroundResource(R.drawable.liked);
Log.v("liked url is ", uriBuilder.toString() + " " +
String.valueOf(position));
}
});
The problem I noticed is that, if I click the like button on the first item in the listView, it's not just the first item's like button that changes to 'liked' but also the like buttons in every third listView item that are displayed changes to 'liked' but the API call happens only once and correctly appropriate to the item clicked.
I am not able to figure out exactly how and why every third item's like button is getting pressed starting from the like button that I actually pressed but (fortunately) only one and also the correct API is called.
Please help.