阅读Firebase数据库并在RecyclerView中显示:Android

时间:2017-12-22 10:54:03

标签: android firebase firebase-realtime-database android-recyclerview

这是我的代码:

Liste.java文件:

public class Liste extends AppCompatActivity{

    RecyclerView myRecyclerview;
    MyAdapter adapter;
    List<Dentist> listDentist;
    FirebaseDatabase FDB;
    DatabaseReference DBR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_liste );

        myRecyclerview = (RecyclerView) findViewById ( R.id.myRecycler );
        RecyclerView.LayoutManager LM = new LinearLayoutManager ( getApplicationContext ());
        myRecyclerview.setLayoutManager ( LM );
        myRecyclerview.setItemAnimator ( new DefaultItemAnimator () );
        myRecyclerview.addItemDecoration ( new DividerItemDecoration ( getApplicationContext (),LinearLayoutManager.VERTICAL ) );

        listDentist = new ArrayList<> (  );

        adapter = new MyAdapter (listDentist);

        FDB= FirebaseDatabase.getInstance ();
        GetDataFirebase();

 void GetDataFirebase(){
        DBR = FDB.getReference ("dentistes");

        DBR.addChildEventListener ( new ChildEventListener ( ) {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                Dentist data = dataSnapshot.getValue (Dentist.class);
                //Add to Arraylist
                listDentist.add ( data );
                //Add list into adapter
                myRecyclerview.setAdapter ( adapter );


            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        } );
    }


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

        List<Dentist> listArray;

        public MyAdapter (List<Dentist> List){
            this.listArray = List;
        }

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

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            Dentist data = listArray.get ( position );
            holder.MyText.setText ( data.getName () );
        }
        public class MyViewHolder extends RecyclerView.ViewHolder{
            TextView MyText;

            public MyViewHolder(View itemView) {
                super ( itemView );
                MyText = (TextView)itemView.findViewById ( R.id.textview_dentist );
            }
        }

        @Override
        public int getItemCount() {
            return listArray.size ();
        }
    }
}

activity_liste.xml文件

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

       <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/myRecycler">
        </android.support.v7.widget.RecyclerView>
</LinearLayout>

iteview.xml文件

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textview_dentist"
            android:layout_marginBottom="15dp"
            android:layout_marginTop="15dp"
            android:layout_marginLeft="15dp"
            android:textSize="20sp"/>

    </LinearLayout>

我的班级模特:

public class Dentist {
    private String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }
}

我有两个问题:

  • 我的Recyclerview中正确显示了牙医列表,但logcat显示了我&#34; E / RecyclerView:没有连接适配器;跳过布局&#34;:我该如何纠正?

  • 如果我更改了我的班级模型的结构:例如我添加地址和TelNumber:我应该如何更改我的代码,以便在我的Recyclerview中显示forent Dentist他的名字,地址和NumTel?

2 个答案:

答案 0 :(得分:1)

要解决此问题,请参阅下面的答案:

  1. 移动此行代码

    adapter = new MyAdapter(listDentist);
    
  2. 内部onChildAdded()方法。必须在此方法中声明和使用您的适配器。

    1. 您需要通过添加这些私有字段并添加相应的公共设置器和getter来更改Dentist类。

答案 1 :(得分:0)

要摆脱logcat中的警告,请在onCreate方法中设置适配器,并在onChildAdded方法中调用adapter.notifyDataSetChanged()来更新值...

    public class Liste extends AppCompatActivity{

    RecyclerView myRecyclerview;
    MyAdapter adapter;
    List<Dentist> listDentist;
    FirebaseDatabase FDB;
    DatabaseReference DBR;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_liste );

        myRecyclerview = (RecyclerView) findViewById ( R.id.myRecycler );
        RecyclerView.LayoutManager LM = new LinearLayoutManager ( getApplicationContext ());
        myRecyclerview.setLayoutManager ( LM );
        myRecyclerview.setItemAnimator ( new DefaultItemAnimator () );
        myRecyclerview.addItemDecoration ( new DividerItemDecoration ( getApplicationContext (),LinearLayoutManager.VERTICAL ) );

        listDentist = new ArrayList<> (  );

        adapter = new MyAdapter (listDentist);

       //Set empty adapter here
        myRecyclerView.setAdapter(adapter);

        FDB= FirebaseDatabase.getInstance ();
        GetDataFirebase();

 void GetDataFirebase(){
        DBR = FDB.getReference ("dentistes");

        DBR.addChildEventListener ( new ChildEventListener ( ) {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                Dentist data = dataSnapshot.getValue (Dentist.class);
                //Add to Arraylist
                listDentist.add ( data );
                //Add list into adapter


               adapter.notifyDataSetChanged();

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        } );
    }


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{

        List<Dentist> listArray;

        public MyAdapter (List<Dentist> List){
            this.listArray = List;
        }

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

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            Dentist data = listArray.get ( position );
            holder.MyText.setText ( data.getName () );
        }
        public class MyViewHolder extends RecyclerView.ViewHolder{
            TextView MyText;

            public MyViewHolder(View itemView) {
                super ( itemView );
                MyText = (TextView)itemView.findViewById ( R.id.textview_dentist );
            }
        }

        @Override
        public int getItemCount() {
            return listArray.size ();
        }
    }
}

对于Alex Mamo提到的第二个问题,您需要在牙医课程中添加您想要的字段以及相应的getter和setter。