Recyler视图未显示任何内容

时间:2018-06-29 16:31:03

标签: firebase android-recyclerview google-cloud-firestore recyclerview-layout

我设置了一个回收站视图以显示来自Firestore的数据。我几乎一切都做对了,但是当我运行我的应用程序时,它没有显示任何数据。有人请帮助我。我在这里附加了我的代码。我添加了setLinearlayoutmanager并将recyclerview的可见性设置为可见。我试图从用户那里获取数据,并使用该数据从数据库中检索内容。

display_data.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"
        android:background="@drawable/loginbg"
        tools:context=".displayData">

        <EditText
            android:id="@+id/deptName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginStart="41dp"
            android:layout_marginTop="41dp"
            android:ems="10"
            android:hint="Enter dept eg:IT"
            android:inputType="textPersonName"
            android:textColorHint="@android:color/white"
            android:textColorLink="@android:color/black"
            tools:textColor="@android:color/background_light" />

        <Button
            android:id="@+id/getData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/deptName"
            android:layout_toEndOf="@+id/deptName"
            android:background="@drawable/buttonshape"
            android:text="Get" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/mainlist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="120dp"
            android:visibility="visible" />

    </RelativeLayout>

我的Java代码display.java:

    public class displayData extends AppCompatActivity implements View.OnClickListener {
        RecyclerView mlist;
        EditText deptName;
        FirebaseFirestore db3;
        public List<Users> usersList;
        UsersListAdaptor usersListAdaptor;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_display_data);
            findViewById(R.id.getData).setOnClickListener(this);
            deptName=findViewById(R.id.deptName);
            mlist=(RecyclerView) findViewById(R.id.mainlist);
            mlist.setHasFixedSize(true);
            mlist.setLayoutManager(new LinearLayoutManager(this));
            mlist.setAdapter(usersListAdaptor);
            db3=FirebaseFirestore.getInstance();
            usersList=new ArrayList<>();
            usersListAdaptor=new UsersListAdaptor(usersList);
        }

        public void onClick(View view) {
            switch (view.getId()){
                case R.id.getData:
                    getdata();
                    break;
            }
        }
    private void getdata() {
        String deptname=deptName.getText().toString().trim().toLowerCase();
        CollectionReference cref=db3.collection("bookingdetails");
        Query q2=cref.whereEqualTo("dept",deptname);
        q2.addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                if(e!=null){
                    Toast.makeText(getApplicationContext(),"Something went wrong",Toast.LENGTH_SHORT).show();
                }
                for(DocumentChange doc:queryDocumentSnapshots.getDocumentChanges()){
                        Users users = doc.getDocument().toObject(Users.class);
                        usersList.add(users);
                        usersListAdaptor.notifyDataSetChanged();
                }
            }
        });
    }
}

users.class

    package com.example.sarukesi.seminarbook;

    public class Users {
        String name;
        String dept;
        String time;
        String date;

        public Users() {
        }
        public Users(String name, String dept, String time, String date) {
            this.name = name;
            this.dept = dept;
            this.time = time;
            this.date = date;
        }
        public String getName() {
            return name;
        }

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

        public String getDept() {
            return dept;
        }

        public void setDept(String dept) {
            this.dept = dept;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }


    }

UsersListAdapter:

    public class UsersListAdaptor extends RecyclerView.Adapter<UsersListAdaptor.ViewHolder> {

        List<Users> usersList;

        public UsersListAdaptor(List<Users> usersList) {
            this.usersList = usersList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item,viewGroup,false);
            return  new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
            viewHolder.tname.setText(usersList.get(i).getName());
            viewHolder.tdept.setText(usersList.get(i).getDept());
            viewHolder.ttime.setText(usersList.get(i).getTime());
            viewHolder.tdate.setText(usersList.get(i).getDate());
        }

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

        public class ViewHolder extends RecyclerView.ViewHolder{

            View mview;
            public TextView tname,tdept,ttime,tdate;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                mview=itemView;
                tname=itemView.findViewById(R.id.nametxt);
                tdept=itemView.findViewById(R.id.depttxt);
                ttime=itemView.findViewById(R.id.timetxt);
                tdate=itemView.findViewById(R.id.datetxt);
            }
        }
    }

list_item.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"
        android:padding="20dp">

        <TextView
            android:id="@+id/nametxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="monospace"
            android:padding="15dp"
            android:text="Name"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/depttxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="serif-monospace"
            android:padding="15dp"
            android:text="Dept"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/timetxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="cursive"
            android:padding="15dp"
            android:text="Time"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/datetxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="serif-monospace"
            android:padding="15dp"
            android:text="Date"
            android:textSize="16sp" />
    </LinearLayout>

1 个答案:

答案 0 :(得分:0)

        mlist.setAdapter(usersListAdaptor);
        db3=FirebaseFirestore.getInstance();
        usersList=new ArrayList<>();
        usersListAdaptor=new UsersListAdaptor(usersList);

您可以在分配旧适配器后创建新适配器。将第一行移到该块的末尾