-1

As refer in this link. I want to try in my coding apps. But it didnt retrieve any data from my firebase database. I just cant figure out how to fix it. Can someone please help me. Is there any idea, where did I miss?

My firebase database as show image below:-

enter image description here

Spinner coding:-

mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.keepSynced(true);

mDatabase.child("Advertisement").child(mAuth.getUid()).addValueEventListener(new ValueEventListener()
        {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                //final List<String> name = new ArrayList<String>();
                ArrayList<String> identity = new ArrayList<>();
                identity.add(0, "Choose Tuition Centre");

                for (DataSnapshot nameSnapshot: dataSnapshot.getChildren())
                {
                    String tuitionName = nameSnapshot.child("adstuitioname").getValue(String.class);
                    identity.add(tuitionName);
                }

                ArrayAdapter<String> nameAdapter = new ArrayAdapter<String>(RatingActivity.this, android.R.layout.simple_spinner_item, identity);
                nameAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                mName.setAdapter(nameAdapter);
            }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        mName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                if(position==0)
                {
                    //Toast.makeText(getApplicationContext(),"No Item Selected",Toast.LENGTH_LONG).show();
                }

                else
                {
                    Toast.makeText(getApplicationContext(),parent.getItemAtPosition(position) +" Selected",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {
                // TODO Auto-generated method stub
            }
        });
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Daisy
  • 527
  • 1
  • 5
  • 14
  • 1
    If the app closes, then there is a logcat that you've not uploaded to this post. You are also ignoring all errors in `onCancelled`, so please put something there – OneCricketeer Oct 01 '18 at 08:27
  • What seems to be the problem? – Ümañg ßürmån Oct 01 '18 at 08:33
  • @UmangBurman my apps crash so cant figure out whether the spinner show the data or not – Daisy Oct 01 '18 at 08:42
  • 1
    Can you show the logcat somehow in the question so that we can understand – Ümañg ßürmån Oct 01 '18 at 08:44
  • @UmangBurman Sir.. I already fix it other ways and I cant retrieve the data from the firebase database. I already edit my question. – Daisy Oct 01 '18 at 09:33
  • Are you getting any data in Identify List? – Ümañg ßürmån Oct 01 '18 at 09:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181057/discussion-between-daisy-and-umang-burman). – Daisy Oct 01 '18 at 09:56
  • Do you any message in `databaseError`? Please responde with @. – Alex Mamo Oct 01 '18 at 10:05
  • @AlexMamo Dont have sir... – Daisy Oct 01 '18 at 11:34
  • @cricket_007 Sir.. I already fix it other ways and I cant retrieve the data from the firebase database. I already edit my question. – Daisy Oct 01 '18 at 11:34
  • If you've fixed it, then feel free to add your own answer – OneCricketeer Oct 01 '18 at 12:42
  • No @cricket_007, I try to fix it other ways, but still cant retrieve the data from the firebase database to spinner. – Daisy Oct 01 '18 at 12:53
  • You're getting an error somewhere. Please try to add log statements to your code for the spots where you think data should be added to the arraylist, and then edit your question to include those logs. That will help us to help you better – OneCricketeer Oct 02 '18 at 00:57
  • @cricket_007 my apps didnot crash sir, the problem is I cant retrieve the data from database into my spinner. – Daisy Oct 02 '18 at 09:37
  • I did not say your *app crashes*. **You did** say your "app suddenly closes". I'm not sure why you're trying to argue with me when I'm trying to help you help yourself by *debugging your own code*... Basically, if you just look at your Firebase loop and compare what is actually in the database, you have an extra object above where you seem to think `adstuitioname` is available, therefore, accessing that will make "your app crash suddenly". **Or** `databaseError` in your code is actually being set, you're ***ignoring it***. The answers below look correct, but I can't test them – OneCricketeer Oct 02 '18 at 12:21
  • @cricket_007 omg, honestly I didnt arguer with you sir, I already edited my question, the problem now is I cant retrieve the data from firebase into my spinner :'D – Daisy Oct 02 '18 at 12:23
  • and yes, I'm also referring the below answer from Maam Ami :D – Daisy Oct 02 '18 at 12:24

3 Answers3

1

Add listener as below :

DatabaseReference mDatabaseRef =  
                            FirebaseDatabase.getInstance().getReference();
ArrayList<String> list=new ArrayList<>();
mDatabaseRef.child("Advertisement")
            .addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                   for (DataSnapshot snapshot :dataSnapshot.getChildren()) 
                     {
                        Advertisementject advertisement= 
                                snapshot.getValue(Advertisement.class);
                        //replace Object.class with your class name
                        //get your key value here from your "Custom class" 
                        // which contains "adstutioname" 
                        // add in list
                        list.add(advertisement.getAdstutuioname());
                    }

                    //set adapter
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

generate class like this

public class Advertisement{

@SerializedName("adstutuioname")
@Expose
private String adstutuioname;

public String getAdstutuioname() {
  return adstutuioname;
}

public void setAdstutuioname(String adstutuioname) {
  this.adstutuioname = adstutuioname;
}

}
  • Your class contains all your params and .
  • Replace Object with this class
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Can you please give an example for this ? 1) get your key value here from your "Custom class" which contains "adstutioname". 2) add in list – Daisy Oct 01 '18 at 11:41
  • I already create my advertisement java class which consists of constructor and getter. – Daisy Oct 01 '18 at 12:11
  • but this one --- >//get your key value here from your "Custom class" which contains "adstutioname" andd this one ---> // add in list. How ? :D – Daisy Oct 01 '18 at 12:11
  • So now you will get Advertisement advertisement instance. then access Advertisement.getAdstutuioname() and add it to your list – Ami Trambadia Oct 01 '18 at 12:15
  • String tuitionName = dataSnapshot.child("adstuitioname").getValue().toString(); – Daisy Oct 01 '18 at 12:18
  • Is it like this ? – Daisy Oct 01 '18 at 12:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181077/discussion-between-daisy-and-ami-trambadia). – Daisy Oct 01 '18 at 12:20
0

I hope the problem is with your getValue() method. Firebase getValue method returns an Object, and you are expecting a String. Please modify this line to convert your Object to String -

String tuitionName = nameSnapshot.child("adstuitioname").getValue(String.class).toString();
Anirban Roy
  • 180
  • 6
  • Why areaName? Im using this one sir, String tuitionName = nameSnapshot.child("adstuitioname").getValue(String.class); – Daisy Oct 01 '18 at 09:13
  • Sir... still the same. But I fix it other ways and I cant retrieve the data from the firebase database. I already edit my question. – Daisy Oct 02 '18 at 09:41
0

It's not clear if you're trying to read once, or watch changes Difference between addValueEventListener() and addListenerForSingleValueEvent() of firebase

First things first, look if you are getting a database error

@Override
public void onCancelled(DatabaseError databaseError) {
    // Toast or print the databaseError, don't ignore it
}

Then look where you're adding a listener - mDatabase.child("Advertisement").child(mAuth.getUid())., which from your picture is every element at the level of -LNZ6....

For that level, you have a list of object's, so you can need to use a loop, and as shown from some examples on Firebase site, it's recommended that you make an object, but you still can use child method for a certain field as you're trying to do

But if a field doesn't exist, you get null value, and Adapters cannot hold nulls, so you must ignore or set some default value.

ArrayList<String> identity = new ArrayList<>();
identity.add(0, "Choose Tuition Centre");

for (DataSnapshot nameSnapshot: dataSnapshot.getChildren())  {
    String tuitionName = nameSnapshot.child("adstuitioname").getValue(String.class);
    Log.d("DEBUG TUITION", String.valueOf(tuitionName);  // look for this in your logs 
    identity.add(tuitionName == null ? "none" : tuitionName);
}
ArrayAdapter<String> nameAdapter =... 
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Sir, I try already, still the same, this = {RatingActivity$2@6534} dataSnapshot = {DataSnapshot@6536} "DataSnapshot { key = 52ictJZU8TWWqOsGbpnQ6GDCYUv1, value = null }" identity = {ArrayList@6537} size = 1 – Daisy Oct 03 '18 at 01:14
  • I want to fill the spinner with the data from the Firebase Database. If the tuition centre change it name, so the list data in the Spinner also change. – Daisy Oct 03 '18 at 01:23
  • Then it seems `dataSnapshot.getChildren()` is empty and you're not adding the listener to the correct object. For example, how do you know that `mAuth.getUid()` needs to be added? Is that the right ID? – OneCricketeer Oct 03 '18 at 01:33
  • Plus, your comment there says `52ictJZU8TWWqOsGbpnQ6GDCYUv1`, but that's not in the pictures shown – OneCricketeer Oct 03 '18 at 01:35