[英]How to display a variable 'name' and 'value' of firebase database in a RecyclerView
Fetching Data: 提取数据:
private void fetchResults() {
mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").child(id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot resultSnapshot: dataSnapshot.getChildren()) {
String user = resultSnapshot.getKey();
String score = resultSnapshot.getValue(String.class);
Results results = new Results(user, score);
resultsList.add(results);
}
mAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
Saving Data: 保存数据:
String name = RecieversName;
HashMap<String, String> userMap = new HashMap<>();
userMap.put(name, String.valueOf(mScore));
mRef.child("Users").child(RecieversId).child("Quiz").child("Results").child(UID).setValue(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Intent intent = new Intent(TakingQuiz.this, TakingQuizDone.class);
intent.putExtra("RecieversId",RecieversId);
intent.putExtra("Score", mScore.toString());
startActivity(intent);
finish();
}
}
});
Adapter: 适配器:
public class AdapterQuiz extends RecyclerView.Adapter<AdapterQuiz.ResultViewHolder>{
private FirebaseAuth mAuth;
private List<Results> mResultsList;
public AdapterQuiz(List<Results>mResultsList)
{
this.mResultsList = mResultsList;
}
public class ResultViewHolder extends RecyclerView.ViewHolder{
public TextView name;
public TextView score;
public ResultViewHolder(View view)
{
super(view);
name = (TextView)view.findViewById(R.id.name);
score = (TextView)view.findViewById(R.id.score);
}
}
@Override
public ResultViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_activity_results,parent,false);
mAuth = FirebaseAuth.getInstance();
return new ResultViewHolder(V);
}
@Override
public void onBindViewHolder(ResultViewHolder holder, int position) {
Results results = mResultsList.get(position);
holder.name.setText(results.getName());
holder.score.setText(results.getScore());
}
@Override
public int getItemCount() {
return mResultsList.size();
}
}
Results Class 结果类
public class Results {
private String Name;
private String Score;
public Results() {
}
public Results(String Name, String Score) {
this.Name = Name;
this.Score = Score;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getScore() {
return Score;
}
public void setScore(String score) {
Score = score;
}
} }
I have worked quite sometime on RecyclerView and Firebase but usually I would display a constant 'name' and a variable 'value' but here both name and variable is not decided by me... when the user finishes the quiz and his score will be displayed in the recyclerview but its just showing blank without any error... I'm not sure if this is the right way of fetching this kind of data... can anyone help me out please 我已经有一段时间在RecyclerView和Firebase上工作了,但是通常我会显示一个常量“名称”和一个变量“值”,但是这里的名称和变量都不由我决定...当用户完成测验时,他的分数将是显示在recyclerview中,但显示为空白而没有任何错误...我不确定这是否是获取此类数据的正确方法...任何人都可以帮帮我吗
Database - https://ibb.co/eMWkFJ 数据库-https: //ibb.co/eMWkFJ
Results results;
String name, score;
private void fetchResults() {
mDatabaseReference.child("Users").child(id).child("Quiz").child("Results").child(id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childDataSnapshot : dataSnapshot.getChildren()) {
name = childDataSnapshot.getKey().toString();
score = childDataSnapshot.child(name).getValue());
results = new Results(user, score);
resultsList.add(results);
mAdapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
I hope this will do your job. 我希望这能做好您的工作。
To solve this, you first need to set the adapter before notifying it for changes. 要解决此问题,您首先需要设置适配器,然后再通知其更改。 So please use the following line of code:
因此,请使用以下代码行:
mRecyclerView.setAdapter(mAdapter);
Instead of this: 代替这个:
mAdapter.notifyDataSetChanged();
This last line of code should be used only when some changes take place in your database and in order to notify the adapter, this must be set in the fist place. 仅当数据库中发生某些更改时才应使用最后一行代码,并且为了通知适配器,必须在第一位置进行设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.