简体   繁体   中英

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 . 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. 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

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

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:

@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.

So inside onStart() you will start listening to the data retrieved to be able to see it in the screen.

Edit:

You need to remove @Exclude

public abstract @interface Exclude implements Annotation

Marks a field as excluded from the Database.

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

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

private String title;
private String image;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM