如何使用FirebaseListAdapter填充ListView? [英] How to populate a ListView using a FirebaseListAdapter?

查看:113
本文介绍了如何使用FirebaseListAdapter填充ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Android项目,该项目将允许用户查找最近的汽油泵的名称,地址,以及最重要的是其价格.我有一个ListView,我想通过Firebase列表适配器填充它,因为我正在为项目使用Firebase.现在,在编写代码之后,我从Firebase中什么也没得到-它只是Android手机上的空白屏幕(Android Studio中没有显示错误).

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).

编写setContentView(lview);之后;在onCreate()方法的最后,它现在给我错误,并且活动崩溃.最后,我从firebase收到了错误:com.firebase.client.FirebaseException:无法弹跳键入

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

苯辛菌类

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

}

listepriser的XML布局

<?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" />

数据库视图

推荐答案

如果查看Failed to bounce to type异常的完整堆栈跟踪,它将告诉您哪里出了问题.

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

但是在这种情况下,您的问题似乎是由JSON中的属性名称以大写字母开头的事实引起的.这可能有效:

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

这篇关于如何使用FirebaseListAdapter填充ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆