Recyclerview 的 Firebase 数据显示错误 - RecyclerView:android 中没有附加适配器
Firebase data to recyclerview shows Error - RecyclerView: No adapter attached in android
你好,我想从 Firebase 获取图像到内存中。当我调试我的代码时,它首先 returns 查看,然后返回 usersPhotoRef.addValueEventListener()
并将 mPhotoArrayList
的大小打印为 0。最后返回 photoRef.getBytes()#addOnSuccessListener()
并将位图放入 ArrayList .我认为应该更正我的代码,以便我可以将 mPhotoArrayList
传递给适配器。我该怎么做或必须进行哪些更改?
此代码的输出:
2019-06-30 15:01:00.130 12154-12154/com.newstrange.worklog E/RecyclerView: No adapter attached; skipping layout
2019-06-30 15:01:00.134 12154-12158/com.newstrange.worklog I/art: Do partial code cache collection, code=24KB, data=30KB
2019-06-30 15:01:00.135 12154-12158/com.newstrange.worklog I/art: After code cache collection, code=23KB, data=30KB
2019-06-30 15:01:00.135 12154-12158/com.newstrange.worklog I/art: Increasing code cache capacity to 128KB
2019-06-30 15:01:00.151 12154-12154/com.newstrange.worklog I/IMAGE_NAME: asdasd_giris_25.54.47_29.Mayıs.2019
2019-06-30 15:01:00.169 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.169 12154-12154/com.newstrange.worklog I/IMAGE_NAME: fdsafdas_giris_25.55.4_29.Mayıs.2019
2019-06-30 15:01:00.170 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.170 12154-12154/com.newstrange.worklog I/IMAGE_NAME: sdf_cikis_8.44.32_30.Mayıs.2019
2019-06-30 15:01:00.173 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.842 12154-12154/com.newstrange.worklog I/GET_BITMAP: 1
2019-06-30 15:01:00.846 12154-12154/com.newstrange.worklog I/GET_BITMAP: 2
2019-06-30 15:01:00.858 12154-12154/com.newstrange.worklog I/GET_BITMAP: 3
代码:
package com.newstrange.worklog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.io.IOException;
import java.util.ArrayList;
public class ListSelfiesFragment extends Fragment {
private FirebaseStorage mStorageIns;
private FirebaseDatabase mDatabaseIns;
private RecyclerView mRecyclerView;
private GridLayoutManager mLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mDatabaseIns = FirebaseDatabase.getInstance(); // DATABASE
mStorageIns = FirebaseStorage.getInstance(); // STORAGE
View view = inflater.inflate(R.layout.fragment_list_selfies, container, false);
mRecyclerView = view.findViewById(R.id.selfies_recyclerview);
//initialize lists
final ArrayList<String> mPhotoIds = new ArrayList<>();
final ArrayList<Bitmap> mPhotoArrayList = new ArrayList<>();
//get names
DatabaseReference usersPhotoRef = mDatabaseIns.getReference().child("storeIDs");
usersPhotoRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final long ONE_MEGABYTE = 1024 * 1024;
for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
String image_name = uniqueKeySnapshot.getValue(String.class);
Log.i("IMAGE_NAME", image_name);
mPhotoIds.add(image_name);
String photoId = image_name + ".png";
StorageReference photoRef = mStorageIns.getReference().child("images/" + photoId);
photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mPhotoArrayList.add(bitmap);
Log.i("GET_BITMAP", String.valueOf(mPhotoArrayList.size()));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SELFIEFRAGMENT", e.getMessage());
}
});
Log.i("SELFIES", String.valueOf(mPhotoArrayList.size()));
}
SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);
mRecyclerView.setAdapter(selfiesAdapter);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("SELFIEFRAGMENT", databaseError.getMessage());
}
});
return view;
}
}
每次将照片添加到 mPhotoArrayList
时,您需要通过调用 notifyDataSetChanged()
让适配器知道其数据集已更改。然后适配器将重新呈现视图。
usersPhotoRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final long ONE_MEGABYTE = 1024 * 1024;
for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
String image_name = uniqueKeySnapshot.getValue(String.class);
Log.i("IMAGE_NAME", image_name);
mPhotoIds.add(image_name);
String photoId = image_name + ".png";
StorageReference photoRef = mStorageIns.getReference().child("images/" + photoId);
photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mPhotoArrayList.add(bitmap);
Log.i("GET_BITMAP", String.valueOf(mPhotoArrayList.size()));
selfiesAdapter.notifyDataSetChanged();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SELFIEFRAGMENT", e.getMessage());
}
});
Log.i("SELFIES", String.valueOf(mPhotoArrayList.size()));
}
要允许回调找到适配器,您需要将其声明为最终的:
final SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);
你好,我想从 Firebase 获取图像到内存中。当我调试我的代码时,它首先 returns 查看,然后返回 usersPhotoRef.addValueEventListener()
并将 mPhotoArrayList
的大小打印为 0。最后返回 photoRef.getBytes()#addOnSuccessListener()
并将位图放入 ArrayList .我认为应该更正我的代码,以便我可以将 mPhotoArrayList
传递给适配器。我该怎么做或必须进行哪些更改?
此代码的输出:
2019-06-30 15:01:00.130 12154-12154/com.newstrange.worklog E/RecyclerView: No adapter attached; skipping layout
2019-06-30 15:01:00.134 12154-12158/com.newstrange.worklog I/art: Do partial code cache collection, code=24KB, data=30KB
2019-06-30 15:01:00.135 12154-12158/com.newstrange.worklog I/art: After code cache collection, code=23KB, data=30KB
2019-06-30 15:01:00.135 12154-12158/com.newstrange.worklog I/art: Increasing code cache capacity to 128KB
2019-06-30 15:01:00.151 12154-12154/com.newstrange.worklog I/IMAGE_NAME: asdasd_giris_25.54.47_29.Mayıs.2019
2019-06-30 15:01:00.169 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.169 12154-12154/com.newstrange.worklog I/IMAGE_NAME: fdsafdas_giris_25.55.4_29.Mayıs.2019
2019-06-30 15:01:00.170 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.170 12154-12154/com.newstrange.worklog I/IMAGE_NAME: sdf_cikis_8.44.32_30.Mayıs.2019
2019-06-30 15:01:00.173 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.842 12154-12154/com.newstrange.worklog I/GET_BITMAP: 1
2019-06-30 15:01:00.846 12154-12154/com.newstrange.worklog I/GET_BITMAP: 2
2019-06-30 15:01:00.858 12154-12154/com.newstrange.worklog I/GET_BITMAP: 3
代码:
package com.newstrange.worklog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import java.io.IOException;
import java.util.ArrayList;
public class ListSelfiesFragment extends Fragment {
private FirebaseStorage mStorageIns;
private FirebaseDatabase mDatabaseIns;
private RecyclerView mRecyclerView;
private GridLayoutManager mLayoutManager;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mDatabaseIns = FirebaseDatabase.getInstance(); // DATABASE
mStorageIns = FirebaseStorage.getInstance(); // STORAGE
View view = inflater.inflate(R.layout.fragment_list_selfies, container, false);
mRecyclerView = view.findViewById(R.id.selfies_recyclerview);
//initialize lists
final ArrayList<String> mPhotoIds = new ArrayList<>();
final ArrayList<Bitmap> mPhotoArrayList = new ArrayList<>();
//get names
DatabaseReference usersPhotoRef = mDatabaseIns.getReference().child("storeIDs");
usersPhotoRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final long ONE_MEGABYTE = 1024 * 1024;
for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
String image_name = uniqueKeySnapshot.getValue(String.class);
Log.i("IMAGE_NAME", image_name);
mPhotoIds.add(image_name);
String photoId = image_name + ".png";
StorageReference photoRef = mStorageIns.getReference().child("images/" + photoId);
photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mPhotoArrayList.add(bitmap);
Log.i("GET_BITMAP", String.valueOf(mPhotoArrayList.size()));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SELFIEFRAGMENT", e.getMessage());
}
});
Log.i("SELFIES", String.valueOf(mPhotoArrayList.size()));
}
SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);
mRecyclerView.setAdapter(selfiesAdapter);
mLayoutManager = new GridLayoutManager(getActivity(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.i("SELFIEFRAGMENT", databaseError.getMessage());
}
});
return view;
}
}
每次将照片添加到 mPhotoArrayList
时,您需要通过调用 notifyDataSetChanged()
让适配器知道其数据集已更改。然后适配器将重新呈现视图。
usersPhotoRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final long ONE_MEGABYTE = 1024 * 1024;
for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {
String image_name = uniqueKeySnapshot.getValue(String.class);
Log.i("IMAGE_NAME", image_name);
mPhotoIds.add(image_name);
String photoId = image_name + ".png";
StorageReference photoRef = mStorageIns.getReference().child("images/" + photoId);
photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
mPhotoArrayList.add(bitmap);
Log.i("GET_BITMAP", String.valueOf(mPhotoArrayList.size()));
selfiesAdapter.notifyDataSetChanged();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.i("SELFIEFRAGMENT", e.getMessage());
}
});
Log.i("SELFIES", String.valueOf(mPhotoArrayList.size()));
}
要允许回调找到适配器,您需要将其声明为最终的:
final SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);