gpt4 book ai didi

java - FirebaseListAdapter 未 populateView 未被调用

转载 作者:行者123 更新时间:2023-12-02 01:14:36 25 4
gpt4 key购买 nike

这个问题已经困扰我一段时间了。我以前从未真正使用过 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>

请求 Activity

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

}
});

}
}

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

这一直让我抓狂,所以我希望有人能提供帮助。

最佳答案

您不需要在 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();
}

关于java - FirebaseListAdapter 未 populateView 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58872919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com