我正在做汽车选择功能。在我的情况下,汽车类型(迷你,微型),汽车选择图像和汽车取消选择图像存储在sqlite中,然后我读取数据库并将汽车选择细节存储在水平循环视图中。一切都运行正常,但我无法选择特定的汽车。当我选择另一个图像时,它会更改为所选图像,但之前选择的图像不会更改为未选择的图像。任何人都可以帮我解决这个问题。
下面的是我的回收器适配器
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
ListItemClickListener mlistener;
Context mCtx;
Cursor mCursor;
private int clickposition = 0;
public RecycleAdapter(Context context, ListItemClickListener listener) {
mCtx = context;
mlistener = listener;
}
@Override
public RecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_vehicles_available, parent, false);
ViewHolder vh = new ViewHolder(view);
return vh;
}
@Override
public void onBindViewHolder(RecycleAdapter.ViewHolder holder, int position) {
mCursor.moveToPosition(position);
String carType = mCursor.getString(mCursor.getColumnIndex(COLUMN_SERVICE));
holder.carType.setText(carType);
byte[] image = mCursor.getBlob(mCursor.getColumnIndex(COLUMN_IMAGE));
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
holder.carImage.setImageBitmap(bitmap);
}
@Override
public int getItemCount() {
if (mCursor == null)
return 0;
return mCursor.getCount();
}
public void swapCursor(Cursor data) {
mCursor = data;
notifyDataSetChanged();
}
public interface ListItemClickListener {
void onClickListener(int position);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView carType;
ImageView carImage;
RelativeLayout rl;
public ViewHolder(View itemView) {
super(itemView);
carType = (TextView) itemView.findViewById(R.id.txtcarType);
carImage = (ImageView) itemView.findViewById(R.id.select_carImage);
rl = (RelativeLayout) itemView.findViewById(R.id.rl);
rl.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rl:
clickposition = getAdapterPosition();
Log.d("clickposition", String.valueOf(clickposition));
int i;
for (i = 0; i < mCursor.getCount(); i++) {
int id = mCursor.getInt(mCursor.getColumnIndex(DbContract.DbEntry._ID));
mlistener.onClickListener(id);
if (clickposition != i) {
mCursor.moveToPosition(i);
Log.d("clickposition1", String.valueOf(i));
byte[] image = mCursor.getBlob(mCursor.getColumnIndex(COLUMN_IMAGE));
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
carImage.setImageBitmap(bitmap);
}
mCursor.moveToPosition(i);
Log.d("clickposition2", String.valueOf(i));
byte[] image_sel = mCursor.getBlob(mCursor.getColumnIndex(COLUMN_IMAGE_SELECTED));
Bitmap bitmap_sel = BitmapFactory.decodeByteArray(image_sel, 0, image_sel.length);
carImage.setImageBitmap(bitmap_sel);
}
Toast.makeText(mCtx, "driverlocation = Firebase.getreference" + mCursor.getString(mCursor.getColumnIndex(COLUMN_SERVICE)), Toast.LENGTH_SHORT).show();
break;
}
}
}
}
答案 0 :(得分:0)
将onBindViewholder更改为此
@Override
public void onBindViewHolder(RecycleAdapter.ViewHolder holder, int position) {
mCursor.moveToPosition(position);
String carType = mCursor.getString(mCursor.getColumnIndex(COLUMN_SERVICE));
holder.carType.setText(carType);
byte[] image = mCursor.getBlob(mCursor.getColumnIndex(COLUMN_IMAGE));
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
holder.carImage.setImageBitmap(bitmap);
holder.rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickposition = position;
Log.d("clickposition", String.valueOf(clickposition));
int i;
for (i = 0; i < mCursor.getCount(); i++) {
int id = mCursor.getInt(mCursor.getColumnIndex(DbContract.DbEntry._ID));
mlistener.onClickListener(id);
if (clickposition != i) {
mCursor.moveToPosition(i);
Log.d("clickposition1", String.valueOf(i));
byte[] image = mCursor.getBlob(mCursor.getColumnIndex(COLUMN_IMAGE));
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
carImage.setImageBitmap(bitmap);
}
mCursor.moveToPosition(i);
Log.d("clickposition2", String.valueOf(i));
byte[] image_sel = mCursor.getBlob(mCursor.getColumnIndex(COLUMN_IMAGE_SELECTED));
Bitmap bitmap_sel = BitmapFactory.decodeByteArray(image_sel, 0, image_sel.length);
carImage.setImageBitmap(bitmap_sel);
}
Toast.makeText(mCtx, "driverlocation = Firebase.getreference" + mCursor.getString(mCursor.getColumnIndex(COLUMN_SERVICE)), Toast.LENGTH_SHORT).show();
}
});
}