Firebase Recycler Adapter 没有显示任何内容

Firebase Recycler Adapter is not showing anything

我正在制作一个片段,其中我在 recyclerview 中显示一些数据。但它没有显示任何错误事件。 我没有使用 firebase 的任何 @PropertyName 这是我的 firebase 数据库快照

NewCustomer.java(我的模块class)

package com.example.lenovo.jdstudio;

import java.util.Map;

public class NewCustomer {
    private String fName, lName, email, phone, photodesc, status;
    private Map time;

    public NewCustomer() {}

    public NewCustomer(String fName, String lName, String email, String phone, String photodesc, String status, Map time) {
        this.fName = fName;
        this.lName = lName;
        this.email = email;
        this.phone = phone;
        this.photodesc = photodesc;
        this.status = status;
        this.time = time;
    }

    public String getfName() {return fName;}

    public String getlName() {return lName;}

    public String getEmail() {return email;}

    public String getPhone() {return phone;}

    public String getPhotodesc() {return photodesc;}

    public String getStatus() {return status;}

    public Map getTime() {return time;}
}

Updaterder.java

public class UpdateOrder extends Fragment {

    private View mView;
    @BindView(R.id.customerDetailsRecyclerView)
    RecyclerView mCustmoerDetails;

    private FirebaseDatabase mDatabase;
    private DatabaseReference mCustomerDatabase;
    private FirebaseRecyclerAdapter firebaseRecyclerAdapter;
    private LinearLayoutManager mManager;
    private static final String TAG = UpdateOrder.class.getSimpleName();
    public UpdateOrder() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_update_order, container, false);
        ButterKnife.bind(this,mView);

        mCustmoerDetails.setHasFixedSize(true);
        mManager = new LinearLayoutManager(getActivity());
        mCustmoerDetails.setLayoutManager(mManager);

        Log.e(TAG,"Before firebaseoption");

        mDatabase = FirebaseDatabase.getInstance();
        mCustomerDatabase = mDatabase.getReference().child("new_customer");
        FirebaseRecyclerOptions<NewCustomer> options =
                new FirebaseRecyclerOptions.Builder<NewCustomer>().
                        setQuery(mCustomerDatabase,NewCustomer.class).
                        build();

        //        Log.e(TAG,"OnStart");

        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<NewCustomer, CustDetailHolder> (options){
            @Override
            protected void onBindViewHolder(@NonNull CustDetailHolder holder, int position, @NonNull NewCustomer model) {
                holder.setFname(model.getfName());
                holder.setLname(model.getlName());
                holder.setPhotoDetail(model.getPhotodesc());
            }

            @Override
            public CustDetailHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.single_order_details, parent, false);
                Log.e(TAG,"oncreate is called");

                return new CustDetailHolder(view);
            }
        };
        mCustmoerDetails.setAdapter(firebaseRecyclerAdapter);

        return mView;
    }

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

    }

    @Override
    public void onStop() {
        super.onStop();
        firebaseRecyclerAdapter.stopListening();
    }

    //ViewHolder class
    public static class CustDetailHolder extends RecyclerView.ViewHolder {
        View mView;

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

        public void setFname(String  name) {
            TextView FName = (TextView) mView.findViewById(R.id.cust_Fname);
            FName.setText(name);
        }


        public void setLname(String name) {
            TextView LName = (TextView) mView.findViewById(R.id.cust_Lname);
            LName.setText(name);
        }

        public void setPhotoDetail(String photoDetail) {
            TextView photo_details = (TextView) mView.findViewById(R.id.photo_Details);
            photo_details.setText(photoDetail);
        }

        public void setOrderSpinner(String status){

        }
    }


}

single_order_detail.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <LinearLayout
            android:id="@+id/cust_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="@dimen/dimen_15dp">

            <TextView
                android:id="@+id/cust_Fname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Fname"
                android:textSize="16dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/cust_Lname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="3dp"
                android:text="Lname"
                android:textSize="16dp"
                android:textStyle="bold" />

        </LinearLayout>


        <TextView
            android:id="@+id/order_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/cust_name"
            android:paddingLeft="@dimen/dimen_15dp"
            android:paddingTop="@dimen/dimen_10dp"
            android:text="order Status goes here"
            android:textSize="15sp" />

        <Spinner
            android:id="@+id/order_status_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/cust_name"
            android:layout_toRightOf="@id/order_status"
            />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/order_status"
            android:paddingLeft="15dp"
            android:paddingTop="@dimen/dimen_10dp"
            android:text="Photo Description:"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/photo_Details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/textView"
            android:layout_below="@+id/textView"
            android:paddingRight="@dimen/dimen_15dp"
            android:paddingTop="@dimen/dimen_5dp"
            android:text="4x6 id and passport"
            android:textSize="12sp" />
    </RelativeLayout>

</android.support.v7.widget.CardView>

fragment_update_order.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.lenovo.jdstudio.UpdateOrder">

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

    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

感谢您看到 post。等待答案。 如果我做错了什么,请纠正我。 不要在不阅读的情况下将其标记为重复。 我附上了所有的听众。

正如我在您的数据库中看到的那样,您的字段 而不是 对应于您的模型 class 中的字段。要解决这个问题,删除旧数据,添加新数据,您的问题将得到解决。

正如我在您的代码中看到的,您在 CustDetailHolder class 的声明中使用了 static 关键字,这是错误的。您只需要:

public class CustDetailHolder extends RecyclerView.ViewHolder

同时删除 ButterKnife.bind(this,mView);,因为我发现您没有在代码中使用它。

模型中的字符串 class:

fName
lName

必须与你的数据库中的相同,在你的数据库中你有这个:

FName
LName

对于检查此内容的任何人:

如我在评论中所说的那样解决了:

@Aakash keep it, but put this instead mCustmoerDetails.setHasFixedSize(false) – Peter Haddad yesterday

OP 必须添加 mCustmoerDetails.setHasFixedSize(false) 而不是 mCustmoerDetails.setHasFixedSize(true)

或移除mCustmoerDetails.setHasFixedSize(true)

伙计,也许你忘记打电话了 firebaseRecyclerAdapter.startListening 在你的@override onStart(){}

我遇到了和你描述的一样的问题,谢谢分享问题和意见。我跟着他们,发现我应该从回收站视图中删除 setHasFixedSize(true) 。此外,我应该分别使用开始和停止侦听适配器来覆盖 onStart 和 onStop 方法。这解决了我 在回收站视图 上不显示任何内容的问题。

P.S:我知道您早就找到了答案并解决了问题。我只是在这里分享这个结论,因为它不能直接从讨论中提取出来。