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

时间:2018-03-10 06:57:43

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

我正在尝试绑定我的firebase数据库中的数据,该数据库包含Food>格式的数据。图像,文字。即带有两个孩子的儿童食品图像和文字。不幸的是,当我运行应用程序并尝试检索它时,它不会显示任何内容。我相信Recycler视图代码似乎很好。如果您知道解决方案,请帮助我。 我在下面附上了我的logcat。 这是我的数据绑定的代码

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

这是我的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.k.a(22): Scheduling fallback in 43199999 (absolute: 46402653) 03-10 10:53:58.608 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.k.a(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.m.a(87): Running job: 1 (12) 03-10 10:53:58.615 3458-3458/? I/Finsky: [2] com.google.android.finsky.ag.c.a(41): ContentSyncJob started 03-10 10:53:58.617 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.m.a(159): RunningQueue size: 1 03-10 10:53:58.617 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.m.a(160): PendingQueue size: 0 03-10 10:53:58.740 3458-3488/? I/Finsky: [155] com.google.android.finsky.m.c.a(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.c.a(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.m.a(184): Job 1 (12) finished 03-10 10:53:58.741 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.m.a(159): RunningQueue size: 0 03-10 10:53:58.741 3458-3458/? I/Finsky: [2] com.google.android.finsky.scheduler.m.a(160): PendingQueue size: 0

我的POJO课程或食物数据库课程

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 + '\'' +
            '}';
}
}

1 个答案:

答案 0 :(得分:0)

由于您使用的是FirebaseUI的新版本,因此您必须执行以下操作:

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


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

活动生命周期如下:

onCreate()>onStart()>onResume()

onCreate()内部,您正在初始化适配器。

因此,在onStart()内,您将开始收听检索到的数据,以便能够在屏幕上看到它。

编辑:

您需要删除@Exclude

  

public abstract @interface Exclude implements Annotation

     

将字段标记为从数据库中排除。

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

并将字段名称更改为与数据库中的名称相同。

如果您在数据库中有titleimage,那么该字段应为

private String title;
private String image;