问题
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);
}
}
This is My Model Class:
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;
}
}
This is My XML containing two 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>
回答1:
It seems to you forgot to pass reference in your query
final Query query = getInstance().getReference();
// Create a reference to the cities collection
CollectionReference citiesRef = db.collection("cities");
// Create a query against the collection.
Query query = citiesRef.whereEqualTo("state", "CA");
Reference
回答2:
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();
}
But first don't forget to make the firebaseListAdapter
variable global.
private FirebaseListAdapter<Users> firebaseListAdapter;
And then just remove FirebaseListAdapter<Users>
where you intialize the adapter.
来源:https://stackoverflow.com/questions/49313035/not-getting-data-from-firebase-on-listview-with-firebaselistadapter