Search code examples
javaandroidfirebasefirebase-realtime-databasefirebaseui

FirebaseUI populate ListView text not displayed


I am populating a ListView of songs using Firebase database and Firebase-UI, all the dependencies are initialized correctly and the app is connected with the database but when it displays the list it doesn't display the text, just empty boxes like that:

List displayed

When an item is added to the database then a box is added but it doesn't show the text. Here is the code:

Song class:

package com.example.gloriadesideri.animas;

public class Song
{
    private String myName;
    private String myURL;
    private String myAuthor;

    public Song(){
        /*myName="";
        myURL="";
        myAuthor="";*/
    }

    public Song(String Author, String Song, String URL) {
        this.myName=Song;
        this.myURL=URL;
        this.myAuthor=Author;
    }
    public String getName()
    {
        return myName;
    }
    public String getURL()
    {
        return myURL;
    }
    public String getAuthor()
    {
        return myAuthor;
    }
    public void setName(String name)
    {
        this.myName=name;
    }
    public void setURL ( String URL)
    {
        this.myURL=URL;
    }
    public void setAuthor(String author)
    {
        this.myAuthor=author;
    }
}

Song Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="20dp"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/songName"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/author"/>
</LinearLayout>

Activity that should have the list view:

package com.example.gloriadesideri.animas;

import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;

public class Canzoni extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
{
   //nav view parameters
    private DrawerLayout cDrawerLayout;
    private ActionBarDrawerToggle cToggle;

    //list view parameters
    private ListView mListView;

    //firebase parameters
     private FirebaseListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canzoni);
        //nav view code
        cDrawerLayout= (DrawerLayout) findViewById(R.id.draw_viewC);
        cToggle= new ActionBarDrawerToggle(this, cDrawerLayout,R.string.Open, R.string.Close);
        cDrawerLayout.addDrawerListener(cToggle);
        cToggle.syncState();


        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null)
            actionBar.setDisplayHomeAsUpEnabled(true);
        NavigationView cNavigationView = (NavigationView) findViewById(R.id.nav_viewC);
        if (cNavigationView != null)
        {
            cNavigationView.setNavigationItemSelectedListener(this);
        }

        Query mQuery= FirebaseDatabase.getInstance().getReference().child("songs");
        mListView= (ListView) findViewById(R.id.canzoni_list);
        FirebaseListOptions<Song>  mOptions= new FirebaseListOptions.Builder<Song>()
                .setLayout(R.layout.song_layout)
                .setQuery(mQuery, Song.class)
                .setLifecycleOwner(this)
                .build();

        mAdapter= new FirebaseListAdapter <Song>(mOptions){
            @Override
            protected void populateView(View v, Song model, int position) {
                TextView songName= v.findViewById(R.id.songName);
                TextView songAuthor=v.findViewById(R.id.author);


                songName.setText(model.getName());
                songAuthor.setText(model.getAuthor());
            }
        };
        mListView.setAdapter(mAdapter);

        }

    @Override
    protected void onStart() {
        super.onStart();
        mAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mAdapter.stopListening();
    }

    @Override

    public boolean onOptionsItemSelected(MenuItem Item)
    {
        if(cToggle.onOptionsItemSelected(Item))
        {
            return true;
        }
        return super.onOptionsItemSelected(Item);
    }
    @Override
    public boolean onNavigationItemSelected(MenuItem Item)
    {
        int id = Item.getItemId();
        Intent intent;
        if (id == R.id.preghiere)
        {
            intent= new Intent(this, Preghiere.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if ( id== R.id.bans)
        {
            intent= new Intent(this, Bans.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if (id== R.id.canzoni)
        {
            intent= new Intent(this, this.getClass());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if (id==R.id.calendario)
        {
            intent= new Intent(this, Calendario.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if (id== R.id.per_riflettere)
        {
            intent= new Intent(this, perRiflettere.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        else if( id== R.id.home)
        {
            intent= new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            this.startActivity(intent);
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.draw_viewC);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

}

layout of the above activity

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Canzoni"
    android:id="@+id/draw_viewC">


    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/canzoni_list">

    </ListView>

    <android.support.design.widget.NavigationView
        android:layout_width="239dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/header"
        app:itemIconTint="@color/black"
        app:itemTextColor="@color/black"
        app:menu="@menu/drawem_menu"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp"
        tools:ignore="MissingConstraints"
        android:id="@+id/nav_viewC"/>

</android.support.v4.widget.DrawerLayout>

Any suggestion on how to fix this?

Edit:
the database looks like this:

Database

both writing and reading rules are true

Edit

I have changed the private names in the Song class so they are the same as the database.

private String Author;

private String Song;

private String Url;

it starts working the problem is that now it displays just the author

Update 2


Solution

  • After few experiments with code like change this

    songName.setText(model.getName());
    songAuthor.setText(model.getAuthor());
    

    to this

    songName.setText(model.getURL());
        songAuthor.setText(model.getAuthor());
    

    I have learned that the problem was really the names I gave to the privates and the names I gave to the get-methods in the Song class. The names of the privates should be the same as the database fields and the names of the methods should be getYourPrivateName().