簡體   English   中英

使用Firebase列表適配器讀取和寫入Firebase數據庫

[英]Reading and writing to firebase database using Firebase list adapter

我正在嘗試從ListView的Firebase數據庫讀取數據。 ListView中的每個項目都帶有一個按鈕,一旦單擊該按鈕,將在textview中設置的讀取數據寫入數據庫。 下面的代碼是我用於顯示ListView和寫入數據庫的代碼。 這是我的數據庫

 if (FirebaseAuth.getInstance().getCurrentUser() ==null{
       Toast.makeText(getApplicationContext(),"logon first", Toast. Length.SHORT).show() ; 
   } else{
    display() ;
    } 

 private void display(){
    Query query=FirebaseDatabase.getInstance().getReference().child("border_details");
    FirebaseListOptions<DRive>options=new FirebaseListOptions.Builder<DRive>().setQuery(query,DRive.class).setLayout(R.layout.messages).build();
    ListView list= findViewById(R.id.list_of_messages);
    adapter=new FirebaseListAdapter<DRive>(options) {
        @Override
        protected void populateView(View v, DRive model, int position) {
            Button d=(Button)findViewById(R.id.button_accept);
            final TextView z=(TextView)findViewById(R.id.dropoff);
            final TextView c=(TextView)findViewById(R.id.pickup);
            final TextView f=(TextView)findViewById(R.id.timed);
            final TextView m=(TextView) findViewById(R.id.points);
            z.setText(model.getDropoffspot());
            c.setText(model.getPickupspot());
            f.setText(model.getPickuptime());
            m.setText(model.getKey());
            d.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DRive dRive=new DRive();
                    hec();
                    dRive.setDropoffspot(chi);
                    dRive.setKey(pt);
                    dRive.setPickupspot(g);
                    dRive.setPickuptime(boo);
                    dRive.setKey(pt);
                    myRef.child(pt).push().setValue(dRive);
                    Toast.makeText(getApplicationContext(),"shi",Toast.LENGTH_SHORT).show();


                }

                private void hec() {
                    pt=m.getText().toString();
                    boo=f.getText().toString();
                    chi=z.getText().toString();
                    g=c.getText().toString();
                }
            });




        }
    };
    list.setAdapter(adapter);

這是我的json

{
"order_details" : {
"DEzUoNY6BqOVn1DVIbFwOk6B4dT2" : {
  "-LlkSlkLOZYY7iy5vBWY" : {
    "dropoffspot" : "17 nursery crescent",
    "key" : "DEzUoNY6BqOVn1DVIbFwOk6B4dT2",
    "kutaPoints" : 80,
    "pickupspot" : "Mukuba l",
    "pickuptime" : "teaTime"
  }
}

}}

這是我的駕駛課

public class DRive {
private String Pickuptime;
private String Pickupspot;
private String Dropoffspot;
private String passengers;
private String Key;
public DRive(String passengers, String Pickupspot, String Pickuptime, String Dropoffspot, String Key){
    this.Dropoffspot=Dropoffspot;
    this.Pickupspot=Pickupspot;
    this.passengers=passengers;
    this.Pickuptime=Pickuptime;
    //mestime=new Date().getTime();

}
public DRive(){

}
public String getPickuptime(){
    return Pickuptime;
}

public void setPickuptime(String time) {
    this.Pickuptime = time;
}

public String getDropoffspot() {
    return Dropoffspot;
}

public void setDropoffspot(String dropoffspot) {
    this.Dropoffspot = Dropoffspot;
}

public String getPickupspot() {
    return Pickupspot;
}

public void setPickupspot(String pickupspot) {
    this.Pickupspot = Pickupspot;
}

public String getPassengers() {
    return passengers;
}

public String getKey() {
    return Key;
}

public void setKey(String key) {
    Key = key;
}

該應用程序應該從子order_details讀取數據,並使用FirebaseListAdapterListView顯示數據,而該應用程序則不能。

該應用程序應該從數據庫讀取並使用FirebaseListAdapter在列表視圖的各個部分中顯示檢索到的數據,相反,它甚至不顯示ListView 請提供協助。

在您的order_details節點和實際的DRive對象之間,還有另一個孩子。 我在您的數據庫中看到的是已認證用戶的uid 為了解決此問題,您也需要向該孩子添加呼叫。 因此,請更改以下代碼行:

Query query=FirebaseDatabase.getInstance().getReference().child("order_details");

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query=FirebaseDatabase.getInstance().getReference().child("order_details").child(uid);

編輯:

無法使用您的實際數據庫結構來一次查詢數據庫並獲得所有用戶的所有訂單。 如果需要,應該重新考慮數據庫結構或簡單地重復數據。 如果要獲取所有用戶的所有訂單,請參見以下可能的模式:

Firebase-root
  |
  --- order_details
        |
        --- orderIdOne
        |     |
        |     --- //orderDetails
        |     |
        |     --- uid: "DEzUoNY6BqOVn1DVIbFwOk6B4dT2"
        |
        --- orderIdTwo
              |
              --- //orderDetails
              |
              --- uid: "USw0LFY6BqOVn1DVIbFwOkhcvPwf"

要獲取所有訂單,只需在order_details節點上附加一個偵聽器,如果要獲取與單個用戶對應的所有訂單,請使用以下查詢:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference orderDetailsRef = rootRef.child("order_details");
Query query = orderDetailsRef.orderByChild("uid").equalTo(uid);
query.addListenerForSingleValueEvent(/* ... */);

編輯2:

由於DRive類中的字段與數據庫中的字段不匹配,因此您DRive 為了解決這個問題,請從以下帖子中查看我的答案:

我找到了解決問題的方法。 我添加了如下所示的onstart和onstop偵聽器

  @Override 
   protected void onStart() {
        super.onStart();
        adapter.startListening();
   }
   @Override 
    protected void onStop() {
         super.onStop();
         adapter.stopListening();
   }
  }

例如,在引入textview之后,我還通過添加'v'對Textview代碼進行了調整。 而不是最終的TextView f =(TextView)findViewById(R.id.timed); 我將其更改為最終的TextView f =(TextView)v。 findViewById(R.id.timed);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM