获取数据:
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();
}
});
}
保存数据:
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();
}
}
});
适配器:
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();
}
}
结果类
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;
}
}
我在RecyclerView和Firebase上工作了很长时间,但通常我会显示一个常量'name'和一个变量'value',但这里的名字和变量都不是我决定的......当用户完成测验时他的得分将显示在recyclerview中,但它只显示空白而没有任何错误...我不确定这是否是获取此类数据的正确方法...任何人都可以帮助我
数据库 - https://ibb.co/eMWkFJ
答案 0 :(得分:0)
import re
p = re.compile(ac_name + '\s*')
["" if not kv else kv[0].split('=')[-1].strip() for kv in [filter(lambda x: p.match(x), xs) for xs in datas]]
我希望这能完成你的工作。
答案 1 :(得分:0)
要解决此问题,首先需要先设置适配器,然后再通知它。所以请使用以下代码行:
mRecyclerView.setAdapter(mAdapter);
而不是:
mAdapter.notifyDataSetChanged();
只有在数据库中发生某些更改并且为了通知适配器时,才应使用最后一行代码,这必须在第一步设置。