Created
October 28, 2019 10:21
-
-
Save dwijonarko/0380213ff340b1189f550c0228c248d3 to your computer and use it in GitHub Desktop.
Firebase - 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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:orientation="vertical" | |
tools:context=".MainActivity"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:textAlignment="center" | |
android:textSize="24dp" | |
android:text="Firebase A" | |
/> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:hint="Todo" | |
android:id="@+id/todotext" | |
/> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:onClick="addData" | |
android:text="Add" | |
/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vokasi.firebase_a; | |
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.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 MainActivity extends AppCompatActivity { | |
EditText todotitle; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
todotitle = (EditText) findViewById(R.id.todotext); | |
final FirebaseDatabase database = FirebaseDatabase.getInstance(); | |
DatabaseReference todoRef = database.getReference("connection"); | |
todoRef.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
String value = dataSnapshot.getValue(String.class); | |
Toast.makeText(MainActivity.this, value, Toast.LENGTH_LONG).show(); | |
} | |
@Override | |
public void onCancelled(DatabaseError error) { | |
Toast.makeText(MainActivity.this, error.toException().toString(), Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
public void addData(View view){ | |
final FirebaseDatabase database = FirebaseDatabase.getInstance(); | |
DatabaseReference todoRef = database.getReference("todos"); | |
String title = todotitle.getText().toString(); | |
todoRef.push().setValue(new Todo(title,false)); | |
todotitle.setText(""); | |
Toast.makeText(this, "Data sudah tersimpan", Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vokasi.firebase_a; | |
public class Todo { | |
String title; | |
boolean completed; | |
public Todo(String title, boolean completed) { | |
this.title = title; | |
this.completed = completed; | |
} | |
public String getTitle() { | |
return title; | |
} | |
public void setTitle(String title) { | |
this.title = title; | |
} | |
public boolean isCompleted() { | |
return completed; | |
} | |
public void setCompleted(boolean completed) { | |
this.completed = completed; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment