FirebaseListAdapter不populateView不被调用

时间:2019-11-15 08:22:11

标签: android firebase firebase-realtime-database android-listview firebaseui

这已经困扰了我一段时间了。我以前从未真正使用过ListViews,也从未使用过FirebaseListAdapters。我要做的就是通过显示ID和用户位置来启动列表的基础,但由于某种原因,未调用populateView重写,因此我无法弄清楚为什么..

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    tools:context=".RequestActivity"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Requested"
        android:gravity="center"
        android:textSize="20sp"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ListView"/>



</LinearLayout>

RequestActivity

package com.dotcreate.rfal;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;


//Firebase imports
import com.firebase.ui.database.FirebaseListAdapter;
import com.firebase.ui.database.FirebaseListOptions;
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.ValueEventListener;

public class RequestActivity extends AppCompatActivity {

    private ListView ListView;

    private FirebaseListOptions<Requests> listAdapterOptions;
    private FirebaseListAdapter<Requests> listAdapter;

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


        //Set our listview
        rideRequestListView = (ListView) findViewById(R.id.ListView);
        listening = false;
        populateListView();
    }


    private void populateListView(){
        /*
            Create a database reference to the request
         */
        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        DatabaseReference requestReference = FirebaseDatabase.getInstance().getReference().child("Requests");
        requestReference.keepSynced(true);
        requestReference.addValueEventListener(new ValueEventListener() {

            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                DatabaseReference requestReference = FirebaseDatabase.getInstance().getReference().child("Requests");
                listAdapterOptions = new FirebaseListOptions.Builder<Requests>()
                        .setLayout(android.R.layout.simple_list_item_1)
                        .setQuery(requestReference, Requests.class)
                        .build();

                listAdapter = new FirebaseListAdapter<Requests>(listAdapterOptions) {
                    @Override
                    protected void populateView(View v, RideRequests requests, int position) {
                        ((TextView)v.findViewById(android.R.id.text1)).setText(requests.getID());
                    }
                };
                ListView.setAdapter(listAdapter);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

    }
}

这是数据库,就像我说的超级简单。 Firebase Realtime Database

这一直让我pull着头发,希望有人能帮忙。

1 个答案:

答案 0 :(得分:2)

您无需在adapter内添加addValueEventListener即可检索数据。请尝试以下操作:

DatabaseReference requestReference = FirebaseDatabase.getInstance().getReference().child("Requests");

 listAdapterOptions = new FirebaseListOptions.Builder<Requests>()
   .setLayout(android.R.layout.simple_list_item_1)
   .setQuery(requestReference, Requests.class)
   .build();

 listAdapter = new FirebaseListAdapter<Requests>(listAdapterOptions) {
    @Override
  protected void populateView(View v, RideRequests requests, int position) {
     ((TextView)v.findViewById(android.R.id.text1)).setText(requests.getID());
   }
 };
ListView.setAdapter(listAdapter);

在此处查看更多信息:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-to-populate-a-listview

还使用startListening填充数据:

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