嘿,我试图从fire base实时数据库中检索数据,但是当我打开活动时,ListView没有显示任何内容。
我做了我在YouTube和FireBase官方网站上缝的所有东西,但不起作用 谢谢帮手
主要代码:
public class HomePage extends AppCompatActivity {
TextView tvBalance ;
ListView lvExpense ;
ArrayList <Expense> arrayList ;
expenseAdapter expenseAdapter ;
private DatabaseReference database ;
FirebaseAuth firebaseAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
firebaseAuth = FirebaseAuth.getInstance();
// Find view by id
tvBalance = (TextView) findViewById(R.id.tvBalance) ;
lvExpense = (ListView) findViewById(R.id.lvExpense) ;
// Reference
retriveData() ;
}
public void retriveData ()
{
database = FirebaseDatabase.getInstance().getReference("Expense") ;
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
arrayList = new ArrayList <Expense> ();
for( DataSnapshot data : dataSnapshot.getChildren() )
{
Expense p = data.getValue(Expense.class);
arrayList.add(p);
}
expenseAdapter = new expenseAdapter(HomePage.this, 0,0 ,arrayList) ;
lvExpense.setAdapter(expenseAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(HomePage.this, databaseError.getMessage() ,Toast.LENGTH_SHORT).show();
}
}) ;
}
}
适配器代码
public class expenseAdapter extends ArrayAdapter <Expense> {
Context context;
List <Expense> objects;
public expenseAdapter(Context context, int resource, int textViewResourceId, List<Expense> objects) {
super(context, resource, textViewResourceId, objects);
this.context=context;
this.objects=objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = ((Activity)context).getLayoutInflater();
View view = layoutInflater.inflate(R.layout.expense_listview, parent, true);
TextView tvTitle = (TextView)view.findViewById(R.id.tvTitle);
TextView tvCategory = (TextView)view.findViewById(R.id.tvCategory);
TextView tvDate = (TextView)view.findViewById(R.id.tvDate);
TextView tvPrice = (TextView)view.findViewById(R.id.tvPrice);
Expense temp = objects.get(position);
tvTitle.setText(temp.getTitle());
tvCategory.setText(temp.getCategory());
tvDate.setText(temp.getDay() + "/" + temp.getMonth() + "/" + temp.getYear());
tvPrice.setText(Double.toString(temp.getMoney()));
return view ;
}
}
当我运行该应用程序时,所有内容都会出现,但它只是忽略了检索数据的功能。 没有错误或类似的东西,但看起来它根本没有作用