0

I am beginner to android development using firebase database. I am trying to fetch the data from firebase realtime databse and store it to listview. But when i run the app it launches,but there is no data and it get crashed after few seconds. Can you help me by suggesting where i am wrong?

I Have tried this kind of code in java file. And the code shown below is in mainactivity class without any kind of import or typo error.

private FirebaseDatabase fbdatabase;
     private DatabaseReference dbref;
     private ListView lstvwdata;
     private ArrayList<String> alist = new ArrayList<>();
     private ArrayAdapter<String> aadapt;
     String ad_lst_val;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         fbdatabase =FirebaseDatabase.getInstance("https://lstdatafetch.firebaseio.com/");
         dbref = fbdatabase.getReference("Orders");
         lstvwdata =(ListView)findViewById(R.id.lstview);
         final ArrayAdapter<String> aadapt = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,alist);
         lstvwdata.setAdapter(aadapt);
         dbref.addChildEventListener(new ChildEventListener() {
             @Override
             public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                 ad_lst_val = dataSnapshot.getValue(String.class);
                 alist.add(ad_lst_val);
                 aadapt.notifyDataSetChanged();
             }

             @Override
             public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

             }

             @Override
             public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

             }

             @Override
             public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

             }

            @Override
             public void onCancelled(@NonNull DatabaseError databaseError) {

             }
         }); }

I just want to fetch the data and put into listview.

Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
Neel Gohel
  • 11
  • 3
  • 4
    Post the crash logs in your question – Suhas Ts Jun 07 '19 at 06:59
  • 4
    Post the error from the logcat, but the first question is: do you ask for internet permission in the manifest? – MarkWalczak Jun 07 '19 at 07:07
  • Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Vladyslav Matviienko Jun 07 '19 at 07:29
  • 1
    @MarkWalczak yes,i have provided user permission. But now i get my query solved. One of the reason for crashing was data conversion from firebase. I have taken the dataarray string type and my actual data was enetred in db was long type.(i assumed that while entering that it will take as string). – Neel Gohel Jun 07 '19 at 08:08
  • @SuhasTs thanks buddy, it was my fault of entering wrong data type in db(while entering i assumed that it will convert into string..but its not and it cause a problem).It was long type and i have taken string type array. Thanks to make me aware new thing..LogCAT. – Neel Gohel Jun 07 '19 at 08:11
  • If the app crashes, there is a stack trace. Please look that up on logcat, and add it to your question. Please also responde with @AlexMamo – Alex Mamo Jun 07 '19 at 09:25

1 Answers1

0

If you want to display order list from firebase data, I am using addValueEventListener in my application.

ArrayList<String> ordersList = new ArrayList<>();

databaseRef.child("Orders").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        ordersList.clear();
        for (DataSnapshot data : dataSnapshot.getChildren()){
            ordersList.add(data.getKey);
        }

        //Update Your Adapter here

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.w(TAG, "getUser:onCancelled", databaseError.toException());
        // ...
    }
});
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74