简体   繁体   English

使用Firebase UI数据库绑定到Recycler View(Android)时,Firebase不会检索数据

[英]Firebase not retrieving data when binding using Firebase UI Database to Recycler View (Android)

I am trying to bind data from my firebase database which contains data in the format Food > Image , Text . 我正在尝试绑定来自Firebase数据库的数据,该数据库包含食品>图像,文本格式的数据。 ie Child food with two children Image and Text. 即与两个孩子的图像和文本的儿童食品。 Unfortunately when i run the app and try to retrieve it , it does not display anything. 不幸的是,当我运行该应用程序并尝试对其进行检索时,它不会显示任何内容。 I believe the recycler view code seems fine . 我相信回收者的视图代码似乎还不错。 If you know the solution please help me out. 如果您知道解决方案,请帮助我。 I have attached my logcat below. 我在下面附上了我的logcat。 This is the code for my data binding 这是我的数据绑定的代码

    Query query = FirebaseDatabase
            .getInstance()
            .getReference()
            .child("Food");

    FirebaseRecyclerOptions<fooddatabase> options =
            new FirebaseRecyclerOptions.Builder<fooddatabase>()
                    .setQuery(query, fooddatabase.class)
                    .build();
    adapter = new FirebaseRecyclerAdapter<fooddatabase, FoodViewHolder>(options) {
        @Override
        public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.food_item, parent, false);

            return new FoodViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull FoodViewHolder holder, int position, @NonNull fooddatabase model) {
            holder.Foodname.setText(model.getTitle());
            Picasso.with(getBaseContext()).load(model.getImage())
                    .into(holder.Foodimage);
            final fooddatabase clickItem = model;
            holder.setItemClickListener(new ItemClickListener() {
                @Override
                public void onClick(View view, int position, boolean isLongClick) {
                    Toast.makeText(home.this, ""+clickItem.getTitle(), Toast.LENGTH_SHORT).show();
                    //return null;
                }
            });
        }

    };

    foodlist.setAdapter(adapter);

And this is my logcat 这是我的logcat

03-10 10:53:55.079 3004-10085/? V/FA-SVC: Event recorded: Event{appId='com.example.zafee.foodreviewapp', name='screen_view(_vs)', params=Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=signin, firebase_previous_id(_pi)=-8953806997713635209, firebase_screen_class(_sc)=home, firebase_screen_id(_si)=-8953806997713635208}]} 03-10 10:53:55.082 3004-10085/? V/FA-SVC: Upload scheduled in approximately ms: 14891 03-10 10:53:55.084 3004-10085/? V/FA-SVC: Cancelling job. JobID: 812057698 03-10 10:53:55.093 3004-10085/? V/FA-SVC: Scheduling upload with DelayedRunnable 03-10 10:53:55.093 3004-10085/? V/FA-SVC: Scheduling upload with GcmTaskService 03-10 10:53:55.093 3004-10085/? V/FA-SVC: Scheduling task with Gcm. time: 14891 03-10 10:53:55.105 3004-10085/? V/FA-SVC: Background event processing time, ms: 30 03-10 10:53:55.170 1394-1394/? D/gralloc_ranchu: gralloc_alloc: Creating ashmem region of size 8298496 03-10 10:53:55.212 1913-1958/? W/RemoteFillService: Not handling { when=-6ms what=3 target=com.android.internal.os.HandlerCaller$MyHandler } as service for ComponentInfo{com.google.android.gms/com.google.android.gms.autofill.service.AutofillService} is already destroyed 03-10 10:53:55.224 1913-1922/? I/zygote: Background concurrent copying GC freed 72697(4MB) AllocSpace objects, 18(376KB) LOS objects, 33% free, 11MB/17MB, paused 755us total 111.565ms 03-10 10:53:55.265 10051-10056/com.example.zafee.foodreviewapp I/zygote: JIT allocated 56KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int) 03-10 10:53:55.309 10051-10080/com.example.zafee.foodreviewapp D/EGL_emulation: eglMakeCurrent: 0xa89852a0: ver 2 0 (tinfo 0xa8983230) 03-10 10:53:55.328 10051-10056/com.example.zafee.foodreviewapp I/zygote: Do full code cache collection, code=193KB, data=149KB 03-10 10:53:55.329 10051-10056/com.example.zafee.foodreviewapp I/zygote: After code cache collection, code=151KB, data=89KB 03-10 10:53:55.346 10051-10080/com.example.zafee.foodreviewapp D/EGL_emulation: eglMakeCurrent: 0xa89852a0: ver 2 0 (tinfo 0xa8983230) 03-10 10:53:55.451 2152-2152/? I/GoogleInputMethod: onFinishInputView() : Dummy InputConnection bound 03-10 10:53:55.452 2152-2152/? I/PhenotypeExpConfig: refreshConfiguration() : Force = false : UpdateAvailable = false : Age = 74 minutes : MaxAge = 720 minutes 03-10 10:53:55.455 2152-2152/? I/GoogleInputMethod: onFinishInput() : Dummy InputConnection bound 03-10 10:53:55.455 2152-2152/? I/GoogleInputMethod: onStartInput() : Dummy InputConnection bound 03-10 10:53:55.464 1913-1960/? I/ActivityManager: Displayed com.example.zafee.foodreviewapp/.home: +923ms 03-10 10:53:55.531 2152-5906/? D/EGL_emulation: eglMakeCurrent: 0x8ebaf960: ver 2 0 (tinfo 0x8ebf46d0) 03-10 10:53:55.755 1913-1927/? D/AlarmManagerService: Kernel timezone updated to -240 minutes west of GMT 03-10 10:53:55.763 1913-5015/? D/AlarmManagerService: Setting time of day to sec=1520664836 03-10 10:53:56.008 1913-5015/? W/AlarmManagerService: Unable to set rtc to 1520664836: No such device 03-10 10:53:56.024 4098-4103/? I/zygote: Compiler allocated 4MB to compile int com.google.common.logging.nano.GsaClientLogProto$GsaClientEvent.computeSerializedSize() 03-10 10:53:56.465 5772-5827/? D/EGL_emulation: eglMakeCurrent: 0xa423dea0: ver 2 0 (tinfo 0x9347f160) 03-10 10:53:56.755 1913-1913/? W/WindowManager: removeWindowToken: Attempted to remove non-existing token: android.os.Binder@a07e56 03-10 10:53:57.500 1388-1648/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 6433649 , only wrote 6158160 03-10 10:53:57.736 1401-1401/? I/qemu-props: start adbd ... 03-10 10:53:58.280 1913-2131/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?). 03-10 10:53:58.605 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.al.a(150): onJobSchedulerWakeup 03-10 10:53:58.607 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ka(22): Scheduling fallback in 43199999 (absolute: 46402653) 03-10 10:53:58.608 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ka(22): Scheduling fallback in 64799999 (absolute: 68002654) 03-10 10:53:58.611 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.r.handleMessage(25): DeviceState: DeviceState{currentTime=1520664838609, isCharging=true, isIdle=false, netAny=true, netNotRoaming=true, netUnmetered=true} 03-10 10:53:58.615 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ma(87): Running job: 1 (12) 03-10 10:53:58.615 3458-3458/? I/Finsky: [2] com.google.android.finsky.ag.ca(41): ContentSyncJob started 03-10 10:53:58.617 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ma(159): RunningQueue size: 1 03-10 10:53:58.617 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ma(160): PendingQueue size: 0 03-10 10:53:58.740 3458-3488/? I/Finsky: [155] com.google.android.finsky.mca(25): Completed 0 account content syncs with 0 successful. 03-10 10:53:58.740 3458-3458/? I/Finsky: [2] com.google.android.finsky.ag.ca(5): Installation state replication succeeded. 03-10 10:53:58.741 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ai.b(12): jobFinished: 1 12 03-10 10:53:58.741 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ma(184): Job 1 (12) finished 03-10 10:53:58.741 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ma(159): RunningQueue size: 0 03-10 10:53:58.741 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.ma(160): PendingQueue size: 0

My POJO Class or fooddatabase class 我的POJO类或fooddatabase类

public final class fooddatabase {
// Excluding these fields because proguard can make them public
@Exclude
private String mTitle;
@Exclude private String mImage;

public fooddatabase() {
    // Needed for Firebase
}

@Keep
public String getTitle() {
    return mTitle;
}

@Keep
public void setTitle(String title) {
    mTitle = title;
}

@Keep
public String getImage() {
    return mImage;
}

// The "image" part of `setImage` needs to match what it's called in your database.
// Or you could use `@PropertyName("nameInTheDatabase")`
@Keep
public void setImage(String image) {
    mImage = image;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    fooddatabase model = (fooddatabase) o;

    return (mTitle == null ? model.mTitle == null : mTitle.equals(model.mTitle))
            && (mImage == null ? model.mImage == null : mImage.equals(model.mImage));
}

@Override
public int hashCode() {
    int result = mTitle == null ? 0 : mTitle.hashCode();
    result = 31 * result + (mImage == null ? 0 : mImage.hashCode());
    return result;
}

@Override
public String toString() {
    return "Model{" +
            "mTitle='" + mTitle + '\'' +
            ", mImage='" + mImage + '\'' +
            '}';
}
}

Since you are using the new version of FirebaseUI , then you have to do the following: 由于您使用的是新版本的FirebaseUI ,因此您必须执行以下操作:

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


 @Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}

The activity lifecycle goes through the following: 活动生命周期经历以下过程:

onCreate()>onStart()>onResume()

Inside onCreate() you are initializing the adapter. onCreate()您正在初始化适配器。

So inside onStart() you will start listening to the data retrieved to be able to see it in the screen. 因此,在onStart()内部,您将开始侦听检索到的数据,以便能够在屏幕上看到它。

Edit: 编辑:

You need to remove @Exclude 您需要删除@Exclude

public abstract @interface Exclude implements Annotation 公共抽象@interface排除实现注释

Marks a field as excluded from the Database. 将字段标记为从数据库中排除。

https://developers.google.com/android/reference/com/google/firebase/database/Exclude https://developers.google.com/android/reference/com/google/firebase/database/排除

and change the fields name to be the same as in your database. 并将字段名称更改为与数据库中的名称相同。

SO if in the database you have title and image then the field should be 因此,如果在数据库中具有titleimage则该字段应为

private String title;
private String image;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 FirebaseRecyclerAdapter 从 Firebase 实时数据库中检索数据到 Recycler 视图 - Retrieving data from Firebase Real time database into Recycler View using FirebaseRecyclerAdapter 无法从 firebase 数据库 android 中检索数据,回收站视图 - Unable to retrieve data from firebase database android, recycler view 当Firebase Database android恢复正常时,Recycler View更新 - Recycler View updates when acticity resumed with Firebase Database android Firebase Recycler View适配器(从Firebase检索) - Firebase Recycler View Adapter (Retrieving from firebase) Android &amp;&amp; Firebase实时数据库数据检索和写入 - Android && Firebase realtime database data retrieving and writing 适用于Android的Firebase数据库,检索数据时遇到麻烦 - Firebase Database for Android, trouble retrieving data 如何使用firebase实时数据库在回收器视图上使用拖放 - how to use drag and drop on recycler view using firebase realtime database Recycler 视图未使用数组列表显示来自 firebase 的数据 - Recycler view is not displaying data from firebase using an array list FireBase 实时数据库:回收器视图未填充 - FireBase Realtime Database : Recycler View not populating 我的 Firebase 没有从 Firebase 数据库中检索数据 - My Firebase is not retrieving data from Firebase Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM