Joe Bot
Joe Bot

Reputation: 11

Firebase Not Populating List

I am building an Android shopping list app following a Angga Risky tutorial.

Code running Error free, but the list I have created is not being populated by the firebase real-time database it is connected to. Below is the mainactivity

public class MainActivity extends AppCompatActivity {

    TextView titlepage, subtitlepage, endpage;

    DatabaseReference reference;
    RecyclerView ourlist;
    ArrayList < MyList > list;
    ListAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        titlepage = findViewById(R.id.titlepage);
        subtitlepage = findViewById(R.id.subtitlepage);
        endpage = findViewById(R.id.endpage);

        //working with data
        ourlist = findViewById(R.id.ourlist);
        ourlist.setLayoutManager(new LinearLayoutManager(this));
        list = new ArrayList < MyList > ();

        //get data from firebase
        reference = FirebaseDatabase.getInstance().getReference().child("compshopper3");
        reference.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                //set code to retrieve data and replace layout
                for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) {
                    MyList p = dataSnapshot1.getValue(MyList.class);
                    list.add(p);
                }
                listAdapter = new ListAdapter(MainActivity.this, list);
                ourlist.setAdapter(listAdapter);
                listAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                // set code to show an error
                Toast.makeText(getApplicationContext(), "Dude Where's My Data???", Toast.LENGTH_SHORT).show();

            }

        });
    }
}

Here is look at the firebase data

enter image description here

Here is a shot of the emulator output.

enter image description here

Upvotes: 0

Views: 112

Answers (2)

Shrey Garg
Shrey Garg

Reputation: 1337

In addition to @peter-haddad's answer, make sure your firebase database rules allow you to access the data without authentication, in case you're trying to do that (which we usually do during dev testing).

Firebase console -> Database (Realtime Database) -> Rules ->

{
 "rules": {
   ".read": true,
   ".write": true
 }
}

Upvotes: 1

Peter Haddad
Peter Haddad

Reputation: 80924

First change the reference to refer to the parent node:

reference = FirebaseDatabase.getInstance().getReference().child("CompShopperApp");

Then remove the for loop inside onDataChange() and make sure that your model class contains the fields price1, price2, price3 and itemtitle:

    reference.addValueEventListener( new ValueEventListener () {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            MyList p    = dataSnapshot.getValue(MyList.class);
            list.add(p);
            listAdapter = new ListAdapter(MainActivity.this, list);
            ourlist.setAdapter(listAdapter);
            listAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            // set code to show an error
            Toast.makeText(getApplicationContext(), "Dude Where's My Data???", Toast.LENGTH_SHORT).show();

        }

Upvotes: 1

Related Questions