我的firebaserecyclerView没有显示在应用程序中,但数据已添加到firebase结构中
这是我的Firebase结构
我需要在recyclerview中显示费用名称和总计
主要活动
public class DayExpensesActivity extends AppCompatActivity {
private RecyclerView recyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
FirebaseRecyclerAdapter<Expenses,ExpenseViewHolder> firebaseRecyclerAdapter;
RecyclerView.LayoutManager layoutManager;
Query query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_expenses);
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
firebaseDatabase=FirebaseDatabase.getInstance();
databaseReference=firebaseDatabase.getReference();
final Expenses expensesDate = new Expenses(username, monthyr, currentdate);
query=databaseReference.child("users").child(expensesDate.getUsername()).child(expensesDate.getMonth()).child(expensesDate.getDate());
FirebaseRecyclerOptions<Expenses> options =
new FirebaseRecyclerOptions.Builder<Expenses>()
.setQuery(query, Expenses.class)
.build();
firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<Expenses, ExpenseViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull ExpenseViewHolder holder, int position, @NonNull Expenses model) {
holder.expensesText.setText(model.getExpenseName());
holder.totaltext.setText(model.getTotal());
@NonNull
@Override
public ExpenseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.expenses_card_layout,parent,false);
return new ExpenseViewHolder(view);
}
};
firebaseRecyclerAdapter.notifyDataSetChanged();
recyclerView.setAdapter(firebaseRecyclerAdapter);
ExpensesViewHolder
public class ExpenseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView expensesText,totaltext;
public ImageButton addBtn,editBtn;
private ItemClickListener itemClickListener;
public void setItemClickListener(ItemClickListener itemClickListener){
this.itemClickListener=itemClickListener;
}
public ExpenseViewHolder(View itemView) {
super(itemView);
expensesText=(TextView) itemView.findViewById(R.id.expensestext);
totaltext=(TextView) itemView.findViewById(R.id.totaltxt);
addBtn=(ImageButton)itemView.findViewById(R.id.add_btn);
editBtn=(ImageButton)itemView.findViewById(R.id.btn_edt);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
}
答案 0 :(得分:0)
要简单地显示这两个值,请使用以下代码:
DatabaseReference ref = databaseReference
.child("users")
.child(expensesDate.getUsername())
.child("Expenses")
.child(expensesDate.getMonth())
.child(expensesDate.getDate());
现在,您可以添加一个侦听器来读取这些值:
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String expenseName = dataSnapshot.child("expenseName").getValue(String.class);
String total = dataSnapshot.child("total").getValue(String.class);
Log.d("TAG", expenseName + " / " + total);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage());
}
};
ref.addListenerForSingleValueEvent(valueEventListener);
您的logcat中的输出将是:
pegrol / 76
由于使用RecyclerView来回收视图,因此无需在RecyclerView
中显示这些值,并且在您的情况下,也无需回收单个视图。