如何从数据库中检索我的RecyclerView数据,我的RecyclerView显示为空

时间:2017-01-15 11:16:22

标签: android firebase android-recyclerview

My FirebaseHow my RecyclerView display after my code

这是我保存数据的方式

saveBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            firebaseAuth = FirebaseAuth.getInstance();
            mCurrentUser = firebaseAuth.getCurrentUser();
            DatabaseReference db = FirebaseDatabase.getInstance().getReference().child("Email").child(mCurrentUser.getUid()).child("Appointment").push();
            String name = nameTxt.getText().toString().trim();
            String address = addressTxt.getText().toString().trim();
            String tel = telTxt.getText().toString().trim();
            String date = dateApp.getText().toString().trim();
            String time = timeApp.getText().toString().trim();

            //Adding values
            db.child("Name").setValue(name);
            db.child("Address").setValue(address);
            db.child("Tel").setValue(tel);
            db.child("Date").setValue(date);
            db.child("Time").setValue(time);

            //Storing values to Firebase
            Toast.makeText(AddAppointment.this,"Saved",Toast.LENGTH_SHORT).show();
            Intent mainIntent = new Intent(AddAppointment.this,Appointment.class);
            startActivity(mainIntent);
        }
    });

我的代码将数据检索到RecyclerView“但没有成功”

public class ViewAppointment extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private FirebaseUser mCurrentUser;
    private DatabaseReference db;
    private RecyclerView recyclerView;
    private FirebaseListAdapter<AppointmentData> mAdapter;

    ImageButton refreshBtn;

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

        recyclerView = (RecyclerView)findViewById(R.id.recycle);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        firebaseAuth = FirebaseAuth.getInstance();

        mCurrentUser = firebaseAuth.getCurrentUser();
        db = FirebaseDatabase.getInstance().getReference().child("Email").child(mCurrentUser.getUid()).child("Appointment");
        db.keepSynced(true);
        addListenerOnButton();
        FirebaseRecyclerAdapter<AppointmentData, myViewHolder> adapter = new FirebaseRecyclerAdapter<AppointmentData, myViewHolder>
            (AppointmentData.class,R.layout.model,myViewHolder.class,db) {

            @Override
            protected void populateViewHolder(myViewHolder viewHolder, AppointmentData model, int position) {
                viewHolder.nameTxt.setText(model.getName());
                viewHolder.dateApp.setText(model.getDate());
                viewHolder.timeApp.setText(model.getTime());
            }
        };
        recyclerView.setAdapter(adapter);
    }

    public static class myViewHolder extends RecyclerView.ViewHolder {
        private TextView nameTxt,dateApp,timeApp;

        public myViewHolder(View itemView){
            super(itemView);
            nameTxt = (TextView)itemView.findViewById(R.id.nameTxt);
            dateApp = (TextView) itemView.findViewById(R.id.dateAppointment);
            timeApp = (TextView)itemView.findViewById(R.id.timeAppointment);
        }
    }
}

AppointmentData.java

public class AppointmentData {

    private String name, tel, address, time, date;

    public AppointmentData() {

    }

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

    public void setTel(String tel) {
        this.tel = tel;
    }

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

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

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

    public AppointmentData(String name, String tel, String address, String time, String date) {
        this.name = name;
        this.tel = tel;
        this.address = address;
        this.time = time;
        this.date = date;
    }

    public String getName() {
        return name;
    }

    public String getTel() {
        return tel;
    }

    public String getAddress() {
        return address;
    }

    public String getDate() {
        return date;
    }

    public String getTime() {
        return time;
    }
}

Model.xml RecyclerView布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/colorControlHighlight"
    android:orientation="vertical">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Name"
        android:id="@+id/nameTxt"
        android:padding="10dp"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:layout_alignParentLeft="true"
        android:background="@android:color/holo_blue_light" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/cast_intro_overlay_button_background_color">

        <TextView
            android:text="Date"
            android:layout_width="185dp"
            android:layout_height="wrap_content"
            android:id="@+id/dateAppointment" />

        <TextView
            android:text="Time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/timeAppointment"
            android:layout_weight="1" />
    </LinearLayout>

</LinearLayout>

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

ViewAppointment.xml

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycle"
    android:layout_width="match_parent"
    android:layout_height="380dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="21dp">

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

2 个答案:

答案 0 :(得分:0)

我认为您的问题出在AppointmentData课程中。您已将所有成员变量命名为小写但与数据库中的键名称不完全匹配(因为每个键名的第一个字母都是大写)。因此,请确保AppointmentData类的键名和成员变量匹配。

答案 1 :(得分:0)

我对你的问题做了一点观察,这似乎只是一个小错误。我指的是这篇文章:

https://stackoverflow.com/a/34883389/4088357

  

我们将创建一个代表Blog Post的Java类。与上次一样,我们必须确保我们的字段名称与Firebase数据库中的属性名称匹配,并为该类提供默认的无参数构造函数。

因此,您可以在Firebase数据库中保存值时更改代码:

//Adding values
db.child("name").setValue(name);
db.child("address").setValue(address);
db.child("tel").setValue(tel);
db.child("date").setValue(date);
db.child("time").setValue(time);