Currently, I am trying to get an activity to work where a class is listed and on click, it returns Users' names and grades in a separate activity with a Listview. When, the user signs up for the class their uid is saved under the class. First, I am retrieving the uids' listed under the class name, which works.After that, I am using the uid to navigate back to the user information and retrieve the name and grade and combine it into one variable, so I can show it in a Listview. Unfortunately, when I run the app I get the error:
Can't convert object of type java.lang.String to type com.samuelford48gmail.thsconnect.User
When I ran the app in debug mode, it said that the name and grade were null. Also, when I used system.out.println to show the datasnapshot it only returned the Classes under the users' uid. I think part of the problem could be my structure in Firebase.
Here is my Firebase structure:
{
"Science" : {
"-LhrSfhNaQX1ZiNToeYQ" : {
"Students" : {
"-LhzXqvBPJl-gvN7bZ6c" : "9TKqqNqge5cKpFgjHByAFpZ3iRD2"
},
"date_clasname" : "dd",
"room_number" : "dd",
"teacher" : "dd",
"uid" : "-LhrSfhNaQX1ZiNToeYQ"
},
"-LhrSfwx3nKd3DhRNJRp" : {
"date_clasname" : "dd",
"room_number" : "dd",
"teacher" : "dd",
"uid" : "-LhrSfwx3nKd3DhRNJRp"
},
"-LhrSg-4YJ52a1Zj9LoK" : {
"Students" : {
"-LhrfCKXsrjDWC4dJU8H" : "9TKqqNqge5cKpFgjHByAFpZ3iRD2"
},
"date_clasname" : "dd",
"room_number" : "dd",
"teacher" : "dd",
"uid" : "-LhrSg-4YJ52a1Zj9LoK"
}
},
"Users" : {
"9TKqqNqge5cKpFgjHByAFpZ3iRD2" : {
"Classes" : {
"-LhrfCKVETBZrgOIrCLR" : {
"date_clasname" : "dd",
"room_number" : "dd",
"teacher" : "dd"
},
"-LhzWYnqD-qi8v-MFWKF" : {
"date_clasname" : "dd",
"room_number" : "dd",
"teacher" : "dd"
},
"-LhzXqv0RZMts7Df1Ehg" : {
"date_clasname" : "dd",
"room_number" : "dd",
"teacher" : "dd"
}
},
"email" : "[email protected]",
"grade" : "11",
"name" : "Samuel"
},
"NnJlSVeqGXhJ2Nab2bhjr0HIpku2" : {
"Classes" : {
"-LhNEZI73xZWbu8tb4ZL" : {
"date_clasname" : "March 5",
"room_number" : "101",
"teacher" : "pence"
},
"-LhNFH_LB8PGKKOML-Ta" : {
"date_clasname" : "d",
"room_number" : "",
"teacher" : "s"
},
"-LhNFHjC0sYKaXd_OcX2" : {
"date_clasname" : "d",
"room_number" : "",
"teacher" : "s"
},
"-LhrbM1htXYd0CxZiKMZ" : {
"date_clasname" : "dd",
"room_number" : "dd",
"teacher" : "dd"
},
"email" : "[email protected]",
"grade" : "12",
"name" : "Samuel Ford"
},
}
}
}
Here is the code for the activity with the Listview that is attempting to get the user's name and grade:
public class admin_show_students_uid extends AppCompatActivity {
private FirebaseDatabase database;
private DatabaseReference myRef;
String user_info = null;
String name = null;
ListView lv;
ArrayList<String> list=new ArrayList<>();
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.admin_show_students_uid);
lv = (ListView) findViewById(R.id.lv_science_students);
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list);
lv.setAdapter(adapter);
lv.setStackFromBottom(true);
final String post_key = getIntent().getStringExtra("post_key");
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Science").child(post_key).child("Students");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {
//name gets the uid saved
name = dataSnapshot.getValue(String.class);
myRef = database.getReference("Users").child(name);
getusersinfo();
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void getusersinfo(){
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot2 : dataSnapshot.getChildren()) {
User user = dataSnapshot2.getValue(User.class);
String name = user.getName();
String grade = user.getGrade();
user_info = ("Name " + name + "Grade" + grade);
list.add(user_info);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Thank you in advance and let me know if you have any questions about my code!