0

I am using the following Android code and that is working fine, there is no issue but I have a question.

How the variables name (such as: name,course,duration) converted as text for node creation as give in the following image:enter image description here

The XML code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!--        android:id="@+id/btn"-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="process"
        android:text="save to database"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.66"
        tools:ignore="MissingConstraints,OnClick" />

    <EditText
        android:id="@+id/t2"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="enter your name"
        android:textColor="#000"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.172"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/t3"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="enter your course"
        android:textColor="#000"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.522"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.298"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/t4"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="enter your duration"
        android:textColor="#000"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.504"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.429"
        tools:ignore="MissingConstraints" />

    <EditText
        android:id="@+id/t1"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:hint="enter your roll number"
        android:textColor="#000"
        android:textSize="25dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.495"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.041"
        tools:ignore="MissingConstraints" />


</androidx.constraintlayout.widget.ConstraintLayout>

Code of MainActivity.java:

package com.example.application_for_curd;    
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity {
    EditText t1,t2,t3,t4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void process(View v){
        t1 = findViewById(R.id.t1);
        t2 = findViewById(R.id.t2);
        t3 = findViewById(R.id.t3);
        t4 = findViewById(R.id.t4);
        String roll = t1.getText().toString().trim();
        String name = t2.getText().toString().trim();
        String course = t3.getText().toString().trim();
        String duration = t4.getText().toString().trim();

        dataholder obj = new dataholder(name,course,duration);
        FirebaseDatabase db = FirebaseDatabase.getInstance();
        DatabaseReference node = db.getReference("students");

        node.child(roll).setValue(obj);

        t1.setText("");
        t2.setText("");
        t3.setText("");
        t4.setText("");

        Toast.makeText(getApplicationContext(),"values inserted", Toast.LENGTH_LONG).show();
    }
}

code for dataholder.java:

package com.example.application_for_curd;    
public class dataholder {
    String name,course,duration;

    public dataholder(String name, String course, String duration) {
        this.name = name;
        this.course = course;
        this.duration = duration;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }
}

How the variables name going to be converted as the text for the creation of child nodes, that can be seen by an attached image which consists of child node named as name, course, duration.

1 Answers1

1

According to your last comment:

but name, course, duration these are the variables of string that are not a string

I understand that you want to have in your database values for your fields according to their corresponding data types. The "name" and "course" make sense to be of type String, however, the "duration" should be added as a number. You can save two different types of values into a property of type number, "long" values and "double" values. To achieve this, you should change the type of the property in your "dataholder" class to be of type "long" for example, and not String. The same rules apply in the case of a double value. So you should have the following declaration:

private String name, course;
private long duration;

And here is the corresponding getter and setter:

public long getDuration() {
    return duration;
}

public void setDuration(long duration) {
    this.duration = duration;
}

And your constructor should look like this:

public dataholder(String name, String course, long duration) {
    this.name = name;
    this.course = course;
    this.duration = duration;
}

Now to save the value for "duration" as a number and not as a String, please change the following line of code:

String duration = t4.getText().toString().trim();

To:

long duration = Long.parseLong(t4.getText().toString().trim());

With these changes, a value like "2 years" is not allowed anymore, as it's a String and not a number. So be aware to change all "duration" properties in the database to hold numbers and not String values.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The solution given by you is very good for multiple data type object. but I will say again that is not the answer to my question. I am going to explain my question again. Sir, I did not pass the string values as: String name = "name", course = "course", duration = "duration"; but the child node of /firebase/students/101/ named by "name", "course", "duration". How the firebase database getting the values ("name", "course", "duration") to create child node of /firebase/students/101/. – sanjay gupta Apr 20 '21 at 14:55
  • You are getting the values for "roll", "name", "course" and "duration" from the corresponding EditTexts t1, t2, t3, and t4 using ".getText()" method. Once you have that data you are creating a new object of your "dataholder" class using `dataholder obj = new dataholder(name,course,duration);`. This means that you pass those values to the "dataholder" constructor. Then you write that "obj" object to the database using `node.child(roll).setValue(obj);`. In this way, you end put having this database schema. Note that 101, 102 are the roll numbers that you are filling up. – Alex Mamo Apr 20 '21 at 15:05
  • I am very sorry, you may become irritated by the question. but t2, t3, and t4 using ".getText()" method get the values entered by the user and that will be stored in the variables (roll, name, course and duration). From the image roll ="101" name = "Sanjay Kumar Mishra" course = "PhD" and duration = "5 years". but how firebase getting the string to create child name as "course", "name" and "duration". The values of the child I am passing but child name I am not passing. – sanjay gupta Apr 20 '21 at 16:54
  • Oh, I see. Yes, you are only passing the values, which are roll ="101" name = "Sanjay Kumar Mishra" course = "PhD" and duration = "5 years". However, there is no need to pass "course", "name" and "duration", because Firebase takes those names from your class. The names of the fields in your class are translated/mapped directly to Firebase. Is it ok now? – Alex Mamo Apr 20 '21 at 17:55
  • ok fine, please answer my another simple question: https://stackoverflow.com/questions/67183775/how-do-we-get-name-and-number-of-child-nodes-of-root-node-using-android-code I know these are very simple but I am new for android code platform. gradually I am learning. – sanjay gupta Apr 20 '21 at 18:04
  • I'll take a look and if I'll know the answer, I'll write to you. – Alex Mamo Apr 20 '21 at 18:10