0

I'm trying to get data from Firestore then display it using a CardView. It seems to get all the necessary code in, but when I run the app, no data is displayed. The app runs without crashing. I can't figure out where the problem is. I'd be very grateful if someone could show me what I'm missing or where the wrong is.

enter image description here

Main Activity:

public class MainActivity extends AppCompatActivity implements
    GoogleApiClient.OnConnectionFailedListener,
    RestaurantAdapter.OnRestaurantSelectedListener {


private static final String TAG = "MainActivity";

private static final int RC_SIGN_IN = 9001;


// FireStore
@BindView(R.id.recycler_restaurants)
RecyclerView mRestaurantsRecycler;

private FirebaseFirestore mFirestore;
private Query mQuery;

private RestaurantAdapter mAdapter;


// FireStore END


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    ButterKnife.bind(this);


    // Enable Firestore logging
    FirebaseFirestore.setLoggingEnabled(true);

    // Firestore
    mFirestore = FirebaseFirestore.getInstance();

    // Get ${LIMIT} restaurants
    mQuery = mFirestore.collection("restaurants")
            .limit(5);


    // RecyclerView
    mAdapter = new RestaurantAdapter(mQuery, this);


    mRestaurantsRecycler.setLayoutManager(new LinearLayoutManager(this));
    mRestaurantsRecycler.setAdapter(mAdapter);


}


@Override
protected void onStart() {
    super.onStart();

    // Start listening for Firestore updates
    if (mAdapter != null) {
        mAdapter.startListening();
    }

}


@Override
protected void onResume() {
    super.onResume();

    if (mAdapter != null) {
        mAdapter.stopListening();
    }

}


@Override
public void onStop() {
    super.onStop();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    }
    return super.onOptionsItemSelected(item);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {

    }
}


@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}


@Override
public void onRestaurantSelected(DocumentSnapshot restaurant) {

}

Restaurant Adapter

public class RestaurantAdapter extends FireStoreAdapter<RestaurantAdapter.ViewHolder> {


public interface OnRestaurantSelectedListener {

    void onRestaurantSelected(DocumentSnapshot restaurant);

}


private OnRestaurantSelectedListener mListener;


public RestaurantAdapter(Query query, OnRestaurantSelectedListener listener) {
    super(query);
    mListener = listener;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    return new ViewHolder(inflater.inflate(R.layout.item_restaurant, parent, false));
}


@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(getSnapshot(position), mListener);
}


static class ViewHolder extends RecyclerView.ViewHolder {


    @BindView(R.id.restaurant_item_name)
    TextView nameView;

    @BindView(R.id.restaurant_item_description)
    TextView descriptionView;


    public ViewHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }

    public void bind(final DocumentSnapshot snapshot,
                     final OnRestaurantSelectedListener listener) {

        Restaurant restaurant = snapshot.toObject(Restaurant.class);


        nameView.setText(restaurant.getName());
        descriptionView.setText(restaurant.getDescription());


        // Click listener
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onRestaurantSelected(snapshot);
                }
            }
        });
    }

}

Recycler:

<!-- Main Restaurants recycler -->
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_restaurants"
    android:layout_width="368dp"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    tools:listitem="@layout/item_restaurant"
    tools:layout_editor_absoluteY="0dp"
    tools:layout_editor_absoluteX="8dp" />

CardView:

    <android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    app:cardElevation="10dp">


    <LinearLayout
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/restaurant_item_name"
            android:textSize="30sp"
            android:textColor="@color/colorPrimaryDark"

            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <TextView
            android:id="@+id/restaurant_item_description"
            android:textSize="20sp"
            android:textColor="@color/colorPrimaryDark"
            android:textStyle="italic"

            android:layout_width="match_parent"
            android:layout_height="wrap_content" />




    </LinearLayout>

</android.support.v7.widget.CardView>
Styx
  • 9,863
  • 8
  • 43
  • 53
Andrew Irwin
  • 691
  • 12
  • 40
  • your `onActivityResult` is empty should there be anything there? – Peter Haddad Nov 07 '17 at 10:13
  • 1
    For future visitors, you can take a look **[here](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)**, where I have explained step by step how to display data from Firestore into a `RecyclerView` using Android. – Alex Mamo Mar 14 '18 at 13:05
  • I've moved into using React Native for creating this app instead. It seems to be a lot lot easier !!! Especially for this particular part! & you write the one lot of code that then makes an android app & iOS app out of the one code base. – Andrew Irwin Mar 15 '18 at 09:59

1 Answers1

0

Try to set debug breakpoints. I think you're calling

mAdapter.startListening()

before the adapter is instantiated.

Try removing it from onStart() and put after the adapter setup. If results starts to come, refactor your code as you prefer.

Valerio
  • 3,297
  • 3
  • 27
  • 44