CRUD Menggunakan Firebase Part 2
Langkah PART 1
Design Layout
Membuat layout dengan struktur
Masukan code xml pada dialog_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="5dp" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1"> <EditText android:id="@+id/dialogNIS" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="NIS" android:inputType="textPersonName" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_weight="1"> <EditText android:id="@+id/dialogNama" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Nama" android:inputType="textPersonName" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight=".5" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/btnUbah" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@color/colorAccent" android:clickable="true" android:text="UBAH" /> <Button android:id="@+id/btnHapus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@color/colorAccent" android:clickable="true" android:text="HAPUS" /> </LinearLayout> </LinearLayout>
Terakhir pada fragment_input.xml masukan code xml berikut
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:alpha="0.2" android:background="@color/colorAccent" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:orientation="vertical"> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/etNIS" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="NIS" android:inputType="textPersonName" android:padding="15dp" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <EditText android:id="@+id/etNama" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:hint="Nama" android:inputType="textPersonName" android:padding="15dp" android:singleLine="true" /> </android.support.design.widget.TextInputLayout> <Button android:id="@+id/btnSimpan" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="20dp" android:backgroundTint="@color/colorAccent" android:text="SIMPAN" /> </LinearLayout> </RelativeLayout>
Package Java
Membuat package dengan struktur
Dibagian InputFragment.java masukan sourcecode berikut
package com.codetr.tanwir.crudfirebase; import android.app.DatePickerDialog; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.Toast; import com.codetr.tanwir.crudfirebase.Constants.Config; import com.codetr.tanwir.crudfirebase.Model.Siswa; import com.firebase.client.Firebase; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale; import java.util.Objects; public class InputFragment extends Fragment{ private Button btnSimpan; private EditText etNis; private EditText etNama; Firebase fire; @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); Firebase.setAndroidContext(getContext()); fire = new Firebase(Config.FIREBASE_URL); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_input, container, false); btnSimpan = (Button) view.findViewById(R.id.btnSimpan); etNis = (EditText) view.findViewById(R.id.etNIS); etNama = (EditText) view.findViewById(R.id.etNama); btnSimpan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String nis = etNis.getText().toString().trim(); String nama = etNama.getText().toString().trim(); Siswa siswa = new Siswa(); if (!Objects.equals(nis, "") && !Objects.equals(nama, "")) { siswa.setNis(nis); siswa.setNama(nama); fire.child("Siswa").push().setValue(siswa); Toast.makeText(getActivity(), "Berhasil disimpan", Toast.LENGTH_SHORT).show(); } else Toast.makeText(getActivity(), "Masukan data anda", Toast.LENGTH_SHORT).show(); } }); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } }
Jika masih bingung bisa lihat penempatan code pada vidio berikut
Output Aplikasi
Sekian artikel dari saya semoga bermanfaat, jika ada yang ditanyakan comment saja di bawah.
bang Class SiswaFragment gak kelihatan
ReplyDeleteSource code nggk jelas, di blog nggk ada link Download source
ReplyDelete