RecyclerView Android Studio不显示Firebase数据库中的数据

时间:2018-12-30 04:02:57

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

我正在尝试让Recyclerview显示我的Firebase数据,但未显示,日志中没有错误。

public class Look extends AppCompatActivity {

    RecyclerView recyclerView;
    DatabaseReference databaseReference;
    FirebaseRecyclerOptions<Object> options;
    FirebaseRecyclerAdapter<Object,CategoryViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look);
        Intent intent = getIntent();
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);
        databaseReference = FirebaseDatabase.getInstance().getReference().child("message");

        options = new FirebaseRecyclerOptions.Builder<Object>()
                .setQuery(databaseReference, Object.class).build();
        adapter = new FirebaseRecyclerAdapter<Object, CategoryViewHolder>(options) {

            @Override
            protected void onBindViewHolder(CategoryViewHolder holder, int position, Object model) {


                holder.Type.setText(model.getType());
                holder.Color.setText(model.getColor());
            }

            @Override
            public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_category_item, parent, false);


                return new CategoryViewHolder(view);

            }
        };

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter.startListening();
        recyclerView.setAdapter(adapter);

    }
    @Override
    protected  void onStart(){
       super.onStart();
       if(adapter!=null)
           adapter.startListening();


    }
    @Override
    protected void onStop() {
        super.onStop();
        if (adapter != null)
            adapter.stopListening();

    }
    @Override
    protected void onResume(){
        super.onResume();
        if(adapter!=null)
            adapter.startListening();


    }

}

通常,代码在构建时没有错误,在运行时也没有错误。作为参考,我正在Android模拟器上运行它。仿真器缓冲,然后它不显示我的任何数据,然后停止。因此,我想知道我的错误是什么。

1 个答案:

答案 0 :(得分:0)

我的对象类不包含无参数构造函数,从而解决了该问题。