我正在尝试从ValueEventListener
检索数据,并将此数据添加到对话框中并显示给用户,但它仅返回最后一个孩子,因此我为单击的项目的键做了祝酒并返回正确的密钥
我正在尝试检索该节点
MainActivity
public class PaymentFragment extends Fragment {
private RecyclerView recyclerView;
private Toolbar toolbar;
private ProgressBar progressBar;
private DatabaseReference payoutRef;
private Lead leadModel;
private FirebaseRecyclerAdapter<Lead, PaymentViewHolder> adapter;
private Lead currentLead;
private String name;
private String contactPhone;
private String amount;
private String info;
private String purpose;
private String status;
private long actionDate;
private String paymentSanctioned;
private String payoutAmount;
private String current_user;
private String paidStatus;
private FirebaseAuth auth;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.payment_activity, container, false);
Toolbar toolbar = view.findViewById(R.id.payment_toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
auth = FirebaseAuth.getInstance();
current_user = auth.getCurrentUser().getUid();
payoutRef = FirebaseDatabase.getInstance().getReference().child("Payout");
recyclerView = view.findViewById(R.id.payment_recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
progressBar = view.findViewById(R.id.payment_progress);
return view;
}
@Override
public void onStart() {
super.onStart();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
progressBar.setVisibility(View.GONE);
recyclerView.setVisibility(View.VISIBLE);
}
}, 1000);
adapter = new FirebaseRecyclerAdapter<Lead, PaymentViewHolder>(
Lead.class,
R.layout.payment_item,
PaymentViewHolder.class,
payoutRef.orderByChild("userId").equalTo(current_user)
) {
@Override
protected void populateViewHolder(PaymentViewHolder viewHolder, Lead model, int position) {
final String key = adapter.getRef(position).getKey();
viewHolder.paymentText.setText(model.getName());
payoutRef.child(key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentLead = dataSnapshot.getValue(Lead.class);
name = currentLead.getName();
contactPhone = currentLead.getContactPhone();
amount = currentLead.getAmount();
info = currentLead.getInfo();
purpose = currentLead.getPurpose();
status = currentLead.getStatus();
actionDate = currentLead.getActionDate();
paymentSanctioned = currentLead.getSanctionedPayment();
paidStatus = currentLead.getPaid();
payoutAmount = currentLead.getPayoutAmount();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
viewHolder.paymentCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(), key, Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), payoutAmount, Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Lead Information");
builder.setMessage(
"Name: " + name + "\n\n" +
"Contact Phone: " + contactPhone + "\n\n" +
"Amount: " + amount + "\n\n" +
"Info: " + info + "\n\n" +
"Purpose: " + purpose + "\n\n" +
"Status: " + status + "\n\n" +
"Action Date: " + getDate(actionDate) + "\n\n" +
"Payment Sanctioned: " + paymentSanctioned + "\n\n" +
"Payout Amount: " + payoutAmount + "\n\n" +
"Paid Status: " + paidStatus + "\n\n"
);
builder.setCancelable(true);
builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
};
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
private String getDate(long actionDate) {
SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
return sfd.format(new Date(actionDate));
}
ValueEventListener
@Override
protected void populateViewHolder(PaymentViewHolder viewHolder, Lead model, int position) {
final String key = adapter.getRef(position).getKey();
viewHolder.paymentText.setText(model.getName());
payoutRef.child(key).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
currentLead = dataSnapshot.getValue(Lead.class);
name = currentLead.getName();
contactPhone = currentLead.getContactPhone();
amount = currentLead.getAmount();
info = currentLead.getInfo();
purpose = currentLead.getPurpose();
status = currentLead.getStatus();
actionDate = currentLead.getActionDate();
paymentSanctioned = currentLead.getSanctionedPayment();
paidStatus = currentLead.getPaid();
payoutAmount = currentLead.getPayoutAmount();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
模型类
public class Lead {
private String name;
private String contactPhone;
private String amount;
private String info;
private String purpose;
private String status;
private long actionDate;
private String nextActionDate;
private String sanctionedPayment;
private String paid;
private String userId;
private String payoutAmount;
private String remarks;
public Lead() {
}
public Lead(String name, String contactPhone, String amount, String info, String purpose, String status, long actionDate, String nextActionDate, String sanctionedPayment, String paid, String userId, String payoutAmount, String remarks) {
this.name = name;
this.contactPhone = contactPhone;
this.amount = amount;
this.info = info;
this.purpose = purpose;
this.status = status;
this.actionDate = actionDate;
this.nextActionDate = nextActionDate;
this.sanctionedPayment = sanctionedPayment;
this.paid = paid;
this.userId = userId;
this.payoutAmount = payoutAmount;
this.remarks = remarks;
}
public String getPayoutAmount() {
return payoutAmount;
}
public void setPayoutAmount(String payoutAmount) {
this.payoutAmount = payoutAmount;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPaid() {
return paid;
}
public void setPaid(String paid) {
this.paid = paid;
}
public long getActionDate() {
return actionDate;
}
public void setActionDate(long actionDate) {
this.actionDate = actionDate;
}
public String getNextActionDate() {
return nextActionDate;
}
public void setNextActionDate(String nextActionDate) {
this.nextActionDate = nextActionDate;
}
public String getSanctionedPayment() {
return sanctionedPayment;
}
public void setSanctionedPayment(String sanctionedPayment) {
this.sanctionedPayment = sanctionedPayment;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactPhone() {
return contactPhone;
}
public void setContactPhone(String contactPhone) {
this.contactPhone = contactPhone;
}
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public String getPurpose() {
return purpose;
}
public void setPurpose(String purpose) {
this.purpose = purpose;
}
}
ViewHolder类
public class PaymentViewHolder extends RecyclerView.ViewHolder {
public TextView paymentText;
public CardView paymentCardView;
public PaymentViewHolder(@NonNull View itemView) {
super(itemView);
paymentText = itemView.findViewById(R.id.payment_item_name);
paymentCardView = itemView.findViewById(R.id.payment_item_cardView);
}
答案 0 :(得分:0)
只需将此代码放入您的onDataChange方法即可。
if (dataSnapshot != null) {
try {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Lead leadModel = postSnapshot.getValue(Lead.class);
if (leadModel!=null){
leadList.add(leadModel);
Log.e(" name is ", "="+leadModel.getName());
}
}
adapter = new MessageAdapter(getActivity().this, leadList);
recyclerView.setAdapter(adapter);
} catch (Exception e) {
}
}