【问题标题】:Firebase Recycler Adapter not working with recycler view of childrenFirebase Recycler Adapter 不适用于儿童的回收站视图
【发布时间】:2017-04-26 19:40:00
【问题描述】:

我有一个数据库,我的数据看起来像这样。

我已将 Firebase Recycler Adapter 用于 S1、S2 等的信息,以在回收器视图中表示该数据。但是,数据不仅显示在我的应用程序中。该应用程序供教师通过从 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);
        }
    }
}

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

【问题讨论】:

    标签: android firebase firebase-realtime-database firebaseui


    【解决方案1】:

    把它放在你的应用级 gradle 中: implementation 'com.firebaseui:firebase-ui-database:3.2.1'

    在你的 onStart 方法中放这个:

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

    在你的 BlogViewHolder 中放这个:

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

    你们都准备好了:)

    【讨论】:

      【解决方案2】:

      您的数据库引用指向错误的节点,您可以这样使用它:

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

      那么 .child("Faculty/Student") 和 recylerview 不要使用 addValueListener。 Here你可以找到如何在firebase中使用recycler view

      【讨论】:

        猜你喜欢
        • 2015-09-20
        • 1970-01-01
        • 1970-01-01
        • 2013-02-12
        • 2023-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-21
        相关资源
        最近更新 更多