-2

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>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Are you calling `startListening` anywhere. Otherwise the listener won't be active yet. See https://stackoverflow.com/questions/47228262/firebaselistadapter-not-pushing-individual-items-for-chat-app-firebase-ui-3-1/47228433#47228433 – Frank van Puffelen Mar 16 '18 at 04:10

2 Answers2

0

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

DynamicMind
  • 4,240
  • 1
  • 26
  • 43
0

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.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193