使用FirebaseListAdapter无法从ListView上的Firebase获取数据

时间:2018-03-16 04:04:35

标签: android android-layout listview firebase-realtime-database android-adapter

此代码的问题是数据未显示在列表视图中,并且应用程序也未显示任何错误。我正在从其他一些活动初始化这个模型类,所以请任何人都能给我一些关于什么是真正问题的建议。

public class FireBaseListAdapter extends AppCompatActivity {

    private ListView mlistview;

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

        mlistview = (ListView)findViewById(R.id.firebase_list);

      //  DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

        final Query query = getInstance().getReference();

        FirebaseListOptions<Users> options = new FirebaseListOptions.Builder<Users>().setQuery(query,Users.class).setLayout(R.layout.listview_for_firebaselistadapter).build();

        FirebaseListAdapter<Users> firebaseListAdapter = new FirebaseListAdapter<Users>(options) {
            @Override
            protected void populateView(View v, Users model, int position) {
                model = getItem(position);
                TextView textViewname = (TextView)v.findViewById(R.id.listAdapter_nametext);
                TextView textViewplace = (TextView)v.findViewById(R.id.listAdapter_placetext);

                textViewname.setText(model.getName());
                textViewplace.setText(model.getPlace());
            }
        };
        mlistview.setAdapter(firebaseListAdapter);
    }
}

这是我的模特课:

    public class Users {

    private String name;
    private String place;

    public Users() {
    }

    public Users(String name, String place) {
        this.name = name;
        this.place = place;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }
}

这是我的XML包含两个textView

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

    <TextView
        android:id="@+id/listAdapter_nametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Driver Name"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/listAdapter_placetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Driver Place"
        android:textSize="25sp"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

您似乎忘记在查询中传递参考

final Query query = getInstance()。getReference();

// Create a reference to the cities collection
CollectionReference citiesRef = db.collection("cities");

// Create a query against the collection.
Query query = citiesRef.whereEqualTo("state", "CA");

Reference

答案 1 :(得分:0)

要解决此问题,您需要覆盖onStart()方法并添加以下代码行:

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

但首先不要忘记将firebaseListAdapter变量全局化。

private FirebaseListAdapter<Users> firebaseListAdapter;

然后只需删除适配器初始化的FirebaseListAdapter<Users>