RecyclerView Adapter notifyDataSetChanged()

时间:2018-08-08 08:17:18

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

我从Firebase数据库中获取了一些数据,并试图用它填充RecyclerView适配器。调用Adapter的notifyDataSetChanged()之后,屏幕闪烁,但没有任何反应,我什至无法在onBindViewHolder中捕获断点。

这是我的代码:

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,其中包含RecyclerView

<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:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="USERNAME"
        android:textColor="#000"
        android:textSize="16sp"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="SCORE"
        android:textColor="#000"
        android:textSize="16sp"/>

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000"
    />

<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:clipToPadding="false"
                                        android:paddingTop="10dp"
                                        android:scrollbars="vertical"
                                        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

这是我的Adapters 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:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_marginEnd="10dp"
    android:layout_marginStart="10dp"
    android:background="#4545"/>

因此它将在下面包含两个水平的TextView和一个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对象的列表,并在这两个TextViews(用户名和得分)中设置文本

最后是我要从中通知适配器的活动:

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

                                                       }
                                                   }

    );
}
}

因此,在for循环完成之后,Toast显示正确数量的结果,调用了适配器setResults方法,在那里我收到了正确数量的结果,这是一张图片:

List of results inside setResults()

但是在调用notifyDataSetChanged()之后,屏幕只是闪烁并且所有内容都是空白...在它们上设置了断点后,无法访问onBindViewHolder中的方法。.知道这里有什么问题吗?

1 个答案:

答案 0 :(得分:0)

您的代码中有几个问题。第一个是您正在调用setResults()方法,并将results类中的ScoreAdapter列表传递给构造函数,并且 not 传递给构造函数。为了解决这个问题,请将您的setResults()方法更改为如下所示的构造函数:

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

还必须实例化适配器并将其设置为回调中的RecyclerView。因此,要解决此问题,只需移动以下代码行:

ScoreAdapter scoreAdapter = new ScoreAdapter(result);
recyclerView.setAdapter(scoreAdapter);

在以下行之后:

Toast.makeText(Results.this, String.valueOf(results.size()), Toast.LENGTH_SHORT).show();

也不要忘记注释以下代码行:scoreAdapter.setResults(results);

现在看,要实例化适配器,您需要将result列表传递给构造函数。

也请不要忘记将public no-argument constructor添加到您的Result2类中。请参阅 here 更多详细信息。