【问题标题】:I can't get the items from the FirebaseRealtimeDB to recyclerview我无法从 FirebaseRealtimeDB 获取项目到 recyclerview
【发布时间】:2021-02-01 09:28:28
【问题描述】:

我想显示来自 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() {}

    }

【问题讨论】:

  • 首先,停止忽略错误。使用Log.d(TAG, databaseError.getMessage());。您是否在 logcat 中打印了一些内容?
  • "我认为它不能在 MainUpload.java 中运行 loadPhoto()" 为什么不呢?当您在调试器中运行此代码并逐行执行时,哪一行没有按照您的预期执行?

标签: android firebase-realtime-database android-recyclerview


【解决方案1】:

获取自定义对象的数据

我想让你知道的第一件事。当从 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 - 前进到下一个断点,如果没有断点则释放)。


不要忽略错误和异常

如果您有错误或异常,请在您的问题中提供。没有人会聪明地只查看代码并在不那么明显的情况下发现错误。例外能够帮助我们帮助您,也将节省您和我们的时间。欲了解更多信息,请查看:https://stackoverflow.com/help/how-to-ask


我希望这不会被否决,因为它不是问题的直接答案。随着问题变得更加清晰以回答用户的问题,这仍将被编辑。

【讨论】:

    猜你喜欢
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    相关资源
    最近更新 更多