Lompat ke konten Lompat ke sidebar Lompat ke footer

Widget HTML #1

Cara Membuat Input Insert Firebase Realtime Android Studio Java Programmatically

Klik Tools > Firebase > Realtime Firebase > Get started with realtime database

Add The Realtime database to your app

klik tombolnya sampai tertulis Dependencies set up correctly

jika tetap tidak bisa, gunakan cara manual seperti ini

pastikan sudah implementasi dibawah ini pada gradle

implementation platform('com.google.firebase:firebase-bom:30.4.1')
implementation 'com.google.firebase:firebase-database:16.0.5'
implementation 'com.google.firebase:firebase-database:20.1.0'

 jika sudah lanjut 

jika anda menggunakan sdk 33 (direkomendasikan) letakkan implementasi berikut

implementation "androidx.activity:activity:1.6.0-alpha05"
compileSdk 33

defaultConfig {
applicationId "com.ranairu.creation"
minSdk 29
targetSdk 33

compilesdk 33 

targetsdk 33


daftar realtime database di 

1. masuk ke firebase

2. klik build > realtime database

3. Create Database > opilih server singapur cuy biar kenceng > next

4. start in test mode > enable


selesai. lanjut cek apakah di file google-service-json sudah ada script ini ?

"firebase_url": "https://ranairu-creation-default-rtdb.asia-southeast1.firebasedatabase.app",

jika belum silahkan tambahkan totalnya seperti ini

 

"project_info": {
"project_number": "89251172577",
"firebase_url": "https://ranairu-creation-default-rtdb.asia-southeast1.firebasedatabase.app",
"project_id": "ranairu-creation",
"storage_bucket": "ranairu-creation.appspot.com"
},

catatan :

firebase_url diambil dari sini, silahkan sesuaikan

Di manifest tambahkan kode ini

<application
android:enableOnBackInvokedCallback="true"

android:enableOnBackInvokedCallback="true"


buat java baru dengan nama EmployeeInfo
public class EmployeeInfo {

// string variable for
// storing employee name.
private String employeeName;

// string variable for storing
// employee contact number
private String employeeContactNumber;

// string variable for storing
// employee address.
private String employeeAddress;

// an empty constructor is
// required when using
// Firebase Realtime Database.
public EmployeeInfo() {

}

// created getter and setter methods
// for all our variables.
public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public String getEmployeeContactNumber() {
return employeeContactNumber;
}

public void setEmployeeContactNumber(String employeeContactNumber) {
this.employeeContactNumber = employeeContactNumber;
}

public String getEmployeeAddress() {
return employeeAddress;
}

public void setEmployeeAddress(String employeeAddress) {
this.employeeAddress = employeeAddress;
}
}

buat empty activity baru 

Java nya


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class Favorite_Akun extends AppCompatActivity {

// creating variables for
// EditText and buttons.
private EditText employeeNameEdt, employeePhoneEdt, employeeAddressEdt;
private Button sendDatabtn;

// creating a variable for our
// Firebase Database.
FirebaseDatabase firebaseDatabase;

// creating a variable for our Database
// Reference for Firebase.
DatabaseReference databaseReference;

// creating a variable for
// our object class
EmployeeInfo employeeInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite_akun);

// initializing our edittext and button
employeeNameEdt = findViewById(R.id.idEdtEmployeeName);
employeePhoneEdt = findViewById(R.id.idEdtEmployeePhoneNumber);
employeeAddressEdt = findViewById(R.id.idEdtEmployeeAddress);

// below line is used to get the
// instance of our FIrebase database.
firebaseDatabase = FirebaseDatabase.getInstance();

// below line is used to get reference for our database.
databaseReference = firebaseDatabase.getReference("EmployeeInfo");

// initializing our object
// class variable.
employeeInfo = new EmployeeInfo();

sendDatabtn = findViewById(R.id.idBtnSendData);

// adding on click listener for our button.
sendDatabtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

// getting text from our edittext fields.
String name = employeeNameEdt.getText().toString();
String phone = employeePhoneEdt.getText().toString();
String address = employeeAddressEdt.getText().toString();

// below line is for checking whether the
// edittext fields are empty or not.
if (TextUtils.isEmpty(name) && TextUtils.isEmpty(phone) && TextUtils.isEmpty(address)) {
// if the text fields are empty
// then show the below message.
Toast.makeText(Favorite_Akun.this, "Please add some data.", Toast.LENGTH_SHORT).show();
} else {
// else call the method to add
// data to our database.
addDatatoFirebase(name, phone, address);
}
}
});
}

private void addDatatoFirebase(String name, String phone, String address) {
// below 3 lines of code is used to set
// data in our object class.
employeeInfo.setEmployeeName(name);
employeeInfo.setEmployeeContactNumber(phone);
employeeInfo.setEmployeeAddress(address);

// we are use add value event listener method
// which is called with database reference.
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// inside the method of on Data change we are setting
// our object class to our database reference.
// data base reference will sends data to firebase.
databaseReference.setValue(employeeInfo);

// after adding this data we are showing toast message.
Toast.makeText(Favorite_Akun.this, "data added", Toast.LENGTH_SHORT).show();
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
// if the data is not added or it is cancelled then
// we are displaying a failure toast message.
Toast.makeText(Favorite_Akun.this, "Fail to add data " + error, Toast.LENGTH_SHORT).show();
}
});
}
}

Layout XML nya

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!--EditText for adding employee name-->
<EditText
android:id="@+id/idEdtEmployeeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:hint="Enter Employee Name"
android:importantForAutofill="no"
android:inputType="textPersonName" />

<!--EditText for adding employee phone-->
<EditText
android:id="@+id/idEdtEmployeePhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtEmployeeName"
android:layout_margin="10dp"
android:hint="Enter employee phone number"
android:importantForAutofill="no"
android:inputType="phone" />

<!--EditText for adding employee address-->
<EditText
android:id="@+id/idEdtEmployeeAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtEmployeePhoneNumber"
android:layout_margin="10dp"
android:hint="Enter employee address"
android:inputType="textPostalAddress" />

<!--Button for adding data to Firebase-->
<Button
android:id="@+id/idBtnSendData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtEmployeeAddress"
android:layout_margin="10dp"
android:text="Add employee details"
android:textAllCaps="false" />

</RelativeLayout>

selesai. run aja dan simpan kolomnya lalu coba cek apakah di database realtime sudah terisi seperti ini ?

 

Jika sudah berarti berhasil

Posting Komentar untuk "Cara Membuat Input Insert Firebase Realtime Android Studio Java Programmatically"

close