【问题标题】:Unable to populate RecyclerView adapter with Firebase data无法使用 Firebase 数据填充 RecyclerView 适配器
【发布时间】:2018-08-07 18:04:54
【问题描述】:

我正在尝试从我的 Firebase 数据库中获取数据,但由于某种原因它不起作用,所以我将尝试提供尽可能多的信息.​​.

这是数据库快照:

这是一个 POJO 类:

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;
}

}

这是我的活动布局,名为 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" />

如您所见,我希望有 2 个 TextView,它们的作用类似于选项卡,下面还有一个 RecyclerView,用于显示数据

这是我的适配器 ViewHolder 布局,名为 score_view_holder.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:id="@+id/username"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/score"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:textSize="14sp" />


</LinearLayout>

<View
    android:background="#4545"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"/>

所以它将包含两个水平 TextViews 和一个 View 作为下面的一行..

这是我的适配器:

public class ScoreAdapter extends RecyclerView.Adapter<ScoreAdapter.ScoreViewHolder> {

private List<Result2> results = new ArrayList<>();

public void setResults(List<Result2> results) {
    this.results.clear();
    this.results.addAll(results);
    notifyDataSetChanged();
}

@Override
public ScoreAdapter.ScoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.score_view_holder, parent, false);
    return new ScoreAdapter.ScoreViewHolder(view);
}

@Override
public void onBindViewHolder(ScoreAdapter.ScoreViewHolder holder, int position) {
    Result2 result = getResult(position);
    if (result != null) {
        holder.setUsername(result.getUserName() != null ? result.getUserName() : "-");
        holder.setScore(String.valueOf(result.getScore()));
    }
}

private Result2 getResult(int position) {
    return !results.isEmpty() ? results.get(position) : null;
}

@Override
public int getItemCount() {
    return results.size();
}

public class ScoreViewHolder extends RecyclerView.ViewHolder {

    private TextView username;
    private TextView score;

    public ScoreViewHolder(View itemView) {
        super(itemView);
        username = itemView.findViewById(R.id.username);
        score = itemView.findViewById(R.id.score);
    }

    public void setUsername(String username) {
        this.username.setText(username);
    }

    public void setScore(String score) {
        this.score.setText(score);
    }

}

}

它应该获取 Result2 对象的列表并在这两个 TextView 中设置文本(用户名和分数)

最后是我的活动,所有魔法都没有发生:)

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) {

        }
}

);
}
}

最有趣的是,Toast 显示正确数量的结果,但是当 scoreAdapter.setResults(results);被调用,屏幕闪烁并返回第一个屏幕......它与POJO类或onDataChange方法或主线程有关吗?

我尝试了调试,这里也很有趣。这是在 ScoreAdapter setResult 方法中的断点处捕获的:

我尝试在 onBindViewHolder 和 getResults() 中设置断点,但没有一个被调用:o

【问题讨论】:

  • 任何错误信息?
  • for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) 之后的结果是否正确包含分数和用户名?
  • 是的.. 我尝试在 for 循环中烘烤 result.getUserName() 并正确显示它们
  • 没有错误信息,应用程序甚至没有崩溃,只是一个空白屏幕

标签: android firebase firebase-realtime-database android-recyclerview


【解决方案1】:
if(result != null) {
     results.add(result);
}

看到这里你正在检查result != null。但 result 是 Results 类型的引用变量。所以它不会为空,它会包含一些值。

因此,请尝试删除该 if 语句并仅执行 -

results.add(result);

【讨论】:

  • 还是一样:/只是一个空白屏幕
猜你喜欢
  • 2015-09-15
  • 2016-11-12
  • 1970-01-01
  • 2021-06-10
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
相关资源
最近更新 更多