close
示範
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<String, Object> childUpdates = new HashMap<>(); childUpdates.put("DCBA","123456777");//前面的字是child後面的字是要修改的value值 myRef.updateChildren(childUpdates); } }
相關文章:
[Android] Android 程式設計教學
本網站https://kk665403.pixnet.net/內之全部圖文,Saioyan梟夜所有,非經本人同意不得將全部或部分內容轉載於任何形式之媒體
Copyright © 2021 Saioyan. All rights reserved.
版權所有© 2021 Saioyan梟夜
文章標籤
全站熱搜