我再次回到了这个问题,但是我很接近:我有以下代码,我试图在列表视图中显示firebase值。
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()+"nn";
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控制台运行此代码时,我会看到以下输出。这正是我想要的。但是,它没有从应用程序中显示在我的布局视图中,它显示一个空白屏幕。
**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**
有人可以向我展示如何在我的应用中显示该输出,因为目前没有显示任何内容,而是在调试控制台中显示
您可以使用firebaseui。
使用Firebase数据填充ListView非常简单。
然后添加依赖项:
- 连接到Firebase
- 创建自定义firebaselistadapter子类
类似:
listview = (ListView) findViewById(R.id.listview);
DatabaseReference dref = FirebaseDatabase.getInstance().getReference();
mAdapter = new FirebaseListAdapter<DogExpenditure>(this, DogExpenditure.class, R.layout.yourLayout, ref) {
@Override
protected void populateView(View view, DogExpenditure myObj, int position) {
//populate your view
String amount = dogExpenditure.getAmount()
//Put the value in your layout.
}
};
listview.setAdapter(mAdapter);
更多信息。
//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();
}
使DREF的单身对象
和列表尚未进行。
您应该将费用添加到列表中,并使用
通知更改adapter.notifydatasetchanged()。
list.add(费用)
adapter.notifydatasetchanged()
首先创建一个适配器,因为您已经拥有模型类。myadapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context context;
private List<DogExpenditure> uploads;
public MyAdapter(Context context, List<DogExpenditure> uploads) {
this.uploads = uploads;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_firebase_images, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
DogExpenditure upload = uploads.get(position);
holder.textViewName.setText(upload.getAmount());
holder.textView1.setText(upload.getYourData());
}
@Override
public int getItemCount() {
return uploads.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textview1 = (TextView) itemView.findViewById(R.id.your_id);
}
}
}
现在您的活动
private List<DogExpenditure> uploads;
MyAdapter adapter;
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();
uploads = new ArrayList<>();
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);
uploads.add(dogExpenditure);
}
adapter = new MyAdapter(getApplicationContext(), uploads);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});