FirebaseRecyclerAdapter - 只显示一个空白屏幕,编译时没有错误,滚动时没有错误。我已将Adapter().startListening();
添加到onStart,我已将数据添加到orders
子项中的firebase。我会很乐意帮助你。
这是我的FirebaseRecyclerAdapter,
private FirebaseRecyclerAdapter<Orders, OrderViewHolder> Adapter()
{
Query query = mDatabaseReference.limitToLast(50);
final FirebaseRecyclerOptions<Orders> options =
new FirebaseRecyclerOptions.Builder<Orders>()
.setQuery(query, Orders.class)
.build();
return new FirebaseRecyclerAdapter<Orders, OrderViewHolder>(options) {
@Override
public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.order_single_layout, parent, false);
return new OrderViewHolder(view);
}
@Override
protected void onBindViewHolder(OrderViewHolder ViewHolder, int position, Orders orders) {
ViewHolder.setOrderID(orders.getOrder_id());
ViewHolder.setProductName(orders.getProduct_name());
ViewHolder.setStatus(orders.getStatus());
}
};
}
我的观看者
public static class OrderViewHolder extends RecyclerView.ViewHolder
{
View mView;
private OrderViewHolder(View itemView)
{
super(itemView);
mView = itemView;
}
public void setOrderID(String orderID)
{
TextView orderIdTextView = (TextView) mView.findViewById(R.id.orderSingle_OrderIDTextView);
orderIdTextView.setText(orderID);
}
public void setStatus(String status)
{
TextView statusTextView = (TextView) mView.findViewById(R.id.orderSingle_StatusTextView);
statusTextView.setText(status);
}
public void setProductName(String productName)
{
TextView productNameTextView = (TextView) mView.findViewById(R.id.orderSingle_ProductNameTextView);
productNameTextView.setText(productName);
}
}
创建
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orders);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("orders");
adapter = Adapter();
mOrderList = (RecyclerView) findViewById(R.id.order_RecyclerView);
mLayoutManager = new LinearLayoutManager(this);
mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mOrderList.setHasFixedSize(true);
mOrderList.setLayoutManager(mLayoutManager);
mOrderList.setAdapter(adapter);
}
activityOrder.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.chat.OrdersActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/order_RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_alignParentEnd="true">
</android.support.v7.widget.RecyclerView>
用于打算进入RecyclerView的每封邮件的messageView
RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/orderSingle_ProductNameTextView"
style="@style/textView_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:textColor="#000000"
android:text="Product"
android:textSize="18sp" />
<TextView
android:id="@+id/orderSingle_StatusTextView"
style="@style/textView_style"
android:text="Status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/orderSingle_ProductNameTextView"
android:layout_below="@+id/orderSingle_ProductNameTextView"
android:layout_marginTop="12dp"
android:textColor="#000000"/>
<TextView
android:id="@+id/orderSingle_OrderIDTextView"
style="@style/textView_style"
android:text="order_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/orderSingle_ProductNameTextView"
android:layout_below="@+id/orderSingle_StatusTextView"
android:layout_marginTop="12dp"
android:textColor="#000000" />
答案 0 :(得分:1)
通过将.setLifecycleOwner(this)
添加到
final FirebaseRecyclerOptions<Orders> options =
new FirebaseRecyclerOptions.Builder<Orders>()
.setQuery(query, Orders.class)
.build();
不知道为什么现在有效,但确实如此