我正在尝试通过从用户中检索某些分数并将其显示在recyclerview中来建立排行榜。但是在检索时,出现以下错误。
com.google.firebase.database.DatabaseException:无法转换对象 类型为java.lang.Long的类型 com.cryptocoinpioneers.mybitcoinfaucet.ScoresPOJO
我该如何解决?
下面是我的代码?
LeaderBoard.java
public class LeaderBoard extends Fragment {
public LeaderBoard() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_leader_board, container, false);
recyclerView = view.findViewById(R.id.users_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setHasFixedSize(true);
listData = new ArrayList<>();
adapter = new LeaderBoard.MyAdapter(listData);
adapter.setHasStableIds(true);
GetDataFirebase();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading Data...");
progressDialog.show();
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
final FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
return view;
}
void GetDataFirebase() {
FirebaseUser current_user = FirebaseAuth.getInstance().getCurrentUser();
String uid = current_user.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child("LeaderBoard").child(uid);
Query MostCoins = databaseReference.orderByChild("coins");
MostCoins.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
listData.add(dataSnapshot);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public class MyAdapter extends RecyclerView.Adapter<LeaderBoard.MyAdapter.ViewHolder> {
List<DataSnapshot> list;
public MyAdapter(List<DataSnapshot> List) {
this.list = List;
}
@Override
public void onBindViewHolder(final LeaderBoard.MyAdapter.ViewHolder holder, final int position) {
DataSnapshot studentSnapshot = list.get(position);
ScoresPOJO students = studentSnapshot.getValue(ScoresPOJO.class);
String list_user_id = studentSnapshot.getKey();
holder.news_topic.setText(students.getUsername());
holder.news_body.setText(String.valueOf(students.getCoins()));
}
@NonNull
@Override
public LeaderBoard.MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.scores_list_layout, parent, false);
return new LeaderBoard.MyAdapter.ViewHolder(view);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView news_topic, news_body;
public ViewHolder(View itemView) {
super(itemView);
news_topic = itemView.findViewById(R.id.username);
news_body = itemView.findViewById(R.id.coins);
}
}
@Override
public int getItemCount() {
return list.size();
}
}
}
我的数据POJO
public class ScoresPOJO {
private String username;
private Long coins;
public ScoresPOJO() {
}
public ScoresPOJO(String username, Long coins) {
this.username = username;
this.coins = coins;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getCoins() {
return coins;
}
public void setCoins(Long coins) {
this.coins = coins;
}
}