Я создал базу данных Firebase в реальном времени. Я пытаюсь получить данные, используя представление переработчика, но получаю ошибку, как показано ниже
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.accentra.retrivedatabase, PID: 14196
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.accentra.retrivedatabase.User
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.1.0:418)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.1.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.1.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.1.0:203)
at com.accentra.retrivedatabase.UserDetailActivity$1.onDataChange(UserDetailActivity.java:77)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.1.0:55)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6662)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
I/Process: Sending signal. PID: 14196 SIG: 9
Process 14196 terminated.
Это мой код для Mainактивность
public class MainActivity extends AppCompatActivity {
Button b1;
public static final String Firebase_Server_URL = "https://retrive-database-8f64a.firebaseio.com/";
Firebase firebase;
DatabaseReference databaseReference;
// Root Database Name for Firebase Database.
public static final String Database_Path = "User";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(MainActivity.this);
firebase = new Firebase(Firebase_Server_URL);
databaseReference = FirebaseDatabase.getInstance().getReference(Database_Path);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it=new Intent(MainActivity.this,UserDetailActivity.class);
startActivity(it);
}
});
}
}
Это код для детальной активности пользователя
public class UserDetailActivity extends AppCompatActivity {
DatabaseReference databaseReference;
ProgressDialog progressDialog;
List<User> list = new ArrayList<>();
RecyclerView recyclerView;
Firebase firebase;
RecyclerView.Adapter adapter ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_detail);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(UserDetailActivity.this));
progressDialog = new ProgressDialog(UserDetailActivity.this);
progressDialog.setMessage("Loading Data from Firebase Database");
progressDialog.show();
databaseReference = FirebaseDatabase.getInstance().getReference(MainActivity.Database_Path);
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
User user = dataSnapshot.getValue(User.class);
list.add(user);
}
adapter = new RecyclerViewAdapter(UserDetailActivity.this,list);
recyclerView.setAdapter(adapter);
progressDialog.dismiss();
}
@Override
public void onCancelled(DatabaseError databaseError) {
progressDialog.dismiss();
}
});
}
}
Это код для Recycler View Adapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
List<User> MainImageUploadInfoList;
public RecyclerViewAdapter(Context context, List<User> TempList) {
this.MainImageUploadInfoList = TempList;
this.context = context;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
User user = MainImageUploadInfoList.get(position);
holder.FirstNameTextView.setText(user.getFirst_name());
holder.LastNameTextView.setText(user.getLast_name());
}
@Override
public int getItemCount() {
return MainImageUploadInfoList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView FirstNameTextView;
public TextView LastNameTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
FirstNameTextView = (TextView) itemView.findViewById(R.id.textView1);
LastNameTextView = (TextView) itemView.findViewById(R.id.textView2);
}
}
}
И это мой класс пользователя
public class User {
private String first_name;
private String last_name;
public User() {
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
}
Пожалуйста, помогите мне решить эту проблему