问题描述
在我的android应用程序中,我创建了一个包含 ListView
的活动,该活动由来自Firebase数据库的数据填充。 $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
companyX:{
address:50th avenue,NY,
name:备用工具有限公司
,
companyZ:{
address:50th Broadway,NY,
name:Burgers and Burgers
}
$ bcompany-request:{
companyX:{
req1:true
req2:true
}
请求:{
req1:{
destination:Upper Tooting 122,Bronx,
origin:Philadelphia,
time:1473593287,
...
}
req2:{
...
}
}
我想填充 ListView
来自 requests
节点的请求列表。但我首先需要知道属于某个特定公司的所有请求,因此我首先访问 company-requests
节点并检索属于特定公司的所有请求密钥。
我遇到的问题是在数据库最终数据到达之前创建 ListView
:
public class RequestsListActivity extends AppCompatActivity {
private ListView rListView;
DatabaseReference rootNode = FirebaseDatabase.getInstance()。getReference();
@Override
protected void onCreate(Bundle savedInstanceState){
...
rViewView =(ListView)findViewById(R.id.result_list_view);
//首先我检索一个特定公司的所有请求
DatabaseReference companyRequests = rootNode.child(company-requests / companyX);
companyRequests.addValueEventListener(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
//然后我检索这些请求的所有键
...
while(iterator.hasNext()){
String key = iterator.next()。getKey();
//对于每个键我检索它的细节从请求节点
DatabaseReference currRequest = rootNode.child(requests /+ key);
currRequest.addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange DataSnapshot dataSnapshot){
String time;
time =(String)dataSnapshot.child(time)。getValue();
Request request = new Request(time);
allRequests.add(请求);
}
... onCancelled ...
});
此代码执行早:在我们从FIREBASE获得任何数据
RequestAdapter adapter = new RequestAdapter(RequestsListActivity.this,allRequests);
rViewView.setAdapter(adapter);
}
... onCancelled ...
});
$ b $ p $如何插入一个等待
(spinner?)等待值从Firebase加载?解决方案您可以使用一个简单的计数器来跟踪待处理的装载数量:
$ p $ companyRequests.addValueEventListener(new ValueEventListener(){
public void onDataChange(DataSnapshot dataSnapshot){
//在开始时我们需要加载所有子元素
final long [] pendingLoadCount = {dataSnapshot.getChildrenCount()};
for(DataSnapshot childSnapshot :dataSnapshot.getChildren()){
//对于每个键我从请求节点检索它的细节
DatabaseReference currRequest = rootNode.child(requests /+ childSnapshot.getKey());
currRequest.addListenerForSingleValueEvent(new ValueEventListener(){
public void onDataChange(DataSnapshot dataSnapshot){
字符串时间;
time =(String)dataSnapshot.child(time)。getValue();
请求请求=新请求(时间);
allRequests.add(request);
//我们加载了一个子项,检查是否完成
pendingLoadCount [0] = pendingLoadCount [0] - 1;
if(pendingLoadCount [0] == 0){
RequestAdapter adapter = new RequestAdapter(RequestsListActivity.this,allRequests);
rViewView.setAdapter(adapter);
}
}
... onCancelled ...
});
}
}
});
in my android application I create an activity which contains a ListView
which is populated with data from Firebase Database.
The JSON Tree of the structure of the database is the following:
{
"companies" : {
"companyX" : {
"address" : "50th avenue, NY",
"name" : "Spare-Tools Ltd."
},
"companyZ" : {
"address" : "50th Broadway, NY",
"name" : "Burgers and Burgers"
}
},
"company-requests" : {
"companyX" : {
"req1" : true
"req2" : true
}
},
"requests" : {
"req1" : {
"destination" : "Upper Tooting 122, Bronx",
"origin" : "Philadelphia",
"time" : "1473593287",
...
}
"req2" : {
...
}
}
}
I want to populate the ListView
with the list of requests from the requests
node. But I first need to know all requests that belong to a specific company so I first go to the company-requests
node and retrieve all the request-keys belonging to the specific company.The problem I am facing is that the ListView
is created before the final data from the database arrived:
public class RequestsListActivity extends AppCompatActivity {
private ListView rListView;
DatabaseReference rootNode = FirebaseDatabase.getInstance().getReference();
@Override
protected void onCreate(Bundle savedInstanceState) {
...
rListView = (ListView) findViewById(R.id.result_list_view);
//First I retrieve all the requests of a specific company
DatabaseReference companyRequests = rootNode.child("company-requests/companyX");
companyRequests.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Then I retrieve all the keys of these requests
...
while (iterator.hasNext()) {
String key = iterator.next().getKey();
//For each key I retrieve its details from the requests node
DatabaseReference currRequest = rootNode.child("requests/" + key);
currRequest.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String time;
time = (String) dataSnapshot.child("time").getValue();
Request request = new Request(time);
allRequests.add(request);
}
...onCancelled...
});
}
//THIS CODE IS EXECUTED TO EARLY: BEFORE WE HAVE ANY DATA FROM FIREBASE
RequestAdapter adapter = new RequestAdapter(RequestsListActivity.this, allRequests);
rListView.setAdapter(adapter);
}
...onCancelled...
});
}
}
How can I insert a wait
(spinner?) that waits until the values are loaded from Firebase?
You can use a simple counter to keep track of the number of pending loads:
companyRequests.addValueEventListener(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
// at the start we need to still load all children
final long[] pendingLoadCount = { dataSnapshot.getChildrenCount() };
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
//For each key I retrieve its details from the requests node
DatabaseReference currRequest = rootNode.child("requests/" + childSnapshot.getKey());
currRequest.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
String time;
time = (String) dataSnapshot.child("time").getValue();
Request request = new Request(time);
allRequests.add(request);
// we loaded a child, check if we're done
pendingLoadCount[0] = pendingLoadCount[0] - 1;
if (pendingLoadCount[0] == 0) {
RequestAdapter adapter = new RequestAdapter(RequestsListActivity.this, allRequests);
rListView.setAdapter(adapter);
}
}
...onCancelled...
});
}
}
});
这篇关于Android Firebase等待数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!