0

This is my database and I wanna retrieve a data (which is underline in red color) from firebase to Listview .

This is the picture of my XML code

This is the my java code:

public class TimeTableActivity extends AppCompatActivity {

    ListView table01;
    DatabaseReference Trainline;
    ArrayList<String> arrayList= new ArrayList<>();
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_time_table);
        Trainline= FirebaseDatabase.getInstance().getReference("Time_table");
        table01 = findViewById(R.id.table01);
        arrayAdapter = new ArrayAdapter<String>( TimeTableActivity.this, android.R.layout.simple_list_item_1);
        table01.setAdapter(arrayAdapter);

        Trainline.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {    
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {    
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot snapshot) {    
            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {    
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {    
            }
        });    
    }    
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

0

In the onChildAdded you can:

  1. Read the value from the snapshot
  2. Add it to the list that the adapter is based on
  3. Tell the adapter to refresh itself

In code:

arrayAdapter = new ArrayAdapter<String>(TimeTableActivity.this, 
                                        android.R.layout.simple_list_item_1, arrayList);
                                                                          //  Add this
 Trainline.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) {    
        String value = snapshot.child("time_table").getValue(String.class);  //  1. Get the value
        arrayList.add(value); //  2. Add to the array
        arrayAdapter.notifyDataSetChange(); //  3. Tell the adapter to repaint.
    }
    ...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Firstly Thanks Frank for help to me... I tries yours code and then go to timetable activity suddenly the application will stop automatically....why is that? – Lakruwan Nov 03 '21 at 23:39
  • That's hard to say without seeing an error. I recommend checking out https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – Frank van Puffelen Nov 03 '21 at 23:53
  • Yeah... got it.. Now it's work nicely , Thanks again Frank... – Lakruwan Nov 04 '21 at 22:02
  • yeah your answer is correct. I already vote up but here get message like this "Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded." – Lakruwan Nov 04 '21 at 22:37
  • Hey @Lakruwan You should be able to accept an answer (by clicking the checkmark next to it), since you posted the question. – Frank van Puffelen Nov 12 '21 at 00:32
0

When you read data in the firebase database, you should retrieve the entire data. So, there is no way to fetch only specific fields from the data returned on reading a path.

Because of that, if it's possible for your case, I would suggest you to add new data in firebase with the purpose of queries that only retrieve the information that you need.

For doing that, you can have a separate node that can be called something like TimeTable-SpecificList. Now, in this table, have user uid as your child keys, and each entry shall have only a timeTableSpecific field which you want to fetch.

Example:

replicaTimeTable :{
      key1: {
           timeTableSpecific: "Colombo-Gampaha ..."
      },
      key2: {
           timeTableSpecific: "Colombo-Kandy ..."
      },
      key3: {...},

}

This solution to minimize download redundancy and optimize for speed then is the way to go!

Reference: Fetch particular fields from Firebase database