本文介绍了在Android中使用Firebase数据填充ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我又回到了这个问题,但是我很接近:我有以下代码,试图在列表视图中显示Firebase值.
I am back with this question again, I am close though: I have the following code which I am trying to show the firebase values in a list view.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mystatement);
listview = (ListView) findViewById(R.id.listview);
textView4 = (TextView) findViewById(R.id.textView4);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
listview.setAdapter(adapter);
dref = FirebaseDatabase.getInstance().getReference();
DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
dref= dref.child("Expenditure");
dref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
DogExpenditure dogExpenditure = postSnapshot.getValue(DogExpenditure.class);
//Adding it to a string
String expenses = "Amount: "+dogExpenditure.getAmount()+"\nReason for Use: "+dogExpenditure.getItem()+"\n\n";
String amount = dogExpenditure.getAmount();
String item = dogExpenditure.getItem();
System.out.println(expenses);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
当我运行此代码时,在我的android studio控制台中,我看到以下输出.这正是我想要的.但是,它没有显示在应用程序的布局视图中,而是显示了空白屏幕.
When I run this code, in my android studio console, I see the below output. Which is exactly what I want. But, it is not displaying in my layout view from the app, it shows a blank screen.
**03-07 15:15:05.777 32332-32332/com.example.moses.farm I/System.out: Amount: dd
03-07 15:15:05.779 32332-32332/com.example.moses.farm I/System.out: Reason for Use: dddd
03-07 15:15:05.782 32332-32332/com.example.moses.farm I/System.out: Amount: kwaraaaa
03-07 15:15:05.782 32332-32332/com.example.moses.farm I/System.out: Reason for Use: ayeeeee**
有人可以告诉我如何在我的应用程序中显示该输出,因为当前它未显示任何内容,但是在调试控制台中我可以看到它
推荐答案
//Just above onCreate
List<String> expenseList = new ArrayList<>();
ArrayAdapter<String> adapter;
//In value event listener
expenseList.add(expense)
if(expensList.size() == 1)
{
adapter = new ArrayAdapter<>(getApplicationContext(),android.R.layout.simple_list_item_1,expensList);
listView.setAdapter(adapter)
}
else if(expenseList.size() > 1)
{
adapter.notifyDatasetChanged();
}
这篇关于在Android中使用Firebase数据填充ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!