我尝试在Android应用中使用Firebase数据库,但是当我运行应用时,我的输出是空白的。我不知道如何在我的应用中查看所有Firebase数据。
ProductionDetail.java
public class ProductionDetail extends AppCompatActivity {
RecyclerView mRecyclerview;
Myadapater adapter;
List<Datastore> listData;
FirebaseDatabase FDB;
DatabaseReference DBR;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_production_detail);
mRecyclerview = (RecyclerView)findViewById(R.id.recycle_view);
mRecyclerview.setHasFixedSize(true);
RecyclerView.LayoutManager LM = new
LinearLayoutManager(getApplicationContext());
mRecyclerview.setLayoutManager(LM);
mRecyclerview.setItemAnimator(new DefaultItemAnimator());
mRecyclerview.addItemDecoration(new
DividerItemDecoration(getApplicationContext(),
LinearLayoutManager.VERTICAL));
listData = new ArrayList<>();
adapter = new Myadapater(listData);
FDB = FirebaseDatabase.getInstance();
GetDataFirebase();
}
void GetDataFirebase(){
DBR = FDB.getReference("Sreeauto");
DBR.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Datastore data = new Datastore();
data = dataSnapshot.getValue(Datastore.class);
listData.add(data);
mRecyclerview.setAdapter(adapter);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public class Myadapater extends
RecyclerView.Adapter<Myadapater.MyViewHolder>{
List<Datastore> listArray;
public Myadapater(List<Datastore> List){
this.listArray = List;
}
@Override
public Myadapater.MyViewHolder onCreateViewHolder( ViewGroup parent, int
viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent,
false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder( Myadapater.MyViewHolder holder, int
position) {
Datastore datastore = listArray.get(position);
holder.txt_date.setText(datastore.getDate());
holder.txt_shift.setText(datastore.getShift());
holder.txt_incharge.setText(datastore.getIncharge());
holder.txt_qty.setText(datastore.getQty());
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txt_date;
TextView txt_shift;
TextView txt_incharge;
TextView txt_qty;
public MyViewHolder(View itemView) {
super(itemView);
TextView txtdate =
(TextView)itemView.findViewById(R.id.txt_date);
TextView txtshift =
(TextView)itemView.findViewById(R.id.txt_shift);
TextView txtincharge =
(TextView)itemView.findViewById(R.id.txt_incharge);
TextView txtqty = (TextView)itemView.findViewById(R.id.txt_qty);
}
}
@Override
public int getItemCount() {
return listArray.size();
}
}
}
activity_production_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context="com.udayaj.sreeauto.ProductionDetail">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycle_view"
/>
</LinearLayout>
row.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:id="@+id/linear1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:text="Date"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/txt_date" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="43dp"
android:text="Shift"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/txt_shift" />
<TextView
android:id="@+id/txt_incharge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="38dp"
android:text="Incharge"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_qty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Qty"
android:layout_marginHorizontal="45dp"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
Adapater.java
package com.udayaj.sreeauto;
public class Adapater extends RecyclerView.Adapter<Adapater.ImageViewHolder>
{
private Context mContext;
private List<Datastore> mUpload;
public Adapater(Context context, List<Datastore> uploads) {
mContext = context;
mUpload = uploads;
}
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.row, parent,
false);
return new ImageViewHolder(v);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Datastore uploadCurrent = mUpload.get(position);
holder.txt_date.setText(uploadCurrent.getDate());
holder.txt_shift.setText(uploadCurrent.getShift());
holder.txt_incharge.setText(uploadCurrent.getIncharge());
holder.txt_qty.setText(uploadCurrent.getQty());
}
@Override
public int getItemCount() {
return mUpload.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
TextView txt_date;
TextView txt_shift;
TextView txt_incharge;
TextView txt_qty;
public ImageViewHolder(View itemView) {
super(itemView);
txt_date = itemView.findViewById(R.id.txt_date);
txt_shift = itemView.findViewById(R.id.txt_shift);
txt_incharge = itemView.findViewById(R.id.txt_incharge);
txt_qty = itemView.findViewById(R.id.txt_qty);
}
}
}
Datastore.java
package com.udayaj.sreeauto;
public class Datastore {
private String Date;
private String Shift;
private String Qty;
private String Incharge;
public Datastore(){
}
public Datastore(String date,String shift, String qty, String incharge){
Date = date;
Shift = shift;
Qty = qty;
Incharge = incharge;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getShift() {
return Shift;
}
public void setShift(String shift) {
Shift = shift;
}
public String getQty() {
return Qty;
}
public void setQty(String qty) {
Qty = qty;
}
public String getIncharge() {
return Incharge;
}
public void setIncharge(String incharge) {
Incharge = incharge;
}
}