Ad

Reading And Writing To Firebase Database Using Firebase List Adapter

I am trying to read data from the Firebase database in a ListView. Each item in the ListView comes with a button, once that button is clicked the read data set in the textviews are written to the databse. The code below is the code I used for both showing the ListView and writing to the database.This is my database

 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);

and this is my json

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

} }

this is my drive class

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;
}

The app is supposed to read data from the child order_details and display that in the ListView using the FirebaseListAdapter, instead the app does not.

The app is supposed to read from the database and display the retrieved data in the parts of the listview using the FirebaseListAdapter, instead it does not even show the ListView. Kindly render assistance.

Ad

Answer

I found out how to fix my issue. I added an onstart and onstop listener like shown below

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

I also made adjustments to my Textview code by adding 'v' after introducing a textview, for example. Instead of final TextView f=(TextView)findViewById(R.id.timed); I changed it to final TextView f=(TextView)v. findViewById(R.id.timed);

Ad
source: stackoverflow.com
Ad