gpt4 book ai didi

android - 在 RecyclerView 适配器 notifyDataSetChanged() 之后未调用 onBindViewHolder

转载 作者:行者123 更新时间:2023-12-03 14:56:52 24 4
gpt4 key购买 nike

我正在从 Firebase 数据库中获取一些数据,我正在尝试填充 RecyclerView适配器。经过适配器的notifyDataSetChanged()被调用,屏幕闪烁,没有任何 react ,我什至无法在 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 的 Activity 布局,其中包含 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"/>

这是我的适配器 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"/>

所以它将包含两个水平 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 中设置文本(用户名和分数)

最后是我试图通知适配器的 Activity :
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 中设置断点后无法访问方法……知道这里有什么问题吗?

最佳答案

您的代码中有几个问题。第一个是您正在调用 setResults()方法并通过您的results列出您的ScoreAdapter类(class)和不是 给 build 者。为了解决这个问题,改变你的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类(class)。请看 here 更多细节。

关于android - 在 RecyclerView 适配器 notifyDataSetChanged() 之后未调用 onBindViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51741809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com