Android 无法使用Firebase数据填充RecyclerView适配器
我试图从我的Firebase数据库中获取数据,但由于某些原因它无法工作,因此我将尝试提供尽可能多的信息 这是数据库快照: 这是一个POJO类:Android 无法使用Firebase数据填充RecyclerView适配器,android,firebase,firebase-realtime-database,android-recyclerview,Android,Firebase,Firebase Realtime Database,Android Recyclerview,我试图从我的Firebase数据库中获取数据,但由于某些原因它无法工作,因此我将尝试提供尽可能多的信息 这是数据库快照: 这是一个POJO类: public class Result2 implements Serializable { private int score; private String userName; public Result2(int score, String userName) { this.score = score; this.userNa
public class Result2 implements Serializable {
private int score;
private String userName;
public Result2(int score, String userName) {
this.score = score;
this.userName = userName;
}
public int getScore() {
return score;
}
public String getUserName() {
return userName;
}
}
这是我的activitys布局,名为activity_results.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:weightSum="2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:textColor="#000"
android:textSize="16sp"
android:gravity="center"
android:text="USERNAME"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<TextView
android:textColor="#000"
android:textSize="16sp"
android:text="SCORE"
android:gravity="center"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:background="#000"
android:layout_width="match_parent"
android:layout_height="1dp"
/>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="10dp"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
我尝试在onBindViewHolder和getResults()中设置断点,但都没有被调用:o
if(result != null) {
results.add(result);
}
请参见此处,您正在检查结果!=空
。但result是Results类型的引用变量。所以它不会为null,它将包含一些值
因此,请尝试删除该if语句并仅执行以下操作-
results.add(result);
(DataSnapshot childSnapshot:DataSnapshot.getChildren())的结果是否正确包含分数和用户名?是。。我试着在for循环中烘烤result.getUserName(),结果显示正确无错误消息,应用程序甚至没有崩溃,只是一个空白屏幕还是一样:/只是一个空白屏幕
public class Results extends AppCompatActivity {
private DatabaseReference mDatabase;
private ScoreAdapter scoreAdapter;
private RecyclerView recyclerView;
private List<Result2> results = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
recyclerView = findViewById(R.id.recycler_view);
scoreAdapter = new ScoreAdapter();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(scoreAdapter);
mDatabase = FirebaseDatabase.getInstance().getReference();
loadResults();
}
private void loadResults() {
mDatabase.child("Users").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Result2 result = childSnapshot.getValue(Result2.class);
if(result != null) {
results.add(result);
}
}
Toast.makeText(Results.this, String.valueOf(results.size()), Toast.LENGTH_SHORT).show();
scoreAdapter.setResults(results);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
}
);
}
}
if(result != null) {
results.add(result);
}
results.add(result);