我的ListView
项目包含以下组件 - TextView
,然后在它下面有两个ImageViews
- like
和dislike
。Android单击一个ListView项目会影响其他ListView项目吗?
所以,当我在任like
或dislike
单击我希望能够从灰色like
或dislike
ImageView
变为蓝色。
当我点击like
时,like
ImageView
确实从灰色变为蓝色。但不仅对于相应的ListView
项目,而是列表中的每个第3项 - 如果我在我的列表中有10个项目,并且我点击列表中第一项的like
那么第4,7和10项like
ImageView
从灰色到蓝色。
在我的post_list_item.xml
中的文件root element
我指定了以下android:descendantFocusability="blocksDescendants"
但它也没有帮助。
我的问题是 - 什么我必须这样做,当我点击任何like
或dislike
我将能够改变like
或dislike
ImageView
由灰色变为蓝色,而不会影响其他列表中的项目?
这里是我的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);
}
});
}
}
感谢您抽出时间来回答我的问题。我只是做了你所说的,但它仍然不起作用。 – Nikolaj
它正在改变图像或没有。它是否会影响到像你之前说过的其他项目? –
是的,它正在改变图像,但它仍然影响列表中的其他项目。 – Nikolaj