我正在尝试从ListView
中的Firebase数据库读取数据。 ListView
中的每个项目都带有一个按钮,单击该按钮后,将在文本视图中设置的读取数据写入数据库。下面的代码是我用于显示ListView
和写入数据库的代码。This is my database
if (FirebaseAuth.getInstance().getCurrentUser() ==null{
Toast.makeText(getApplicationContext(),"logon first", Toast. Length.SHORT).show() ;
} else{
display() ;
}
private void display(){
Query query=FirebaseDatabase.getInstance().getReference().child("border_details");
FirebaseListOptions<DRive>options=new FirebaseListOptions.Builder<DRive>().setQuery(query,DRive.class).setLayout(R.layout.messages).build();
ListView list= findViewById(R.id.list_of_messages);
adapter=new FirebaseListAdapter<DRive>(options) {
@Override
protected void populateView(View v, DRive model, int position) {
Button d=(Button)findViewById(R.id.button_accept);
final TextView z=(TextView)findViewById(R.id.dropoff);
final TextView c=(TextView)findViewById(R.id.pickup);
final TextView f=(TextView)findViewById(R.id.timed);
final TextView m=(TextView) findViewById(R.id.points);
z.setText(model.getDropoffspot());
c.setText(model.getPickupspot());
f.setText(model.getPickuptime());
m.setText(model.getKey());
d.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DRive dRive=new DRive();
hec();
dRive.setDropoffspot(chi);
dRive.setKey(pt);
dRive.setPickupspot(g);
dRive.setPickuptime(boo);
dRive.setKey(pt);
myRef.child(pt).push().setValue(dRive);
Toast.makeText(getApplicationContext(),"shi",Toast.LENGTH_SHORT).show();
}
private void hec() {
pt=m.getText().toString();
boo=f.getText().toString();
chi=z.getText().toString();
g=c.getText().toString();
}
});
}
};
list.setAdapter(adapter);
这是我的json
{
"order_details" : {
"DEzUoNY6BqOVn1DVIbFwOk6B4dT2" : {
"-LlkSlkLOZYY7iy5vBWY" : {
"dropoffspot" : "17 nursery crescent",
"key" : "DEzUoNY6BqOVn1DVIbFwOk6B4dT2",
"kutaPoints" : 80,
"pickupspot" : "Mukuba l",
"pickuptime" : "teaTime"
}
}
} }
这是我的驱动器课程
public class DRive {
private String Pickuptime;
private String Pickupspot;
private String Dropoffspot;
private String passengers;
private String Key;
public DRive(String passengers, String Pickupspot, String Pickuptime, String Dropoffspot, String Key){
this.Dropoffspot=Dropoffspot;
this.Pickupspot=Pickupspot;
this.passengers=passengers;
this.Pickuptime=Pickuptime;
//mestime=new Date().getTime();
}
public DRive(){
}
public String getPickuptime(){
return Pickuptime;
}
public void setPickuptime(String time) {
this.Pickuptime = time;
}
public String getDropoffspot() {
return Dropoffspot;
}
public void setDropoffspot(String dropoffspot) {
this.Dropoffspot = Dropoffspot;
}
public String getPickupspot() {
return Pickupspot;
}
public void setPickupspot(String pickupspot) {
this.Pickupspot = Pickupspot;
}
public String getPassengers() {
return passengers;
}
public String getKey() {
return Key;
}
public void setKey(String key) {
Key = key;
}
该应用应该从子order_details
读取数据,并使用ListView
在FirebaseListAdapter
中显示数据,而不是该应用。
该应用应该从数据库中读取,并使用FirebaseListAdapter
在列表视图的各个部分中显示检索到的数据,相反,它甚至不显示ListView
。请提供帮助。
答案 0 :(得分:0)
在您的order_details
节点和实际的DRive
对象之间,还有另一个孩子。我在您的数据库中看到的是已认证用户的uid
。为了解决此问题,您也需要向该孩子添加呼叫。因此,请更改以下代码行:
Query query=FirebaseDatabase.getInstance().getReference().child("order_details");
到
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
Query query=FirebaseDatabase.getInstance().getReference().child("order_details").child(uid);
编辑:
无法使用您的实际数据库结构来一次查询数据库并获得所有用户的所有订单。如果需要,应该重新考虑数据库结构或简单地重复数据。如果要获取所有用户的所有订单,请在下面查看可能的模式:
Firebase-root
|
--- order_details
|
--- orderIdOne
| |
| --- //orderDetails
| |
| --- uid: "DEzUoNY6BqOVn1DVIbFwOk6B4dT2"
|
--- orderIdTwo
|
--- //orderDetails
|
--- uid: "USw0LFY6BqOVn1DVIbFwOkhcvPwf"
要获取所有订单,只需在order_details
节点上附加一个侦听器,如果要获取与单个用户对应的所有订单,请使用以下查询:
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference orderDetailsRef = rootRef.child("order_details");
Query query = orderDetailsRef.orderByChild("uid").equalTo(uid);
query.addListenerForSingleValueEvent(/* ... */);
编辑2:
由于DRive
类中的字段与数据库中的字段不匹配,因此您什么也没得到。为了解决这个问题,请从以下帖子中查看我的答案:
答案 1 :(得分:0)
我找到了解决问题的方法。我添加了如下所示的onstart和onstop侦听器
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
例如,我还通过在引入textview之后添加'v'来对Textview代码进行了调整。 代替 最后的TextView f =(TextView)findViewById(R.id.timed); 我将其更改为 最后的TextView f =(TextView)v。 findViewById(R.id.timed);