如何在FirebaseUi RecyclerView中显示?

时间:2018-03-19 19:49:18

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

我无法在Recyclerview上显示任何项目。我确定我的数据库连接正常。

但最后,它可以在RecyclerView中找不到任何东西,我不知道会发生什么,因为我只需2周就可以触摸Android工作室。

这是我的数据库结构

Root
  |
  |__restaurant
       |_____name
               |_____1
                     |_____lat: "22.34554"
                     |_____long: "34.124253"
                     |_____name: "A"
               |_____2
                     |_____lat: "24.34554"
                     |_____long: "35.124253"
                     |_____name: "B"

I want to show all restaurant name like A, B

这是我的活动(nearActivity.java)

    package com.example.chanyuenpan.garretproject3;

    import android.arch.lifecycle.LifecycleObserver;
    import android.support.annotation.NonNull;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    import com.firebase.ui.database.FirebaseRecyclerAdapter;
    import com.firebase.ui.database.FirebaseRecyclerOptions;
    import com.firebase.ui.database.ObservableSnapshotArray;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.Query;

        public class nearActivity extends AppCompatActivity {

        private restaurant restaurant;
        private DatabaseReference myRef;
        private RecyclerView recyclerView;
        private static final String TAG = "FirebaseRecyclerAdapter";

        Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("chats")
                .limitToLast(50);


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

            myRef = FirebaseDatabase.getInstance().getReference().child("Restaurant");
            myRef.keepSynced(true);

            recyclerView = (RecyclerView) findViewById(R.id.my_RevlerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            final FirebaseDatabase database = FirebaseDatabase.getInstance();





            FirebaseRecyclerOptions<restaurant> options =
                    new FirebaseRecyclerOptions.Builder<restaurant>()
                            .setQuery(myRef, restaurant.class)
                            .build();

            FirebaseRecyclerAdapter<restaurant, myViewHolder> adapter = 
new FirebaseRecyclerAdapter<restaurant, myViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull myViewHolder holder, int position, @NonNull restaurant model) {

                        holder.myTextView.setText(model.getName());
                }

                @Override
                public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.activity_near, parent, false);

                    return new nearActivity.myViewHolder(view);
                }
            };

            recyclerView.setAdapter(adapter);




        }



        public static class myViewHolder extends RecyclerView.ViewHolder{

            public TextView myTextView;

            public myViewHolder(View itemView){
                super(itemView);

                myTextView = itemView.findViewById(android.R.id.text1);

            }
        }    
    }

这是我的xml(activity_near.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="match_parent"
    tools:context="com.example.chanyuenpan.garretproject3.nearActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_RevlerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>

这是我的restaurant.java(Class)

package com.example.chanyuenpan.garretproject3;

import java.io.Serializable;

    public class restaurant implements Serializable {

        String mlat;
        String mlogi;
        String mname;

        public restaurant(){}

        public restaurant (String lat, String logi, String name){
            mlat = lat;
            mlogi = logi;
            mname = name;
        }

        public String getlat() {
            return mlat;
        }

        public void setLong(String logi) { mlogi = logi; }

        public String getlogi(){
            return mlogi;
        }

        public void setLat(String lat) { mlat = lat; }

        public String getName(){
            return mname;
        }
        public void setName(String name) { mname = name; }
    }

这是我的build.gradle

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.chanyuenpan.garretproject3"
        minSdkVersion 25
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.android.gms:play-services-maps:11.8.0'
    implementation 'com.google.android.gms:play-services-location:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.google.firebase:firebase-core:11.8.0'
    compile 'com.google.firebase:firebase-database:11.8.0'
    compile 'com.google.firebase:firebase-storage:11.8.0'
    compile 'com.google.firebase:firebase-auth:11.8.0'
    compile 'com.android.support:cardview-v7:26.1.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.firebaseui:firebase-ui-database:3.2.2'
    compile 'com.firebaseui:firebase-ui:3.2.2'
    compile 'com.firebaseui:firebase-ui-auth:3.2.2'
    implementation 'com.firebaseui:firebase-ui-storage:3.2.2'
    implementation 'com.firebaseui:firebase-ui-firestore:3.2.2'





    implementation 'com.android.support:design:26.1.0'
}

apply plugin: 'com.google.gms.google-services'

1 个答案:

答案 0 :(得分:0)

有一些问题:

  1. 您的数据类应使用一致的getter和setter(所以 getlogi应该是getLong)并且有private个字段。 (也, getlat应为getLat。安装者看起来不错。)
  2. 您传递给回收站选项构建器的查询不正确:它应为FirebaseDatabase.getInstance().getReference().child("restaurant").child("name").limitToLast(50);。我也建议删除不必要的name文件夹。
  3. 应删除对Firebase数据库的所有其他引用,因为它们没有正确大写,并且无法帮助填充回收站视图。
  4. FirebaseRecyclerOptions构建器应包含对setLifecycleOwner(this)的调用。