rut_0_1
rut_0_1

Reputation: 761

Firebase Recycler Adapter not working with recycler view of children

I have a database where my data looks like this.

enter image description here

I have used Firebase Recycler Adapter to the info for S1, S2 and so on, to represent that data in a recycler view. However the data is not showing only in my application. The application is for a teacher to see whether the student is present or not by retrieving data from Firebase.

AttendaceFrag.java

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

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 android.widget.Toast;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;



public class AttendanceFrag extends Fragment {

    RecyclerView recyclerView;
    DatabaseReference databaseReference;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.activity_attendance_frag, container, false);

        recyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);

        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));


        databaseReference = FirebaseDatabase.getInstance().getReference().child("Student");


        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot != null) {
                    Toast.makeText(getContext(), "Data to show", Toast.LENGTH_LONG).show();
                    startShowing();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        return v;
    }

    private void startShowing() {
        FirebaseRecyclerAdapter<Student, AttendanceFrag.RequestViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Student, AttendanceFrag.RequestViewHolder>(
                Student.class,
                R.layout.custom_row,
                AttendanceFrag.RequestViewHolder.class,
                databaseReference
        ) {
            @Override
            protected void populateViewHolder(AttendanceFrag.RequestViewHolder viewHolder, Student model, int position) {

                viewHolder.setName(model.getName());
                viewHolder.setAp(model.getAp());
                viewHolder.setRegno(model.getRegno());
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapter);
    }

    public static class RequestViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public RequestViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setName(String name) {
            TextView stname = (TextView) mView.findViewById(R.id.st_name);
            stname.setText(name);
        }

        public void setRegno(String regno) {
            TextView stregno = (TextView) mView.findViewById(R.id.st_regno);
            stregno.setText(regno);
        }

        public void setAp(String ap) {
            TextView stap = (TextView) mView.findViewById(R.id.st_ap);
            stap.setText(ap);
        }
    }
}

Student.java

public class Student {

    private String name, regno,ap;

    public Student(String name, String regno, String ap) {
        this.name = name;
        this.regno = regno;
        this.ap = ap;
    }

    public Student() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRegno() {
        return regno;
    }

    public void setRegno(String regno) {
        this.regno = regno;
    }

    public String getAp() {
        return ap;
    }

    public void setAp(String ap) {
        this.ap = ap;
    }
}

activity_attendance_frag.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:padding="3dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.xxx.AttendanceFrag">

    <TableLayout
        android:id="@+id/table_lay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TableRow
            android:id="@+id/firstrow"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:background="@drawable/r1"
                android:text="Sr. No."
                android:id="@+id/srnohead"
                android:textAlignment="center"
                android:padding="5dp"
                android:textColor="@color/colorPrimaryDark"
                />
            <TextView
                android:padding="5dp"
                android:textAlignment="center"
                android:background="@drawable/r1"
                android:text="Reg. Number"
                android:layout_width="135dp"
                android:id="@+id/regNohead"
                android:textColor="@color/colorPrimaryDark"/>
            <TextView
                android:padding="5dp"
                android:background="@drawable/r1"
                android:text="Name"
                android:textAlignment="center"
                android:layout_height="wrap_content"
                android:layout_width="120dp"
                android:id="@+id/namehead"
                android:textColor="@color/colorPrimaryDark"/>
            <TextView
                android:padding="5dp"
                android:background="@drawable/r1"
                android:text="P/A"
                android:layout_height="wrap_content"
                android:id="@+id/attnhead"
                android:textColor="@color/colorPrimaryDark"/>
        </TableRow>
    </TableLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/table_lay"
        android:layout_alignParentStart="true"
        android:layout_marginTop="33dp" />

</RelativeLayout>

custom_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:padding="3dp">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/r1"
        android:text="Sr. No."
        android:id="@+id/st_sr"
        android:textAlignment="center"
        android:padding="5dp"
        android:textColor="@color/colorPrimaryDark"
        />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="135dp"
        android:padding="5dp"
        android:textAlignment="center"
        android:background="@drawable/r1"
        android:text="Reg. Number"
        android:id="@+id/st_regno"
        android:textColor="@color/colorPrimaryDark"/>
    <TextView
        android:padding="5dp"
        android:background="@drawable/r1"
        android:text="Name"
        android:textAlignment="center"
        android:layout_height="wrap_content"
        android:layout_width="120dp"
        android:id="@+id/st_name"
        android:textColor="@color/colorPrimaryDark"/>
    <TextView
        android:layout_width="wrap_content"
        android:padding="5dp"
        android:background="@drawable/r1"
        android:text="P/A"
        android:layout_height="wrap_content"
        android:id="@+id/st_ap"
        android:textColor="@color/colorPrimaryDark"/>
</LinearLayout>

Upvotes: 2

Views: 6730

Answers (2)

vinay mishra
vinay mishra

Reputation: 31

put this in your App level gradle : implementation 'com.firebaseui:firebase-ui-database:3.2.1'

In your onStart method put this :

@Override
    protected void onStart() {
        super.onStart();

        Query query = FirebaseDatabase.getInstance()
                .getReference()
                .child("Blog")
                .limitToLast(50);
        FirebaseRecyclerOptions<BlogModel> options =
                new FirebaseRecyclerOptions.Builder<BlogModel>()
                        .setQuery(query, BlogModel.class)
                        .build();

        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<BlogModel, BlogViewHolder>(options) {
            @Override
            public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.row_blog, parent, false);

                return new BlogViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(BlogViewHolder holder, int position, BlogModel model) {
                holder.setTitleBlog(model.getTitle());
                holder.setTitleDesc(model.getDescription());
                holder.setImage(getApplicationContext(), model.getImage_url());
            }
        };
        adapter.startListening();
        mRecycler.setAdapter(adapter);

    }

in your BlogViewHolder put this :

 public static class BlogViewHolder extends RecyclerView.ViewHolder {
        View view;

        public BlogViewHolder(View itemView) {
            super(itemView);
            view = itemView;
        }

        public void setTitleBlog(String title) {
            TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
            tvTitle.setText(title);

        }

        public void setTitleDesc(String desc) {
            TextView tvDesc = (TextView) view.findViewById(R.id.tv_desc);
            tvDesc.setText(desc);
        }


        public void setImage(Context context, String url) {
            ImageButton ib = (ImageButton) view.findViewById(R.id.ib_blog);
            Picasso.with(context).load(url).into(ib);

        }
    }

And your are all set to go :)

Upvotes: 3

ogulcan
ogulcan

Reputation: 53

Your database reference pointing wrong node you can use it like this:

FirebaseDatabase.getInstance().getReferenceFromUrl("https://facapp-bee2.firebaseio.com/");

Then .child("Faculty/Student") and recylerview don't use addValueListener. Here you can find how to use recycler view in firebase

Upvotes: 0

Related Questions