我试图使用自定义适配器将数据从Firebase填充到RecyclerView中,运行应用程序后,屏幕空白,未显示我的recyclerView

时间:2019-02-13 23:56:50

标签: firebase-realtime-database android-recyclerview recycler-adapter populate data-retrieval

我正在开发RecyclerView并使用CustomAdapter,并且我的应用程序运行平稳,但是当我运行我的应用程序时,屏幕为空白,并且没有RecyclerView或其数据生成的任何视图。

public class prospectowner_listview extends AppCompatActivity {

        RecyclerView list;
        private DatabaseReference mFirebaseDatabase;
        private FirebaseDatabase mFirebaseInstance;
        Owner owner;
        customAdapter adapter;
        ArrayList <Owner> ownerlist = new ArrayList <>();


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



            list = findViewById(R.id.list);
            adapter= new customAdapter(prospectowner_listview.this, ownerlist);

            LinearLayoutManager manager = new LinearLayoutManager(getApplicationContext());
            list.setLayoutManager(manager);
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            list.setItemAnimator( new DefaultItemAnimator());
            list.setAdapter(adapter);
            adapter.addData(ownerlist);


            mFirebaseInstance = FirebaseDatabase.getInstance();
            mFirebaseDatabase = mFirebaseInstance.getReference("owners");
            mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot data : dataSnapshot.getChildren()) {
                        owner = data.getValue(Owner.class);
                        ownerlist.add(owner);

                    }}




                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });
        }
    }

我试图返回arraylist.size()并键入list.setHasFixedSize,但没有用!

    ----------------------- Adapter ----------------------



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

    private final ArrayList<com.example.android.e7gzlykora.Owner> ownerlist;
    private Context mContext;

    public customAdapter(Context c, ArrayList<com.example.android.e7gzlykora.Owner>  p) {
        this.mContext = c;
        this.ownerlist = p;
    }




    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
            viewType) {
        return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.prospectowners, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

        com.example.android.e7gzlykora.Owner owner = ownerlist.get(position);

        holder.name.setText(owner.getName());
        holder.field.setText(owner.getFieldname());
        holder.mobile.setText(owner.getMobile());
        holder.address.setText(owner.getAddress());
        holder.cost.setText(owner.getCost());
    }

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

    public void addData(ArrayList<com.example.android.e7gzlykora.Owner> ownerlist ) {
        this.ownerlist.addAll(ownerlist);
        notifyDataSetChanged();
    }





    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        public TextView field;
        public TextView mobile;
        public TextView address;
        public TextView cost;
        public Button bttn;


        public MyViewHolder(View itemView) {
            super(itemView);
            this.name = itemView.findViewById(R.id.name);
            this.field = itemView.findViewById(R.id.fieldName);
            this.mobile = itemView.findViewById(R.id.mobileowner);
            this.address = itemView.findViewById(R.id.address);
            this.cost = itemView.findViewById(R.id.cost);
            this.bttn = itemView.findViewById(R.id.reserve);
        }

    }
}

----------------------------------- owner_listview.xml ----------------------- ---

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/prospectowners"
        />

</LinearLayout>

--------------- futureowners.xml ----------------------------- ----

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


    <TextView
        android:id="@+id/fieldName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="@android:color/black"
        android:textStyle="bold"
         />

    <TextView
        android:id="@+id/nameowner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/fieldName"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:text=""
        android:textColor="@android:color/black"
        />

    <TextView
        android:id="@+id/mobileowner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/nameowner"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
         />

    <TextView
        android:id="@+id/address"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/mobileowner"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
         />

    <TextView
        android:id="@+id/cost"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/address"
        android:layout_marginStart="50dp"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="10dp"
        android:textColor="@android:color/black"
         />

    <Button
        android:id="@+id/reserve"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:layout_below="@+id/cost"
        android:onClick="reserve"
            android:layout_marginStart="230dp"
            android:layout_marginLeft="270dp"
            android:layout_marginTop="10dp"
            android:text="E7gzly"
            android:textSize="15dp" />

------------------- Owner.Class java ------------------------ ----------------

  @IgnoreExtraProperties
    public class Owner extends ArrayList <Owner> {

        public String name;
        public String mobile;
        public String fieldname;
        public String address;
        public String cost;
        public String zone1;
        public String zone2;
        private Class thisClass;


        // Default constructor required for calls to
        // DataSnapshot.getValue(User.class)
        public Owner(){}


        public Owner(String name, String mobile,String fieldname,String address, String cost, String zone1, String zone2) {
            this.name = name;
            this.mobile = mobile;
            this.fieldname = fieldname;
            this.address = address;
            this.cost = cost;
            this.zone1 = zone1;
            this.zone2 = zone2;

        }


        public String getMobile() {
            return mobile;
        }

        public String getName() {
            return name;
        }

         public String getAddress() {
            return address;


         }

        public void setAddress(String address) {
            this.address = address;
        }

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

        public void setMobile(String mobile) {
            this.mobile = mobile;
        }

        public void setCost(String cost) {
            this.cost = cost;
        }

        public void setZone1(String zone1) {
            this.zone1 = zone1;
        }

        public void setZone2(String zone2) {
            this.zone2 = zone2;
        }


        public String getFieldname() {
            return fieldname;
            }

        public void setFieldname(String fieldname) {
            this.fieldname = fieldname;
        }

    public String getCost() {
            return cost;
            }

    public String getZone1() {
            return zone1;
            }

    public String getZone2() {
            return zone2;
            }

    }

1 个答案:

答案 0 :(得分:0)

您正在此行将适配器初始化为空

adapter= new customAdapter(prospectowner_listview.this, ownerlist);

从Firebase数据库获取数据后,您无需将其更改通知它,要对其进行修复,请添加此行

mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot data : dataSnapshot.getChildren()) {
                    owner = data.getValue(Owner.class);
                    ownerlist.add(owner);

                }

                adapter.notifyDataSetChanged();

              }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });
    }

或在获取所有数据后仅设置适配器

 mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    for (DataSnapshot data : dataSnapshot.getChildren()) {
                        owner = data.getValue(Owner.class);
                        ownerlist.add(owner);

                    }

         adapter = new customAdapter(prospectowner_listview.this, ownerlist);

               }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });
        }