不使用FirebaseListAdapter从ListView上的Firebase获取数据 [英] Not Getting Data From Firebase on ListView With FirebaseListAdapter
问题描述
此代码的问题是列表视图中未显示数据,并且应用程序也未显示任何错误.我正在从其他活动中初始化此模型类,所以请任何人可以给我关于什么是真正的问题的建议.
The Problem with this code is that the data is not showing in listview and the app is also not showing any error. I am initializing this model class from some other activity, so please any one can give me suggestion about what would be the real problem.
public class FireBaseListAdapter extends AppCompatActivity {
private ListView mlistview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fire_base_list_adapter);
mlistview = (ListView)findViewById(R.id.firebase_list);
// DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
final Query query = getInstance().getReference();
FirebaseListOptions<Users> options = new FirebaseListOptions.Builder<Users>().setQuery(query,Users.class).setLayout(R.layout.listview_for_firebaselistadapter).build();
FirebaseListAdapter<Users> firebaseListAdapter = new FirebaseListAdapter<Users>(options) {
@Override
protected void populateView(View v, Users model, int position) {
model = getItem(position);
TextView textViewname = (TextView)v.findViewById(R.id.listAdapter_nametext);
TextView textViewplace = (TextView)v.findViewById(R.id.listAdapter_placetext);
textViewname.setText(model.getName());
textViewplace.setText(model.getPlace());
}
};
mlistview.setAdapter(firebaseListAdapter);
}
}
这是我的模型班:
public class Users {
private String name;
private String place;
public Users() {
}
public Users(String name, String place) {
this.name = name;
this.place = place;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
}
这是我的XML,其中包含两个textView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/listAdapter_nametext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Driver Name"
android:textSize="25sp"/>
<TextView
android:id="@+id/listAdapter_placetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Driver Place"
android:textSize="25sp"/>
</LinearLayout>
推荐答案
要解决此问题,您需要覆盖onStart()
方法并添加以下代码行:
To solve this, you need to override the onStart()
method and add the following line of code:
@Override
protected void onStart() {
super.onStart();
firebaseListAdapter.startListening();
}
但是首先不要忘记使firebaseListAdapter
变量成为全局变量.
But first don't forget to make the firebaseListAdapter
variable global.
private FirebaseListAdapter<Users> firebaseListAdapter;
然后删除FirebaseListAdapter<Users>
,在其中初始化适配器.
And then just remove FirebaseListAdapter<Users>
where you intialize the adapter.
这篇关于不使用FirebaseListAdapter从ListView上的Firebase获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!