在与密钥匹配的recyclerView中填充数据

时间:2018-03-16 06:07:31

标签: java android firebase firebase-realtime-database

我有这个活动DataSaveActivity,它是从qrScan活动中调用的。在qrScan扫描QR代码并将字符串值(由推送数据的firebase生成的唯一密钥)返回到DataSaveActivity之后,将调用DataSaveActivity。我需要做的是在回收器视图中显示与keyValue匹配的数据。 这是我的DataSaveActivity

public class DataSaveActivity extends AppCompatActivity {

String keyValue;
private RecyclerView mReceipt;
private DatabaseReference mRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data_save);
    mReceipt = new RecyclerView(this);
    Intent intent = getIntent();
    if(intent == null) {
        keyValue = null;
    } else {
        keyValue = intent.getStringExtra("key");
    }
// HERE ITS DISPLAYING ALL THE DATA INSIDE BILLS 
    mRef = FirebaseDatabase.getInstance().getReference().child("Bills");
//.child(keyValue); WHEN I AM ADDING THIS AFTER .child("Bills")IT CRASHES.
    mReceipt = (RecyclerView)findViewById(R.id.othersRecycler);
    mReceipt.setLayoutManager(new LinearLayoutManager(this));

    FirebaseRecyclerAdapter<DataModel, ReceiptViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DataModel, ReceiptViewHolder>
            (DataModel.class, R.layout.reciept_row, ReceiptViewHolder.class, mRef){
        @Override
        protected void populateViewHolder(ReceiptViewHolder viewHolder,DataModel model, int position) {
            viewHolder.setBillNo("Bill No : " + model.getBillNo());
            viewHolder.setStoreName("Store Name : " + model.getCompany());
            viewHolder.setDate("Date : " + model.getDate());
            viewHolder.setTime("Time : " + model.getTime());
            viewHolder.setBillTotal("Total : " + model.getTotal());

        }
    };
    mReceipt.setAdapter(firebaseRecyclerAdapter);
}
public static class ReceiptViewHolder extends RecyclerView.ViewHolder {
    View mView;
    public ReceiptViewHolder(View itemView){
        super(itemView);
        mView = itemView;
    }
    public void setBillNo(String billNo){
        TextView BillNo = (TextView)mView.findViewById(R.id.receiptBillNo);
        BillNo.setText(billNo);
    }
    public void setStoreName(String company){
        TextView StoreName = (TextView)mView.findViewById(R.id.receiptStoreName);
        StoreName.setText(company);
    }
    public void setDate(String date){
        TextView Date = (TextView)mView.findViewById(R.id.receiptDate);
        Date.setText(date);
    }
    public void setTime(String time){
        TextView Time = (TextView)mView.findViewById(R.id.receiptTime);
        Time.setText(time);
    }
    public void setPhone(String phone){
        TextView Phone = (TextView)mView.findViewById(R.id.receiptPhone);
        Phone.setText(phone);
    }
    public void setBillTotal(String total){
        TextView BillTotal = (TextView)mView.findViewById(R.id.receiptTotal);
        BillTotal.setText(total);
    }

}
}

我的Firebase数据库的结构如下:

structure firebase

我想显示从qrScan活动发送的密钥(keyValue)的数据。

1 个答案:

答案 0 :(得分:0)

首先将firebaseListAdapter变量设为全局。

private FirebaseListAdapter<Users> firebaseListAdapter;

然后只需删除适配器初始化的FirebaseListAdapter<Users>

因此,为了解决这个问题,您需要覆盖onStart()方法并添加以下代码行:

@Override
protected void onStart() {
    super.onStart();
    firebaseListAdapter.startListening();
}