如何在 FirebaseUi RecyclerView 中显示



我无法在我的回收器视图上显示任何项目。我确定我的数据库连接很好。

但最后,它在回收器视图中什么都做不了,我不知道会发生什么,因为我只接触了 android 工作室 2 周。

这是我的数据库结构

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>

这是我的餐厅.java(类(

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. 您的数据类应该使用一致的 getter 和 setter(因此 getlogigetLong (并具有private字段。(另外, getlat应该是getLat.二传手看起来不错。
  2. 您传递到回收器选项生成器的查询不正确:它应该是FirebaseDatabase.getInstance().getReference().child("restaurant").child("name").limitToLast(50); 。我建议也摆脱不必要的name文件夹。
  3. 所有其他对 Firebase 数据库的引用都应移除,因为它们的大小写不正确,并且无助于填充回收器视图。
  4. FirebaseRecyclerOptions构建器应包括对setLifecycleOwner(this)的调用。

相关内容

  • 没有找到相关文章

最新更新