Android,Firebase Recycler Adapter不查询数据库

时间:2018-12-05 17:32:23

标签: android firebase android-recyclerview recycler-adapter

我无法通过FirebaseREcyclerAdapter填充Recycler View。

在活动控制器类中:

private RecyclerView hw_list;

以及onCreate()方法中与Firebase的连接:

hw_ref = FirebaseDatabase.getInstance().getReference().child("hardwares");

我通过onStart()方法制作适配器:

@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerOptions<Hardware> options =
            new FirebaseRecyclerOptions.Builder<Hardware>().setQuery(hw_ref, Hardware.class).build();

    FirebaseRecyclerAdapter<Hardware, AddHardwareViewHolder> adapter = new FirebaseRecyclerAdapter<Hardware, AddHardwareViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull AddHardwareViewHolder holder, int position, @NonNull Hardware model) {
            holder.tv_hwTimeStamp.setText("Registered on" + model.getTimestamp());
        }

        @NonNull
        @Override
        public AddHardwareViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.hardware_row, viewGroup, false);
            AddHardwareViewHolder addHardwareViewHolder = new AddHardwareViewHolder(view);

            return addHardwareViewHolder;
        }
    };

    hw_list.setAdapter(adapter);
    adapter.startListening();
}

在“活动”中,我定义了视图持有人:

    public static class AddHardwareViewHolder extends RecyclerView.ViewHolder {
    TextView tv_hwTimeStamp;
    View mView;

    public AddHardwareViewHolder(@NonNull View itemView) {
        super(itemView);
        mView = itemView;
        tv_hwTimeStamp = mView.findViewById(R.id.tv_register_time);
    }
}

Here's what the database looks like

以及模型类“硬件”:

public class Hardware {

public String Timestamp;
public String pid;

public Hardware() {}

public Hardware(String Timestamp, String pid) {
    this.Timestamp = Timestamp;
    this.pid = pid;

}

public String getTimestamp() {
    return Timestamp;
}

public void setTimestamp(String Timestamp) {
    this.Timestamp = Timestamp;
}

public String getPid() {
    return pid;
}

public void setPid(String pid) {
    this.pid = pid;
}

}

该代码没有错误和警告,但没有任何显示。我在制作适配器和适配器选项的OnStart方法中添加了断点。它们工作正常,代码运行到适配器开始侦听的位置。

但是在那之后,这些覆盖方法将永远无法运行。看来根本没有从Firebase获取数据。但是我App的其他需要Firebase数据的部分都能正常工作。

        FirebaseRecyclerAdapter<Hardware, AddHardwareViewHolder> adapter = new FirebaseRecyclerAdapter<Hardware, AddHardwareViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull AddHardwareViewHolder holder, int position, @NonNull Hardware model) {
            holder.tv_hwTimeStamp.setText("Registered on" + model.getTimestamp());
        }

        @NonNull
        @Override
        public AddHardwareViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.hardware_row, viewGroup, false);
            AddHardwareViewHolder addHardwareViewHolder = new AddHardwareViewHolder(view);

            return addHardwareViewHolder;
        }
    };

因此,“ Recycler View”中没有任何物品,logcat会给我这个:

2018-12-05 11:47:50.282 17420-17420/com.apocalyvec.uitest3 E/RecyclerView: No layout manager attached; skipping layout

在这一点上我真的迷茫了。也许有些配置或初始化我做错了。

1 个答案:

答案 0 :(得分:0)

2018-12-05 11:47:50.282 17420-17420/com.apocalyvec.uitest3 E/RecyclerView: No layout manager attached; skipping layout

您没有将任何布局管理器附加到recyclerView。因此,修改后的代码将是:-

hw_list.setAdapter(adapter);
hw_list.setLayoutManager(new LinearLayoutManager(context));
adapter.startListening();

这里的上下文是活动上下文。 您还可以使用GridLayoutManager等。