[英]Android Firebase Recyclerview Adapter is not populating with data - mSnapshots is 0
Hi I am not sure where I am going wrong with my FirebaseRecycler - I am not getting errors in either the run or logcat.嗨,我不确定我的 FirebaseRecycler 哪里出了问题 - 我在运行或 logcat 中都没有收到错误。 I am just getting a blank activity and the debugger output shows that the FirebaseRecyclerOptions mSnapshot array is size 0. Any help would be great!
我刚刚得到一个空白活动,调试器 output 显示 FirebaseRecyclerOptions mSnapshot 数组的大小为 0。任何帮助都会很棒!
Here is how the firebase data looks: firebase data以下是 firebase 数据的外观: firebase 数据
logcat output: logcat image logcat output: logcat 图像
debuger output: debug image (breakpoint was set at last line of MainActivity onCreate method)调试器 output:调试图像(断点设置在 MainActivity onCreate 方法的最后一行)
MainActivity:主要活动:
public class MainActivity extends AppCompatActivity {
RecyclerView recview;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recview = (RecyclerView)findViewById(R.id.recview);
FirebaseRecyclerOptions<Podcast> options =
new FirebaseRecyclerOptions.Builder<Podcast>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("top10"),Podcast.class)
.build();
adapter = new Adapter(options);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,1,GridLayoutManager.VERTICAL,false);
recview.setLayoutManager(gridLayoutManager);
recview.setAdapter(adapter);
}
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}}
Here is the model class I am using called Podcasts:这是我正在使用的 model class 称为播客:
public class Podcast {
private String rss;
private String title;
private Integer order;
public Podcast(){
}
public Podcast(String url, String title, Integer order){
this.rss = url;
this.title = title;
this.order = order;
}
public String getTitle(){
return title;
}
public String getRss(){
return rss;
}
public Integer getOrder(){
return order;
}
public void setTitle(String title) {this.title = title;}
public void setRss(String rss){this.rss = rss;}
public void setOrder(Integer order){this.order = order;}}
Here is the Adapter class:这是适配器 class:
public class Adapter extends FirebaseRecyclerAdapter<Podcast,Adapter.myviewholder> {
LayoutInflater inflater;
//constructor
public Adapter(@NonNull FirebaseRecyclerOptions<Podcast> options){
super(options);
//inherits titles and url from the Podcast class
}
@Override
protected void onBindViewHolder(@NonNull myviewholder holder, int position, @NonNull Podcast model) {
holder.name.setText(model.getTitle()); //model is the podcast object
holder.url.setText(model.getRss());
holder.order.setText(model.getOrder());
//image with picasso next
}
@NonNull
@Override
public myviewholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_grid_layout,parent,false);
return new myviewholder(view);
}
public static class myviewholder extends RecyclerView.ViewHolder
{
TextView name;
TextView url;
TextView order;
public myviewholder(@NonNull View itemView)
{
super(itemView);
name = (TextView)itemView.findViewById(R.id.textView2);
url = (TextView)itemView.findViewById(R.id.url);
order = (TextView)itemView.findViewById(R.id.order);
}
}}
activity_main.xml activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="306dp"
tools:layout_editor_absoluteY="397dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
custom_gridview_layout.xml custom_gridview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="wrap_content"
android:layout_height="159dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="52dp"
app:cardPreventCornerOverlap="true"
app:contentPadding="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="328dp"
android:layout_height="127dp">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="43dp"
android:layout_marginLeft="43dp"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:layout_marginBottom="16dp"
android:paddingRight="10dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="132dp"
android:layout_marginLeft="132dp"
android:layout_marginEnd="138dp"
android:layout_marginRight="138dp"
android:layout_marginBottom="13dp"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="130dp"
android:layout_marginLeft="130dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="140dp"
android:layout_marginRight="140dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
In your database when you getReference it will get "testapi-232613-defa..." and calling child will bring the key "1wertHC2Qy..." and not "top10" that is why its unable to get any data在您的数据库中,当您获取参考时,它将获得“testapi-232613-defa ...”并且调用 child 将带来密钥“1wertHC2Qy ...”而不是“top10”,这就是它无法获取任何数据的原因
Hi it turns out that the issue was related to the Adapter needing a typecast for the Inflater:嗨,事实证明,该问题与需要为 Inflater 进行类型转换的适配器有关:
old: view = inflater.inflate(R.layout.custom_grid_layout,parent,false);
旧:
view = inflater.inflate(R.layout.custom_grid_layout,parent,false);
new: View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_grid_layout,parent,false)
新:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_grid_layout,parent,false)
Also I changed the database reference to:我还将数据库引用更改为:
DatabaseReference myRef = database.getReferenceFromUrl("https://testapi-232613-default-rtdb.firebaseio.com/1wertHC2QykkMJPdc6aPgV1pasPB8mFpkOyuuCRk1kE0/top10");
and used that as the args for the setQuery() function并将其用作 setQuery() function 的参数
You should change the database preference to您应该将数据库首选项更改为
DatabaseReference myRef = database.getReferenceFromUrl("https://testapi-232613-default-rtdb.firebaseio.com/1wertHC2QykkMJPdc6aPgV1pasPB8mFpkOyuuCRk1kE0/top10"); DatabaseReference myRef = database.getReferenceFromUrl("https://testapi-232613-default-rtdb.firebaseio.com/1wertHC2QykkMJPdc6aPgV1pasPB8mFpkOyuuCRk1kE0/top10");
Do check it out.一定要检查一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.