Zi 字媒體
2017-07-25T20:27:27+00:00
示範
XML
xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
tools:layout_editor_absoluteY="72dp" />
<Button
android:id="@+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="b2"
android:text="刪除"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/t1" />
<Button
android:id="@+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="b3"
android:text="新增"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/b2" />
<Button
android:id="@+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="b4"
android:text="更新"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/b3" />
android.support.constraint.ConstraintLayout>
JAVA
public class MainActivity extends AppCompatActivity {
TextView t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(TextView)findViewById(R.id.t1);
t2=(TextView)findViewById(R.id.t2);
final FirebaseDatabase database = FirebaseDatabase.getInstance();//取得資料庫連結
DatabaseReference myRef= database.getReference("CCCC");
myRef.addChildEventListener(new ChildEventListener() {//讀取
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
t1.setText(""+dataSnapshot.getValue());
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
public void b2(View v){//刪除
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("CCCC");
myRef.child("ABCD").removeValue();
}
public void b3(View v){//新增
final FirebaseDatabase database = FirebaseDatabase.getInstance();//取得資料庫連結
DatabaseReference myRef= database.getReference("CCCC");
myRef.child("ABCD").setValue("224466");
}
public void b4(View v){//更新
FirebaseDatabase database = FirebaseDatabase.getInstance();//取得資料庫連結
DatabaseReference myRef = null;
myRef= database.getReference("CCCC");
Map childUpdates = new HashMap<>();
childUpdates.put("DCBA","123456777");//前面的字是child後面的字是要修改的value值
myRef.updateChildren(childUpdates);
}
}
相關文章:
[Android] Android 學習總集
寫了
5860316篇文章,獲得
23313次喜歡