我有一个适配器,应该从Firebase存储中获取每个用户的个人资料图片并将其设置在ImageView中,但是发生的是,每次更新时,它都会为所有图像视图更改相同的个人资料图片。 (因此,它将在所有图像视图上显示最后获取的图片)。这是获取和设置图片的代码部分:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
myHolder = holder;
StorageReference ref = storageRef.child("profilePictures").child(mDataset.get(position));
final long ONE_MEGABYTE = 1024 * 1024;
ref.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Log.d("Tag", "Success");
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Log.d("Tag", "Failure");
}
});
myHolder.singleItemImage.setImageBitmap(bitmap);
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:srcCompat="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Test"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/icon" />
</android.support.constraint.ConstraintLayout>
mUpdatesReference = FirebaseDatabase.getInstance()。getReference(“ timeUpdates”); mAdapter =新的MyAdapter(myDataset); ValueEventListener监听程序=新的ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
boolean isChanged = false;
for(DataSnapshot datas: dataSnapshot.getChildren()){
if (time < 180 )){
if (!myDataset.contains(datas.getKey().toString())){
myDataset.add(datas.getKey().toString());
isChanged = true;
}
} else {
myDataset.remove(datas.getKey().toString());
isChanged = true;
}
if (isChanged) {
mAdapter.notifyDataSetChanged();
}
}
mUpdatesReference.addValueEventListener(listener);
mRecyclerView.setAdapter(mAdapter);
答案 0 :(得分:1)
因为获得位图并将其设置为方法之外的位置,并且不释放该位图,所以它将为所有用户设置。 只需将您的方法更改为此:
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
myHolder = holder;
StorageReference ref = storageRef.child("profilePictures").child(mDataset.get(position));
final long ONE_MEGABYTE = 1024 * 1024;
ref.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Log.d("Tag", "Success");
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
if(bitmap!=null)
myHolder.singleItemImage.setImageBitmap(bitmap);
else myHolder.singleItemImage.setImageResource(R.drawable.YourPlaceholder);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
Log.d("Tag", "Failure");
myHolder.singleItemImage.setImageResource(R.drawable.YourPlaceholder);
}
});
}
答案 1 :(得分:0)
首先使用位图参考更新模型
void downloadImage(int rowindex){
StorageReference ref = storageRef.child("profilePictures").child(mDataset.get(rowindex).getId());
final long ONE_MEGABYTE = 1024 * 1024;
ref.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Log.d("Tag", "Success");
bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mDataset.get(rowindex).setDownloadedBitmap(bitmap);
notifyDataSetChanged ();
}
在onBindViewHolder()中添加
public void onBindViewHolder(MyViewHolder holder, int position) {
myHolder = holder;
Bitmap currentBitmap=mDataset.get(rowindex).getDownloadedBitmap();
if(currentBitmap!=null){
myHolder.singleItemImage.setImageBitmap(currentBitmap);
}else{
downloadImage(position);
myHolder.singleItemImage.setImageResource(R.drawable.default);
}
}
ProfilePictureItem.java
public class ProfilePictureItem{
private String key;
private Bitmap downloadedBitmap;
public setKey(String key){
this.key=key;
}
public getKey(){
return key;
}
public setDownloadedBitmap(Bitmap downloadedBitmap){
this.downloadedBitmap=downloadedBitmap;
}
public getDownloadedBitmap(){
return downloadedBitmap;
}
}