My ListView
item consists of the following components - TextView
and then under it there are two ImageViews
- like
and dislike
.
So when I click on either like
or dislike
I want to be able to change the like
or dislike
ImageView
from grey to blue.
At the moment when I click on like
the like
ImageView
does change from grey to blue. But not only for the corresponding ListView
item but for every 3rd item in the list - so if I have 10 items in my list and I click on the like
of the first item in the list then 4th, 7th and 10th items like
ImageView
change from grey to blue.
In my post_list_item.xml
in the root element
of the file I specified the following android:descendantFocusability="blocksDescendants"
but it doesn't help either.
My question is - what do I have to do so that when I click on either like
or dislike
I would be able to change the like
or dislike
ImageView
from grey to blue without affecting other list items?
Here is my code of the CursorAdapter
public class PostCursorAdapter extends CursorAdapter {
public PostCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.post_list_item, parent, false);
}
@Override
public void bindView(View view, final Context context, Cursor cursor) {
String post = cursor.getString(cursor.getColumnIndex(DBOpenHelper.POST));
final String liked = cursor.getString(cursor.getColumnIndex(DBOpenHelper.LIKED));
String disliked = cursor.getString(cursor.getColumnIndex(DBOpenHelper.DISLIKED));
final String post_id = cursor.getString(cursor.getColumnIndex(DBOpenHelper.POST_ID));
String userliked = cursor.getString(cursor.getColumnIndex(DBOpenHelper.USER_LIKED));
TextView tvPost = (TextView) view.findViewById(R.id.tvPost );
tvJoke.setText(post);
TextView tvLikeCount = (TextView) view.findViewById(R.id.tvLikeCount);
tvLikeCount.setText(liked);
TextView tvDislikesCount = (TextView) view.findViewById(R.id.tvDislikeCount);
tvDislikesCount.setText(disliked);
final ImageView ivLike = (ImageView) view.findViewById(R.id.ivLike);
// has the user liked it?
int check = Integer.parseInt(userliked);
if(check == 1){
String uri = "@drawable/like_blue"; // where myresource (without the extension) is the file
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
ivLike.setImageDrawable(res);
}
ivLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String uri = "@drawable/like_blue";
int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName());
Drawable res = context.getResources().getDrawable(imageResource);
ivLike.setImageDrawable(res);
}
});
}
}