我已按照下面的教程进行操作,但它只能检索一个空间节点数据,如何检索所有节点数据?照片下面是我的数据库。
group_by
答案 0 :(得分:0)
FirebaseListAdapter
可以显示商品的列表。您的数据模型不是列表,而是树。因此无法使用单个FirebaseListAdapter
显示。
答案 1 :(得分:0)
我已为Adapter
创建了ListView
类,并为ListView
中的项目显示列表创建了 row_item.xml 。
<强> FireListAdapter.java 强>
public class FireListAdapter extends BaseAdapter {
private List<User> modelList = new ArrayList<User>();
private Context context;
LayoutInflater inflater;
public FireListAdapter(Context context, List<User> modelList) {
this.context = context;
this.modelList = modelList;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return modelList.size();
}
@Override
public Object getItem(int position) {
return modelList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
view = inflater.inflate(R.layout.row_item, null);
TextView textName = (TextView) view.findViewById(R.id.textName);
TextView textEmail = (TextView) view.findViewById(R.id.textEmail);
textName.setText(modelList.get(position).getName());
textEmail.setText(modelList.get(position).getEmail());
return view;
}
}
为FirebaseDatabase
中的ListView
的显示数据创建 Welcome.java 。
public class Welcome extends AppCompatActivity implements View.OnClickListener {
private ListView listView;
private List<User> myModelList = new ArrayList<User>();
private DatabaseReference mFirebaseDatabase;
private FirebaseDatabase mFirebaseInstance;
private String TAG = Welcome.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
setUI();
mFirebaseInstance = FirebaseDatabase.getInstance();
// get reference to 'signUp' node Here signUp is table in FirebaseDatabase
mFirebaseDatabase = mFirebaseInstance.getReference("signUp");
getData();
}
public void setUI() {
textProfile = (TextView) findViewById(R.id.textWelcome);
listView = (ListView) findViewById(R.id.listItem);
}
//This method is getting data from FirebaseDatabase
public void getData() {
mFirebaseDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> snapshotIterable = dataSnapshot.getChildren();
Iterator<DataSnapshot> iterator = snapshotIterable.iterator();
while (iterator.hasNext()) {
DataSnapshot dataSnapshot1 = iterator.next();
User user = dataSnapshot1.getValue(User.class);
user.setName(user.getName());
user.setEmail(user.getEmail());
myModelList.add(user);
FireListAdapter fireListAdapter = new FireListAdapter(Welcome.this, myModelList);
fireListAdapter.notifyDataSetChanged();
listView.setAdapter(fireListAdapter);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
希望这会对你有所帮助:)。