I have tried to reorganised my database by adding a child called ("Reviews") that should include reviews added by the users but I keep getting a null displayed on the screen. I can only assume that the variables have not been saved into my modal class. I thought that my calling getReview() in my modal class should work:
DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); // getReference() is the root
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Information info = snapshot.getValue(Information.class);
assert info != null;
String txt = "Medical Clinic: " + info.getName() + " \n\n " + "Review: " + info.getReview() + "\n\nServices Provided: " + info.getDorevitch() + " : " + info.getSkin_Cancer() + " : " + info.getEar_Suctioning();
list.add(txt);
// list.add(snapshot.getValue().toString());
}
adapter.notifyDataSetChanged();
}
This is my modal class
enter code hepublic class Information { // variables have to match in firebase database or it will show null
private String Address;
private String Name;
private String Phone_No;
private String Suburb;
private String State;
private String Postcode;
private String Doctor;
private String Dorevitch;
private String Skin_Cancer;
private String Ear_Suctioning;
private String Reviews;
public Information() {
}
public Information(String address, String name, String phone_No, String suburb, String state, String postcode, String dorevitch, String skin_cancer, String ear_suctioning, String doctor, String review) {
Address = address;
Name = name;
Phone_No = phone_No;
Suburb = suburb;
State = state;
Postcode = postcode;
Doctor = doctor;
Dorevitch = dorevitch;
Skin_Cancer = skin_cancer;
Ear_Suctioning = ear_suctioning;
Reviews = review;
}
public String getDorevitch() {
return Dorevitch;
}
public void setDorevitch(String dorevitch) {
Dorevitch = dorevitch;
}
public String getSkin_Cancer() {
return Skin_Cancer;
}
public void setSkin_Cancer(String skin_Cancer) {
Skin_Cancer = skin_Cancer;
}
public String getEar_Suctioning() {
return Ear_Suctioning;
}
public void setEar_Suctioning(String ear_Suctioning) {
Ear_Suctioning = ear_Suctioning;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getDoctor() {
return Doctor;
}
public void setDoctor(String doctor) {
Doctor = doctor;
}
public String getSuburb() {
return Suburb;
}
public void setSuburb(String suburb) {
Suburb = suburb;
}
public String getReview() {
return Reviews;
}
public void setReview(String review) {
Reviews = review;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getPhone_No() {
return Phone_No;
}
public void setPhone_No(String phone_No) {
Phone_No = phone_No;
}
public String getPostcode() {
return Postcode;
}
public void setPostcode(String postcode) {
Postcode = postcode;
}
I can get it to work but don't know why it is only working if I pull out from the database by doing the following code and not using my modal class?
String reviews = String.valueOf(snapshot.child("Reviews").getValue());
String services = String.valueOf(snapshot.child("Services").getValue());
What is the difference between String.valueof and toString()?
Related
I am facing the following error com.google.firebase.database.DatabaseException: Can't convert an object of type java.lang.String to type
I have a problem with:
members = ds.getValue(Members.class);
listdata.add(members);
Help me solve this error.
reference = fbd.getReference().child("Members");
reference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
members = new Members();
members = ds.getValue(Members.class);
listdata.add(members);
}
recyclerView.setAdapter(adpter);
adpter.notifyDataSetChanged();
}
Here Model Class
package com.example.positivethinkers;
public class Members {
String member_name;
String father_name;
String mobile;
String email;
String nid;
String address;
String password;
String balance;
String notify;
String occupation;
String prolink;
String nick;
public Members() {
}
public Members(String member_name, String mobile, String email, String prolink) {
this.member_name = member_name;
this.mobile = mobile;
this.email = email;
this.prolink = prolink;
}
public Members(String member_name, String father_name, String mobile, String email, String nid, String address, String password, String balance, String notify, String occupation, String prolink, String nick) {
this.member_name = member_name;
this.father_name = father_name;
this.mobile = mobile;
this.email = email;
this.nid = nid;
this.address = address;
this.password = password;
this.balance = balance;
this.notify = notify;
this.occupation = occupation;
this.prolink = prolink;
this.nick = prolink;
}
public String getMember_name() {
return member_name;
}
public void setMember_name(String member_name) {
this.member_name = member_name;
}
public String getFather_name() {
return father_name;
}
public void setFather_name(String father_name) {
this.father_name = father_name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNid() {
return nid;
}
public void setNid(String nid) {
this.nid = nid;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getBalance() {
return balance;
}
public void setBalance(String balance) {
this.balance = balance;
}
public String getNotify() {
return notify;
}
public void setNotify(String notify) {
this.notify = notify;
}
public String getOccupation() {
return occupation;
}
public void setOccupation(String occupation) {
this.occupation = occupation;
}
public String getProlink() {
return prolink;
}
public void setProlink(String prolink) {
this.prolink = prolink;
}
public String getNick() {
return nick;
}
public void setNick(String nick) {
this.nick = nick;
}
}
Here My Database
You are getting the following error:
Can't convert an object of type java.lang.String to type com.example.positivethinkers.Members
Because you are looping through the dataSnapshot object which contains String properties and not Members objects. When you add a ChildEventListener on the Members node, onChildAdded() method is called for each and every child that exists within that node. So there is no need for an iteration. To solve this, simply remove that for loop like in the following lines of code:
reference = fbd.getReference().child("Members");
reference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot dataSnapshot, #Nullable String s) {
members = new Members();
members = ds.getValue(Members.class);
listdata.add(members);
adpter.notifyDataSetChanged();
}
});
Before that, create the adapter with an empty list and inside the callback just notify it about the changes.
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
members = new Members();
String name = ds.child("member_name").getValue(String.class);
String email = ds.child("email").getValue(String.class);
String mobile = ds.child("mobile").getValue(String.class);
String profile = ds.child("prolink").getValue(String.class);
Log.d("TAG", name + " / "+email+" / "+mobile+" / "+profile);
Members members = new Members(name, mobile, email, profile);
listdata.add(members);
}
}
How do I access the getter methods of a nested custom object? I am able to extract the methods which return Strings but not able to extract methods of a nested object.
My implementation is as follows:
public class DataExtraction {
public void showTheData(Object student) throws IOException {
Class classofStudent = student.getClass();
Method[] methodsOfStudent = classofStudent.getDeclaredMethods();
for(Method method:methodsOfStudent)
{
if(isGetType(method))
{
if(method.getReturnType()==String.class)
{
try(InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties"))
{
//InputStream is = new FileInputStream("ObjectFileReaderPrimitive.properties");
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty(method.getName()));
}
}
else
try(InputStream is = new FileInputStream("ObjectFileReaderNonPrimitive.properties"))
{
Class innerObjectClass = method.getReturnType().getClass();
Method[] methodsOfinnerObject = innerObjectClass.getDeclaredMethods();
for(Method methodofInnerClass : methodsOfinnerObject) {
if(isGetType(method))
{
Properties properties = new Properties();
properties.load(is);
System.out.println(properties.getProperty(methodofInnerClass.getName()));
}
}}
}
}}
private boolean isGetType(Method method) {
if(method.getName().startsWith("get"))
return true;
return false;
}
}
Where the student class is as follows-:
package com.sample;
public class Student {
private String id;
private String section;
private Address address;
public Student(String id, String section, Address address) {
super();
this.id = id;
this.section = section;
this.address = address;
}
public Student() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSection() {
return section;
}
public void setSection(String section) {
this.section = section;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
#Override
public String toString() {
return "Student [id=" + id + ", section=" + section + ", address=" + address + "]";
}
}
Address Object-:
package com.sample;
public class Address {
private String AddressLine1;
private String AddressLine2;
private String AddressLine3;
public Address(String addressLine1, String addressLine2, String addressLine3) {
super();
AddressLine1 = addressLine1;
AddressLine2 = addressLine2;
AddressLine3 = addressLine3;
}
public Address() {
super();
}
public String getAddressLine1() {
return AddressLine1;
}
public void setAddressLine1(String addressLine1) {
AddressLine1 = addressLine1;
}
public String getAddressLine2() {
return AddressLine2;
}
public void setAddressLine2(String addressLine2) {
AddressLine2 = addressLine2;
}
public String getAddressLine3() {
return AddressLine3;
}
public void setAddressLine3(String addressLine3) {
AddressLine3 = addressLine3;
}
#Override
public String toString() {
return "Address [AddressLine1=" + AddressLine1 + ", AddressLine2=" + AddressLine2 + ", AddressLine3="
+ AddressLine3 + "]";
}
}
Your problem is that you are not actually getting the correct class for your inner custom object.
Currently you are doing:
Class innerObjectClass = method.getReturnType().getClass();
This does not work because the method getReturnType is already returning the Class object of the return type. So what is happening is you are calling getClass() on a class object. This will return class java.lang.Class. You just need to remove the call to getClass:
Class innerObjectClass = method.getReturnType();
Here I have modified your code so that it prints all the getter objects in Student and Address
Class classofStudent = Student.class;
Method[] methodsOfStudent = classofStudent.getDeclaredMethods();
for (Method method : methodsOfStudent) {
if (isGetType(method)) {
if (method.getReturnType() == String.class) {
System.out.println(method.getName());
} else {
Class innerObjectClass = method.getReturnType();
Method[] methodsOfinnerObject = innerObjectClass.getDeclaredMethods();
for (Method methodofInnerClass : methodsOfinnerObject) {
if (isGetType(method)) {
System.out.println(methodofInnerClass.getName());
}
}
}
}
}
call.enqueue(new Callback<Resul>() {
#Override
public void onResponse(Call<Resul> call, Response<Resul> response) {
progressDialog.dismiss();
Log.d("response", "code = " + response.code());
Log.d("mvvvv","StudentId : "+response.body().toString());
String h=response.body().toString();
if("1".equals(h)){
Intent i = new Intent(Login.this,MainActivity.class);
startActivity(i);
}
else {
Toast.makeText(getApplicationContext()," user name not valid " , Toast.LENGTH_LONG).show();
}
}
Pojo class
package com.example.admin.myappl.Interface;
public class Resul {
private User_info user_info;
private Response Response;
public User_info getUser_info ()
{
return user_info;
}
public void setUser_info (User_info user_info)
{
this.user_info = user_info;
}
public Response getResponse ()
{
return Response;
}
public void setResponse (Response Response)
{
this.Response = Response;
}
public String toString() {
return ""+Response+"";
}
public class Response
{
private String response_message;
private String response_code;
public String getResponse_message ()
{
return response_message;
}
public void setResponse_message (String response_message)
{
this.response_message = response_message;
}
public String getResponse_code ()
{
return response_code;
}
public void setResponse_code (String response_code)
{
this.response_code = response_code;
}
#Override
public String toString()
{
return response_code;
}
}
public class User_info
{
private String profile_picture;
private String lastweek_command;
private String weight;
private String student_id;
private String push_notification_status;
private String id;
private String first_name;
private String updated_at;
private String height;
private String blood_group;
private String email;
private String address;
private String dob;
private String last_name;
private String gender;
private String general_command;
private String activity;
private String mobile_no;
public String getProfile_picture ()
{
return profile_picture;
}
public void setProfile_picture (String profile_picture)
{
this.profile_picture = profile_picture;
}
public String getLastweek_command ()
{
return lastweek_command;
}
public void setLastweek_command (String lastweek_command)
{
this.lastweek_command = lastweek_command;
}
public String getWeight ()
{
return weight;
}
public void setWeight (String weight)
{
this.weight = weight;
}
public String getStudent_id ()
{
return student_id;
}
public void setStudent_id (String student_id)
{
this.student_id = student_id;
}
public String getPush_notification_status ()
{
return push_notification_status;
}
public void setPush_notification_status (String push_notification_status)
{
this.push_notification_status = push_notification_status;
}
public String getId ()
{
return id;
}
public void setId (String id)
{
this.id = id;
}
public String getFirst_name ()
{
return first_name;
}
public void setFirst_name (String first_name)
{
this.first_name = first_name;
}
public String getUpdated_at ()
{
return updated_at;
}
public void setUpdated_at (String updated_at)
{
this.updated_at = updated_at;
}
public String getHeight ()
{
return height;
}
public void setHeight (String height)
{
this.height = height;
}
public String getBlood_group ()
{
return blood_group;
}
public void setBlood_group (String blood_group)
{
this.blood_group = blood_group;
}
public String getEmail ()
{
return email;
}
public void setEmail (String email)
{
this.email = email;
}
public String getAddress ()
{
return address;
}
public void setAddress (String address)
{
this.address = address;
}
public String getDob ()
{
return dob;
}
public void setDob (String dob)
{
this.dob = dob;
}
public String getLast_name ()
{
return last_name;
}
public void setLast_name (String last_name)
{
this.last_name = last_name;
}
public String getGender ()
{
return gender;
}
public void setGender (String gender)
{
this.gender = gender;
}
public String getGeneral_command ()
{
return general_command;
}
public void setGeneral_command (String general_command)
{
this.general_command = general_command;
}
public String getActivity ()
{
return activity;
}
public void setActivity (String activity)
{
this.activity = activity;
}
public String getMobile_no ()
{
return mobile_no;
}
public void setMobile_no (String mobile_no)
{
this.mobile_no = mobile_no;
}
#Override
public String toString()
{
return "ClassPojo [profile_picture = "+profile_picture+", lastweek_command = "+lastweek_command+", weight = "+weight+", student_id = "+student_id+", push_notification_status = "+push_notification_status+", id = "+id+", first_name = "+first_name+", updated_at = "+updated_at+", height = "+height+", blood_group = "+blood_group+", email = "+email+", address = "+address+", dob = "+dob+", last_name = "+last_name+", gender = "+gender+", general_command = "+general_command+", activity = "+activity+", mobile_no = "+mobile_no+"]";
}
}
}
Instead of
String h=response.body().toString();
Use
Resul result = response.body();
If condition should be like this
if("1".equals(result.getResponse().getResponse_code())
Use http://www.jsonschema2pojo.org/ to generate pojo class
If you are using gson for parsing the #SerializedName("your_key") ,
#Expose these 2 tags are required. Otherwise those values will be null
In my Spring boot application, I have collection of Todos and a collection of Courses. In the view of the application, I return the collection of courses and display whatever course I need. The Todos are stored as 1 list which represents all the current Todos. What I would like to do is return a list of Todos for each course. So when the view is opened, the application would display the the course plus the individual todo list for that course.
Is there a way I can use the existing code to incorporate the new functionality. I have created the front end logic and would like to keep that. My initial idea was to add the the course id to the Todo.java, but that did not work.
Todo.java
#Document(collection="todos")
public class Todo {
#Id
private String id;
#NotBlank
#Size(max=250)
#Indexed(unique=true)
private String title;
private Boolean completed = false;
private Date createdAt = new Date();
public Todo() {
super();
}
public Todo(String title) {
this.title = title;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Boolean getCompleted() {
return completed;
}
public void setCompleted(Boolean completed) {
this.completed = completed;
}
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
#Override
public String toString() {
return String.format(
"Todo[id=%s, title='%s', completed='%s']",
id, title, completed);
}
}
TodoRepository.java
#Repository
public interface TodoRepository extends MongoRepository<Todo, String> {
public List<Todo> findAll();
public Todo findOne(String id);
public Todo save(Todo todo);
public void delete(Todo todo);
}
Courses
#Document(collection = "courses")
public class Courses {
#Id
private String id;
private String name;
private String lecturer;
private String picture;
private String video;
private String description;
private String enroled;
public Courses(){}
public Courses(String name, String lecturer, String picture, String video, String description,String enroled) {
this.name = name;
this.lecturer = lecturer;
this.picture = picture;
this.video = video;
this.description = description;
this.enroled = enroled;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLecturer() {
return lecturer;
}
public void setLecturer(String lecturer) {
this.lecturer = lecturer;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEnroled() {
return enroled;
}
public void setEnroled(String enroled) {
this.enroled = enroled;
}
#Override
public String toString() {
return "Courses{" +
"id='" + id + "'" +
", name='" + name + "'" +
", lecturer='" + lecturer + "'" +
", description='" + description + "'" +
'}';
}
}
Here is the first file and there is another to follow, to really explain my question; my question starts in the public Account getbankAddress() method.
public class Bank {
String bankName;
int bankID;
Address address = new Address();
public Bank(){
bankName = "?";
bankID = 0;
}
public String getBankName(){
return bankName;
}
public int getBankID(){
return bankID;
}
public Address getBankAddress(){
// This is where I'm having trouble getting the bank address from the address class,
// How can I produce a result from the mutator method of setCity and setState methods?
return address;
}
public void setBankName(String bankName1){
bankName = bankName1;
}
public void setBankID(int ID){
bankID = ID;
}
public void setBankAddress(String aCity, String aState){
aCity = "city";
aState = "state";
}
public String toString(){
String str = ("\nBank name:\t\t" + bankName + "\nBank ID:\t\t" + bankID +
"\nBank address:\t\t" + bankAddress + "\n\n");
return str;
}
}
public class Address{
private String city;
private String state;
public Address(){
city = "?";
state = "?";
}
public String getCity(){
return city;
}
public String getState(){
return state;
}
public void setCity(String aCity){
aCity = city;
}
public void setState(String aState){
aState = state;
}
public String toString(){
String str1 = (city + "," + state);
return str1;
}
}
I think changing this:
public void setBankAddress(String aCity, String aState){
aCity = "city";
aState = "state";
}
to
public void setBankAddress(String aCity, String aState) {
address.setCity(aCity);
address.setState(aState);
}
should work.
++++++++++++++++
Also, change this:
public void setCity(String aCity){
aCity = city;
}
public void setState(String aState){
aState = state;
}
to
public void setCity(String aCity){
city = aCity;
}
public void setState(String aState){
state = aState;
}