ZZ.IT
ZZ.IT

Reputation: 23

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

Upvotes: 2

Views: 3007

Answers (4)

Frank van Puffelen
Frank van Puffelen

Reputation: 599766

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;
}

Upvotes: 1

Cliabhach
Cliabhach

Reputation: 1550

I haven't used Firebase before, but shouldn't the ListView be added to the layout (or be retrieved from it)?

Something like

setContentView(R.layout.activity_testbezinpriser);
ListView lview = (ListView)findViewById(R.id.station_list);

In the activity_testbezinpriser.xml, there should be a ListView, like

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

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

</LinearLayout>

Upvotes: 0

Kevin O&#39;Neil
Kevin O&#39;Neil

Reputation: 1421

Try changing your Firebase reference. It doesn't look like it pointing to the right location. Based on the link you posted to your data structure it looks like you are trying to list objects stored in your /detail location. If that true try using a ref like this:

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

Upvotes: 1

dehboxturtle
dehboxturtle

Reputation: 56

Just a guess here. Your getPrice() method returns a string. I think you need a getter that has the same return type as the property you are getting. You can keep the String one, but it must be in addition to a method with the signature...

public Double getPrice();

The reason I think this is because firebase is complaining that it is unable to figure what property of your object a certain field matches to. This could be because you are supposed to provide getters for each property, and Firebase may not like that price is a Double but your getter turns it into a string. Here is a link to a detailed post about how this all works by Frank. Why do I get "Failed to bounce to type" when I turn JSON from Firebase into Java objects?

Upvotes: 0

Related Questions