你们能帮我解决我的问题吗? firestore recyclerview代码在这里,我重写了5次,仍然一样,显示未显示任何内容,
第一次,它只加载了我打算显示的列表,所以我尝试改变布局,但是现在即使改变回到原始约束布局,它也什么都不显示
我尝试过的解决方案:
1)在onSart()上添加adapter.listen
2)firestore控制台规则更改为允许
3)将回收站的布局更改为wrap_content,使用协调器布局 以及与布局有关的所有内容
4)添加侦听器以查看当时是否存在数据,在onCreate()>数据确实存在,只是不在recyclerview上显示。
5)添加另一个活动进行身份验证,并将身份验证数据添加到数据库,>可以正常工作,但是仍然存在相同的问题
6)运行调试并使用Log.i()添加流检查>从未调用过我们的onBind和OnCreate适配器,但我找不到答案,
你们能帮我吗,非常感谢。
我使用datasnapshot检查数据是否存在,
我添加了:
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
}
此外,仍然无法正常工作。我用谷歌搜索了所有可能的解决方案, 到目前为止没有任何效果。
import javax.annotation.Nullable;
public class TestActivity extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("employee");
private EmployeeAdapter adapter;
private CollectionReference secondref = db.collection("second_employee");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Log.i("chekk","1");
//adapter.startListening();
secondref.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
String kk = documentSnapshot.getId();
Log.i("chekk uid we input:", kk);
}
}
});
notebookRef.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
for(QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots){
String kk = documentSnapshot.getId();
Log.i("chekk uid:", kk);
}
}
});
setupRecyclerView();
}
private void setupRecyclerView() {
Query query = notebookRef.orderBy("name").limit(2);
FirestoreRecyclerOptions<Nama> options = new FirestoreRecyclerOptions.Builder<Nama>()
.setQuery(query,Nama.class)
.build();
Log.i("chekk","2");
adapter = new EmployeeAdapter(options);
Log.i("chekk","3");
RecyclerView recyclerView = findViewById(R.id.recyclreID);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
Log.i("chekk","4");
// adapter.startListening();
recyclerView.setAdapter(adapter);
Log.i("chekk","5");
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
这是我的适配器代码:
public class EmployeeAdapter extends FirestoreRecyclerAdapter<Nama,EmployeeAdapter.InsiderHolder> {
public EmployeeAdapter(FirestoreRecyclerOptions<Nama> options) {
super(options);
Log.i("chekk","6");
}
@Override
protected void onBindViewHolder(@NonNull InsiderHolder holder, int position, @NonNull Nama model) {
Log.i("chekk","99");
holder.textView.setText(model.getName());
}
@NonNull
@Override
public InsiderHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//return null;
Log.i("chekk","88");
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.custom_layout_testt, viewGroup,false);
return new InsiderHolder(v);
}
public class InsiderHolder extends RecyclerView.ViewHolder {
TextView textView;
public InsiderHolder(@NonNull View itemView) {
super(itemView);
Log.i("chekk","77");
textView = itemView.findViewById(R.id.textviewcustomID);
}
}
}
对象的Java类:
public class Nama {
private String name;
public Nama() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
recyclerview的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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=".TestActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/recyclreID">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
用于自定义布局的xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textviewcustomID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginStart="176dp"
android:layout_marginTop="17dp"
android:layout_marginBottom="20dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
/>
</RelativeLayout>
</android.support.v7.widget.CardView>