2017-01-15 33 views
0

My FirebaseHow my RecyclerView display after my code如何检索数据从数据库我RecyclerView,我RecyclerView显示空

这是我如何保存我的数据

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> 
+1

您是否尝试过使用数据的静态列表,以验证您的RecyclerView正常工作,甚至触摸数据库之前? –

+0

不〜我只是按照教程和修改我的代码...但我不知道哪里错误 –

+0

您的图像显示两张卡片已加载,所以问题存在于您的布局或如何显示文本 –

回答

1

我觉得你的问题是AppointmentData类。你已经用小写命名了所有你的成员变量,它与你的数据库中的键名不完全匹配(因为每个键名的第一个字母都是大写的)。所以请确保您的AppointmentData类的键名和成员变量匹配。

0

我对你的问题做了一些观察,看起来只是一个小小的错误。我指的这个帖子:

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

我们将创建一个表示博客帖子的Java类。像上次一样,我们必须确保我们的字段名称与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); 
+0

中制作一个静态的数组对象列表,谢谢你的帮助。它工作。我很粗心 –

+0

我有一个问题。如果我想从Firebase中删除RecyclerView和数据之一,我该如何编写代码? –

+0

不客气@YewGuanKoh。请为此问题创建新问题。 –

相关问题