数据未能在火力地堡回收站查看
问题描述:
显示我已经按照几个不同的文档齐心协力我的第一火力地堡回收站查看:数据未能在火力地堡回收站查看
两个主要的:
我在Firebase Recycler View中使用了一个Message对象(包含在下面)。由于某些原因数据不显示。从我的日志输出来看,适配器中没有任何东西被调用。下面是我的onCreate()坐镇活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_details);
RecyclerView mRecyclerView;
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
Query query = myMessagesRef.child(RoomID)
.orderByKey()
.limitToLast(50);
FirebaseRecyclerOptions<Message> options =
new FirebaseRecyclerOptions.Builder<Message>()
.setQuery(query, Message.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Message, ChatHolder>(options) {
@Override
public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_item, parent, false);
return new ChatHolder(view);
}
@Override
protected void onBindViewHolder(ChatHolder holder, int position, Message model) {
//super.onBindViewHolder(holder, position);
Log.w(TAG, "Some Info on messages 2 " + model.MessageText);
holder.bindChat(model);
}
};
mRecyclerView.setAdapter(adapter);
}
这里是我的ViewHolder:
static class ChatHolder extends RecyclerView.ViewHolder{
TextView mtext;
//Context mContext;
public ChatHolder(View v) {
super(v);
mtext = (TextView) v.findViewById(com.example.administrationuser.piclo.R.id.textView13);
}
public void bindChat(Message mess){
mtext.setText(mess.MessageText);
}
}
这里是我传递这两个火力地堡和适配器我Message对象(数据我输入同步到火力地堡没有问题,我的查询火力得到正确格式的数据):
public static class Message {
public String UserID;
public String UserName;
public String MessageText;
public Message() {} // Needed for Firebase
public Message(String UserID, String UserName, String MessageText) {
this.UserID = UserID;
this.UserName = UserName;
this.MessageText = MessageText;
}
}
这里是我的活动布局XML(名称为:activity_message_details.xml): (注意靠近底部的回收视图):
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrationuser.piclo.MessageDetails">
<LinearLayout
android:layout_width="395dp"
android:layout_height="643dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:onClick="onClick"/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
最后,这里是我的视图布局XML(名称为:recycler_item.xml):
根据<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</FrameLayout >
答
存在需要修复两件事情。
1)加入adapter.startListening();
(如Elvin No Matter所述)。在我的例子中,我在接近我的onCreate结尾处添加了这个,就在mRecyclerView.setAdapter(adapter);
的下方。
2)将我的视图布局XML包含在<android.support.v7.widget.LinearLayoutCompat>
中。看到我的新视图持有人,与其他一些无关的按钮更改:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<TextView
android:id="@+id/textView13"
android:layout_width="285dp"
android:layout_height="24dp"
android:text="TextView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_editor_absoluteX="-6dp" />
<TextView
android:id="@+id/textView14"
android:layout_width="123dp"
android:layout_height="24dp"
android:layout_alignParentTop="true"
android:layout_marginRight="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="@+id/textView13"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="154dp" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.LinearLayoutCompat>
答
FirestoreRecyclerAdapter使用快照侦听器来监视对Firestore查询的更改 。要开始监听数据,请拨打 startListening()方法。您可能需要在onStart() 方法中调用此方法。在调用startListening()之前,确保已完成对 读取数据所需的任何验证,否则查询将失败。
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
和
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}