joe*_*joe 7 android firebase android-recyclerview firebase-realtime-database
我正在从 Firebase 数据库获取一些数据,并尝试RecyclerView用它填充适配器。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;
}
Run Code Online (Sandbox Code Playgroud)
}
这是我的活动布局,名为 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"/>
Run Code Online (Sandbox Code Playgroud)
这是我的适配器 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"/>
Run Code Online (Sandbox Code Playgroud)
所以它将包含两个水平的 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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
它应该获取 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) {
}
}
);
}
}
Run Code Online (Sandbox Code Playgroud)
所以在 for 循环完成后,Toast 显示正确数量的结果,调用适配器 setResults 方法,我收到正确数量的结果,这是一张图片:
但是在调用 notifyDataSetChanged() 后,屏幕只是闪烁,一切都是空白的......在 onBindViewHolder 中放置断点后无法访问它们......知道这里有什么问题吗?
您的代码中有几个问题。第一个是您正在调用该setResults()方法并将results列表传递给类内部ScoreAdapter而不是构造函数。为了解决这个问题,请将您的setResults()方法更改为构造函数,如下所示:
public ScoreAdapter(List<Result2> results) {
this.results.clear();
this.results.addAll(results);
notifyDataSetChanged();
}
Run Code Online (Sandbox Code Playgroud)
适配器还必须实例化并设置到RecyclerView回调内部。因此,要解决这个问题,只需移动以下代码行:
ScoreAdapter scoreAdapter = new ScoreAdapter(result);
recyclerView.setAdapter(scoreAdapter);
Run Code Online (Sandbox Code Playgroud)
就在以下行之后:
Toast.makeText(Results.this, String.valueOf(results.size()), Toast.LENGTH_SHORT).show();
Run Code Online (Sandbox Code Playgroud)
不要忘记注释这行代码:scoreAdapter.setResults(results);。
现在看,要实例化适配器,您需要将列表传递result给构造函数。
另外,也请不要忘记将其添加public no-argument constructor到您的Result2班级中。请参阅此处更多详细信息。
| 归档时间: |
|
| 查看次数: |
3812 次 |
| 最近记录: |