ZZ.IT ZZ.IT - 28 days ago 25
Android Question

How to populate a ListView using a FirebaseListAdapter?

I am working on Android project, which will allow users to find nearest petrol pump's name, address, and most importantly their prices. I have a ListView, which I want to populate through Firebase list adapter because I am using Firebase for my project. Now after writing code I am getting nothing from Firebase -- it's just blank screen on Android phone (no error showing in Android Studio).

After writing setContentView(lview); in the end of the onCreate() method , it is giving me error now and the activity crashes. Finally I got the error from firebase : com.firebase.client.FirebaseException: Failed to bounce to type

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.firebase.client.Firebase;
import com.firebase.ui.FirebaseListAdapter;

public class testbezinpriser extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_testbezinpriser);
ListView lview = new ListView(this);


Firebase.setAndroidContext(this);
Firebase ref = new Firebase("https://xxxxx.firebaseio.com");

FirebaseListAdapter<Benzin> Adapter = new FirebaseListAdapter<Benzin>(this, Benzin.class, R.layout.listepriser, ref) {
@Override
protected void populateView(View v, Benzin s, int i) {
((TextView)v.findViewById(R.id.navnView)).setText(s.getName());
((TextView)v.findViewById(R.id.addressView)).setText(s.getAddress());
((TextView)v.findViewById(R.id.prisView)).setText(s.getPrice()));

}
};
lview.setAdapter(Adapter);
setContentView(lview);
}
}


And Benzin class

public class Benzin {

String Address;
String Name ;
String Price;

public Benzin(){

}
public Benzin (String Name , String Address , String Price){
this.Name = Name;
this.Address = Address;
this.Price = Price;
}

public String getAddress(){
return Address;
}

public String getName(){
return Name;
}
public String getPrice(){
return Price;
}


}

XML Layout for listepriser

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:layout_width="80dp"
android:layout_height="50dp"
android:id="@+id/logoView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:src="@drawable/shell"
android:contentDescription="tankstation_billed" />

<TextView
android:layout_width="70dp"
android:layout_height="25dp"
android:id="@+id/navnView"
android:layout_alignTop="@+id/logoView"
android:layout_toEndOf="@+id/logoView"
android:layout_marginStart="10dp"
android:textStyle="bold|italic"
android:textSize="15sp"
android:textColor="#000000"
android:text="SHELL"
android:layout_marginTop="5dp"
android:textIsSelectable="false" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Måløvhovedgade 38 , Måløv"
android:id="@+id/addressView"
android:layout_alignBottom="@+id/logoView"
android:layout_alignStart="@+id/navnView"
android:layout_marginBottom="3dp"
android:textSize="12sp"
android:textColor="#000000" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10.99"
android:id="@+id/prisView"
android:layout_alignTop="@+id/navnView"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:textColor="#000000"
android:typeface="serif" />




Database view

Answer

If you look at the full stack trace of the Failed to bounce to type exception, it will tell you what is wrong.

But in this case it seems likely that your problem is caused by the fact that the property names in your JSON start with an uppercase letter. This is likely to work:

public class Benzin {
    public String Address;
    public String Name ;
    public String Price;
}