-1

After uptade syntax of FirebaseUI, can't work without onPopulateViewHolder method. I read the doc of FirebaseUI and did the same. After running app, RecycleView is running without mistake, but it empty. The data of my Firebase didn't appear. I read that the common mistake is that RecycleView used the parameter wrap content or setHasFixedSize(true). I did everything the same way, but the RecycleView is still empty. What is wrong?

1.Gradle:

apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.eugene.lafinalproduction"
    minSdkVersion 15
    targetSdkVersion 27
    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'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.google.firebase:firebase-database:15.0.0'
implementation 'com.google.firebase:firebase-storage:15.0.0'
implementation 'com.google.firebase:firebase-auth:15.0.0'
implementation 'com.firebaseui:firebase-ui-database:3.2.2'
implementation 'com.android.support:recyclerview-v7:27.1.1'
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'
//Glide library
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
}
apply plugin: 'com.google.gms.google-services'

2.Recycle_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".Recycle_activity">

<android.support.v7.widget.RecyclerView
    android:id="@+id/list_result"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:scrollbars="vertical"/>
</RelativeLayout>
  1. Item_list.xml - my each row in RV.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ImageView
        android:id="@+id/image_id"
        android:layout_width="330dp"
        android:layout_height="200dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        app:srcCompat="@drawable/img_2" />
    
    <TextView
        android:id="@+id/text_image_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:textColor="@color/myTextColor"
        android:textSize="85sp" />
    

  2. Recycle_activity.java, with onCreate, RecycleViewAdapter, ViewHolder

    public class Recycle_activity extends AppCompatActivity {
    
    private RecyclerView mResultList;
    private DatabaseReference mPlaceDatabase;
    private Query query;
    private FirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter;
    private DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    private DatabaseReference usersRef = rootRef.child("Users");
    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_activity);
    
        mResultList = findViewById(R.id.list_result);
    
        mPlaceDatabase = FirebaseDatabase.getInstance().getReference();
        query = mPlaceDatabase.child("Users");
    
        //mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));
        //mLinearLayouManager = new LinearLayoutManager(this);
        FirebaseRecyclerOptions<Places> options =
                new FirebaseRecyclerOptions.Builder<Places>()
                        .setQuery(query, Places.class)
                        .build();
    
        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Places, PlaceViewHolder>(options) {
            @Override
            protected void onBindViewHolder(PlaceViewHolder holder, int position, Places model) {
    
                holder.setDetails(getApplicationContext(), model.getName(), model.getImage());
                Toast.makeText(getApplicationContext(), model.getName(), Toast.LENGTH_SHORT).show();
    
            }
    
            @Override
            public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_layout, parent, false);
                return new PlaceViewHolder(view);
            }
    
        };
        mResultList.setAdapter(firebaseRecyclerAdapter);
        ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    String name = ds.child("name").getValue(String.class);
                    Log.d("TAG", name);
                    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
    
                }
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        };
        usersRef.addListenerForSingleValueEvent(valueEventListener);
    }
    @Override
    protected void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }
    
    class PlaceViewHolder extends RecyclerView.ViewHolder {
    
        View mView;
    
        public PlaceViewHolder(View itemView) {
            super(itemView);
    
            mView = itemView;
        }
    
        public void setDetails(Context context, String placeName, String placeImage) {
    
            TextView place_Name = mView.findViewById(R.id.image_id);
            ImageView place_Image = mView.findViewById(R.id.text_image_id);
    
            place_Name.setText(placeName);
            Glide.with(context).load(placeImage).into(place_Image);
        }
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if (firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
    

    }

  3. Places.java with getters,setters:

    public class Places {
    
    public String name_place, image_place;
    
    public Places() {
    
    }
    
    public String getName_place() {
        return name_place;
    }
    
    public void setName_place(String name_place) {
        this.name_place = name_place;
    }
    
    public String getImage_place() {
        return image_place;
    }
    
    public void setImage_place(String image_place) {
        this.image_place = image_place;
    }
    
    public Places(String name_place, String image_place) {
        this.name_place = name_place;
        this.image_place = image_place;
    }
    }
    

And my Firebase Database: Screen Database

p.s. All rules of .write .read are checked.

Update:

    public class Recycle_activity extends AppCompatActivity {

    private RecyclerView mResultList;
    private DatabaseReference mPlaceDatabase;
    private Query query;
    private FirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_activity);

        mResultList = findViewById(R.id.list_result);

        mPlaceDatabase = FirebaseDatabase.getInstance().getReference();
        query = mPlaceDatabase.child("Users");

        //mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(this));
        //mLinearLayouManager = new LinearLayoutManager(this);
        FirebaseRecyclerOptions<Places> options =
                new FirebaseRecyclerOptions.Builder<Places>()
                        .setQuery(query, Places.class)
                        .build();

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Places, PlaceViewHolder>(options) {
            @Override
            protected void onBindViewHolder(PlaceViewHolder holder, int position, Places model) {

                holder.setDetails(getApplicationContext(), model.getName(), model.getImage());

            }

            @Override
            public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.list_layout, parent, false);
                return new PlaceViewHolder(view);
            }

        };
        mResultList.setAdapter(firebaseRecyclerAdapter);
    }

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

    class PlaceViewHolder extends RecyclerView.ViewHolder {

        View mView;

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

            mView = itemView;
        }

        public void setDetails(Context context, String placeName, String placeImage) {

            TextView place_Name = mView.findViewById(R.id.image_id);
            ImageView place_Image = mView.findViewById(R.id.text_image_id);

            place_Name.setText(placeName);
            Glide.with(context).load(placeImage).into(place_Image);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
}

Updated database and storage: Firebase Database, Database Rules, Firebase Storage, Storage Rules.

Logs after launch:

    04-24 01:17:41.304 7093-7093/? I/art: Late-enabling -Xcheck:jni
04-24 01:17:41.345 7093-7093/com.example.eugene.lafinalproduction W/ResourceType: Found multiple library tables, ignoring...
04-24 01:17:41.353 7093-7093/com.example.eugene.lafinalproduction W/System: ClassLoader referenced unknown path: /data/app/com.example.eugene.lafinalproduction-2/lib/arm64
04-24 01:17:41.396 7093-7093/com.example.eugene.lafinalproduction W/ComponentDiscovery: Application info not found.
    Could not retrieve metadata, returning empty list of registrars.
04-24 01:17:41.439 7093-7093/com.example.eugene.lafinalproduction I/FirebaseInitProvider: FirebaseApp initialization successful
04-24 01:17:41.477 7093-7093/com.example.eugene.lafinalproduction W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-24 01:17:41.524 7093-7110/com.example.eugene.lafinalproduction I/FA: App measurement is starting up, version: 12451
    To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
    To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.example.eugene.lafinalproduction
04-24 01:17:41.589 7093-7112/com.example.eugene.lafinalproduction I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:6
    Selected remote version of com.google.android.gms.firebase_database, version >= 6
04-24 01:17:41.606 7093-7112/com.example.eugene.lafinalproduction W/ResourceType: Found multiple library tables, ignoring...
04-24 01:17:41.607 7093-7110/com.example.eugene.lafinalproduction I/FA: Tag Manager is not found and thus will not be used
04-24 01:17:41.613 7093-7093/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7f9d259920), client(37), share_fd(34)
04-24 01:17:41.627 7093-7112/com.example.eugene.lafinalproduction W/System: ClassLoader referenced unknown path: /data/data/com.google.android.gms/app_chimera/m/00000018/n/arm64-v8a
04-24 01:17:41.642 7093-7112/com.example.eugene.lafinalproduction W/ResourceType: Found multiple library tables, ignoring...
04-24 01:17:41.685 7093-7102/com.example.eugene.lafinalproduction I/System: FinalizerDaemon: finalize objects = 1
04-24 01:17:41.689 7093-7113/com.example.eugene.lafinalproduction E/GED: Failed to get GED Log Buf, err(0)
04-24 01:17:41.689 7093-7113/com.example.eugene.lafinalproduction I/OpenGLRenderer: Initialized EGL, version 1.4
04-24 01:17:41.699 7093-7113/com.example.eugene.lafinalproduction I/OpenGLRenderer: Get enable program binary service property (1)
    Initializing program atlas...
04-24 01:17:41.700 7093-7113/com.example.eugene.lafinalproduction I/OpenGLRenderer: Program binary detail: Binary length is 146348, program map length is 128.
    Succeeded to mmap program binaries. File descriptor is 42, and path is /dev/ashmem�.
    No need to use file discriptor anymore, close fd(42).
04-24 01:17:41.709 7093-7113/com.example.eugene.lafinalproduction W/libEGL: [ANDROID_RECORDABLE] format: 1
04-24 01:17:41.710 7093-7113/com.example.eugene.lafinalproduction I/PerfService: PerfServiceNative api init
04-24 01:17:41.716 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa7631fa0), client(37), share_fd(44)
04-24 01:17:41.738 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa7631aa0), client(37), share_fd(47)
04-24 01:17:41.741 7093-7131/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
04-24 01:17:41.741 7093-7131/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: netid=0; mark=0
04-24 01:17:41.742 7093-7131/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
    [getaddrinfo]: netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
04-24 01:17:41.743 7093-7093/com.example.eugene.lafinalproduction I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@5d4e48d time:222822867
04-24 01:17:41.746 7093-7131/com.example.eugene.lafinalproduction D/libc-netbsd: getaddrinfo: get result from proxy gai_error = 7
04-24 01:17:41.746 7093-7131/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS]Unable to resolve host "lafinalproduction.firebaseio.com": No address associated with hostname
04-24 01:17:42.574 7093-7135/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
04-24 01:17:42.574 7093-7135/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
04-24 01:17:43.242 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa7632180), client(37), share_fd(49)
04-24 01:17:43.256 7093-7113/com.example.eugene.lafinalproduction I/[MALI][Gralloc]: [+]r_hnd(0x7fa76322c0), client(37), share_fd(51)
04-24 01:17:43.767 7093-7136/com.example.eugene.lafinalproduction I/System.out: [CDS][DNS] getAllByNameImpl netId = 0
04-24 01:17:43.767 7093-7136/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: netid=0; mark=0
04-24 01:17:43.768 7093-7136/com.example.eugene.lafinalproduction D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
    [getaddrinfo]: netid=0; mark=0
    [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
Spike Johnson
  • 365
  • 4
  • 12
  • 1
    check this link: https://stackoverflow.com/questions/47228262/firebaselistadapter-not-pushing-individual-items-for-chat-app-firebase-ui-3-1/47228433#47228433 – Peter Haddad Apr 23 '18 at 18:54

1 Answers1

3

To solve this, please follow the next steps:

  1. change your model to look like this:

    public class Places {
        private String image, name;
    
        public Places() { }
    
        public Places(String image, String name) {
            this.image = image;
            this.name = name;
        }
    
        public String getImage() { return image; }
        public String getName() { return name; }
    }
    

    The fields from your model class should look exactly like the one from your database. In your code are different. See name_place vs. name.

  2. Make your firebaseRecyclerAdapter varaible global:

    private FirebaseRecyclerAdapter<Places, PlaceViewHolder> firebaseRecyclerAdapter;
    
  3. Remove FirebaseRecyclerAdapter<Places, PlaceViewHolder> from the onCreate() method.

  4. Add the following lines of code in the onStart() and onStop() methods.

    @Override
    protected void onStart() {
        super.onStart();
        firebaseRecyclerAdapter.startListening();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if(firebaseRecyclerAdapter != null) {
            firebaseRecyclerAdapter.stopListening();
        }
    }
    

This is a complete example on how you can retrieve data from a Firebase Realtime database and display it in a RecyclerView using FirebaseRecyclerAdapter.

Edit:

To simply display those names in the logcat, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            Log.d("TAG", name);
            Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thanks for help @Alex Mamo. but i didn't clearly understand, where i must declare onStart, onStop method in code. Any place aside from onCreate? And with making `firebaseRecycleAdapter` global, i did it main body and delete my method `firebasePlaceSearch()` from onCreate, but how correctly declaring my adapter after it? When i did all that you said, there are an error: "Attempt to invoke virtual method 'void com.firebase.ui.database.FirebaseRecyclerAdapter.startListening()' on a null object reference" Something go wrong.. – Spike Johnson Apr 23 '18 at 20:11
  • You need to override both methods `onStart` and `onStop` in your activity class. Should look like your `onCreate` method. You are getting that error because your adapter has not been initialised. So move the `options` variable inside the `onCreate` method ans also all that code within your `firebasePlaceSearch` method right after the declaration of your `options` variable. – Alex Mamo Apr 23 '18 at 20:26
  • Ok, i did everything, that you tell me @Alex Mamo. But again RecycleView is empty, i try to recreate Database, when app was running, but it still emty. I edit the code in my question, like you said, can you look at it? – Spike Johnson Apr 23 '18 at 20:45
  • I cannot see your edited post. Remove all the data and add freash data. Does it work? – Alex Mamo Apr 23 '18 at 20:47
  • Why are you can't @Alex Mamo? I refresh it in my text question in "4" point. I run the app, then delete all data in Firebase console and add some - nothing, `RecycleView` still empty.. – Spike Johnson Apr 23 '18 at 20:56
  • Please share your entire class, to see the code more clearly. – Alex Mamo Apr 23 '18 at 20:57
  • Ok. i did it, at the bottom of my question in the update field @Alex Mamo – Spike Johnson Apr 23 '18 at 21:01
  • Your code is correct. Can you please share again your database that contains the new data? – Alex Mamo Apr 23 '18 at 21:06
  • Sure, did it. In the bottom again @Alex Mamo – Spike Johnson Apr 23 '18 at 21:16
  • Try to add this line of code `Log.d("TAG", model.getName());` in your `onBindViewHolder` method. Does it log all the names? – Alex Mamo Apr 23 '18 at 21:45
  • Nope. There are nothing like that @Alex Mamo. Does deleting project in Firebase console and creating new, than connecting with Android Studio can solve that? – Spike Johnson Apr 23 '18 at 22:03
  • No, it has nothing to do with recreating the project. Have you checked your logcat and is empty when adding this line: `Log.d("TAG", model.getName());`? Search the logat after `TAG`. – Alex Mamo Apr 23 '18 at 22:13
  • Yep, nothing. I posted the logs in question @Alex Mamo – Spike Johnson Apr 23 '18 at 22:20
  • Try to change that line with: `Toast.makeText(getApplicationContext(), model.getName(), Toast.LENGTH_SHORT).show());`. What is happening? – Alex Mamo Apr 23 '18 at 22:25
  • Try, but nothing, no any toast messages..@Alex Mamo – Spike Johnson Apr 23 '18 at 22:30
  • Using the code from my updated answer, does it log/toast the names? – Alex Mamo Apr 23 '18 at 22:39
  • Yeeeeeesss!!!! The problem was primitive, in my method "setDetails" for TextView was find id for ImageView, and backward for ImageView. It so stupid! Thank you SO MUCH man for spending so much time with me. I'm very grateful to you! – Spike Johnson Apr 23 '18 at 23:07