首页 文章

RecyclerView的适配器显示相同的图片而不是不同的图片

提问于
浏览
0

我有一个适配器,它应该从Firebase存储中获取每个用户的 Profiles 图片并将其设置在ImageView中,但会发生的是,每次更新时,它都会为所有图像视图更改相同的 Profiles 图片 . (因此它显示了所有图像视图上的最后一张图片) . 这是获取和设置图片的代码部分:

@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 = new MyAdapter(myDataset); ValueEventListener listener = new 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);

2 回答

  • 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;
          }
    
          }
    
  • 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); 
                    }
                });
    
    
            }
    

相关问题