1

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.

Bwalya
  • 118
  • 11

2 Answers2

0

Between your order_details node and the actual DRive objects there is another child. As I see in your database is the uid of the authenticated user. In order to solve this issue, you need to add a call to that child too. So please change the following line of code:

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

to

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

Edit:

There is no way using your actual database structure to query the database and get all orders of all users in a single go. If you want that, you should reconsider your database structure or simply duplicate data. If you want to get all orders of all users, please see a possible schema below:

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

To get all orders simply attach a listener on order_details node and if you want to get all orders that corresponde to a single user, please use the following query:

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(/* ... */);

Edit2:

You arent't getting anything due the fact that the fields in your DRive class do not match the one in the database. To solve this, please see my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • The thing is, that is only the writing of one user in the json file, once other users access the app they will write data in the same way that I did, and I want all that data to be accessible in the list view. There will be different user id's in the same order_details node – Bwalya Aug 08 '19 at 16:15
  • Fair enough, but you still need to add the call to `.child(uid)`. Have you tried it, does it work? – Alex Mamo Aug 08 '19 at 16:51
  • I haven't, but won't it only return data for the current user? Ideally the user is supposed to view evern other users data – Bwalya Aug 08 '19 at 17:51
  • So you should try it. In the meanwhile, please see my updated answer. – Alex Mamo Aug 08 '19 at 18:05
  • Thank you so much, I will try this first thing in the morning, I really appreciate it, it makes so much sense, am sure it will work – Bwalya Aug 08 '19 at 18:57
  • Ok, keep me posted with your progress. – Alex Mamo Aug 08 '19 at 19:00
  • The list view is still not showing any content, and I did make the changes to the database you suggested – Bwalya Aug 09 '19 at 10:55
  • Perhaps there is something wrong with the earlier part of my code? Let me show the top part where the display method is used – Bwalya Aug 09 '19 at 11:04
  • Show me now your new database strucutre, as a screenshot please and add the code that you are using to get the data. – Alex Mamo Aug 09 '19 at 11:33
  • Okay let me edit the question to include the screenshot and the code to get the data – Bwalya Aug 09 '19 at 12:07
  • Please also add the content of your `DRive` class. – Alex Mamo Aug 09 '19 at 12:21
  • I have added it – Bwalya Aug 09 '19 at 12:37
  • I see now where the problem is. So pelase see my updated answer. Does it work now? – Alex Mamo Aug 09 '19 at 12:58
  • It has still failed, nothing is showing. I tried both methods – Bwalya Aug 09 '19 at 21:04
  • Saying "It has still failed" doesn't help me help you Please provide more info and show me again, your database schema, the content of your `DRive` class and the code that you are using. – Alex Mamo Aug 09 '19 at 21:12
  • My apologies. for the code I am using, I am still using the same code as shown in the question, however I changed the DRive class according to the way your previous post advised. I will update it right away. Perhaps though there is something wrong with my xml files, could that be the case? I have also not made any changes to the database – Bwalya Aug 09 '19 at 21:17
  • Show me your the new changes. Have you only changed your `DRive` class or also in the database? – Alex Mamo Aug 10 '19 at 08:35
0

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

Bwalya
  • 118
  • 11