【发布时间】:2019-08-08 14:15:56
【问题描述】:
我正在尝试从 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 读取数据并使用 FirebaseListAdapter 将其显示在 ListView 中,而不是应用程序。
应用程序应该从数据库中读取并使用FirebaseListAdapter 在列表视图的部分显示检索到的数据,而不是它甚至不显示ListView。请提供帮助。
【问题讨论】:
标签: java android firebase listview firebase-realtime-database