我想显示来自 Firebase 实时数据库的图像。但我认为它无法在MainUpload.java中运行loadPhoto()
Oncreate(){
mRecyclerView = findViewById(R.id.recyclerview_main_list);
int numberOfColumns = 3;
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getApplication(),numberOfColumns);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setHasFixedSize(true);
mItem = new ArrayList<>();
myAdapter = new MyAdapter(mItem);
mRecyclerView.setAdapter(myAdapter);
//clearAll();
loadPhoto();
DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(mRecyclerView.getContext(),mGridLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(dividerItemDecoration);
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
ItemObject itemObject = mItem.get(position);
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra("title",itemObject.getTitle());
intent.putExtra("photo",itemObject.getPhoto());
startActivity(intent);
}
}
loadPhoto()函数
private void loadPhoto() {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String key = FirebaseDatabase.getInstance().getInstance().getReference("users")
.child(currentUser.getUid()).child("Object").getKey();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users")
.child(currentUser.getUid()).child("Object").child(key);
if(databaseReference !=null) {
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
ItemObject itemObject = new ItemObject();
//여기서 에러
AESCoderAndriod aesCoderAndriod = new AESCoderAndriod();
try {
String En1 = snapshot.child("photo").getValue().toString();
byte[] En2 = En1.getBytes();
byte[] Dn1 = aesCoderAndriod.decrypt(seed, En2);
Bitmap Dn2 = byteArrayToBitmap(Dn1);
Uri Dn3 = getImageUri(getApplication(), Dn2);
itemObject.setPhoto(Dn3.toString());
itemObject.setTitle(snapshot.child("title").getValue().toString());
mItem.add(itemObject);
Log.e("test", Dn3.toString());
} catch (Exception e) {
e.printStackTrace();
}
// itemObject.setPhoto(snapshot.child("Photo").getValue().toString());
}
//myAdapter= new MyAdapter(getApplicationContext(),mItem);
// mRecyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}
ItemObject.java
public class ItemObject {
private String title;
private String Photo;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPhoto() {
return Photo;
}
public void setPhoto(String photo){
this.Photo=photo;
}
public ItemObject(String title, String photo) {
this.title = title;
this.Photo=photo;
}
public ItemObject() {}
}
将数据获取到自定义对象
我想让你知道的第一件事。当从 Firebase 检索数据并将它们放入自定义对象中时(在您的情况下为 ItemModel),您可以像这样轻松完成此操作:
ItemModel model = snapshot.getValue(ItemModel.class)
这是 IF 快照只有一个子项。如果快照中有更多孩子,您仍然可以这样做:
List<ItemModel> models = new ArrayList<>();
for (DataSnapshot tempModel : snapshot.getChildren()) {
models.add(tempModel.getValue(ItemModel.class));
}
但请确保 ItemModel 和 Firebase RDB 中的字段相同。此外,您还需要在 ItemModel.class 中有一个公共的空构造函数。就这么写:
public ItemModel() { //default constructor}
使用断点分析您的代码
这可能不是您问题的答案,但对于每个有问题要在这里解决的人来说都是如此。首先,如果某件事不起作用,请先不要放弃,然后来这里寻求帮助。有多种解决方案可以检查哪些内容不起作用!这就是这个过程。
在您的情况下,您不确定
loadPhoto()
是否正常工作,或者是否从 MainUpload.class 调用它。要检查这一点,在调试过程中您可以使用:
如果您使用 Log,只需编写 Log.d("LOAD PHOTO") 之类的内容。将其写在
loadPhoto()
函数的开头,您就会知道它进入了该函数。逻辑很简单,用起来就可以了。更好的是使用断点,您可以逐行查看代码发生了什么(F8 - 前进单行,F9 - 前进到下一个断点,如果没有断点则释放)。