0

I'm building an app to create travel and show travel information, I have an activity to shows all the travels created by the user, but the problem is, I can only see the travels information, on the phone/emulator that I created the travel, even though I login with the same user on different phone/emulator and using the cloud firestore.

Here is the code of my TravelActivity (the one that has the recyclerView with user travels)

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;

public class TravelsActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference travelRef = db.collection("travels");

    private TravelAdapter adapter;

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

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setSelectedItemId(R.id.searchNav);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);

        Toast.makeText(this, "Sim", Toast.LENGTH_SHORT).show();
        setUpRecyclerView();
    }

    private void setUpRecyclerView() {

        Query query = travelRef.orderBy("timestamp", Query.Direction.DESCENDING)
                .whereEqualTo("userID", FirebaseAuth.getInstance().getCurrentUser().getUid())
                .whereGreaterThanOrEqualTo("timestamp", System.currentTimeMillis());




        FirestoreRecyclerOptions<Travel> options = new FirestoreRecyclerOptions.Builder<Travel>()
                .setQuery(query, Travel.class).build();

        //Toast.makeText(this, "Options: "+options.toString().length() , Toast.LENGTH_SHORT).show();

        adapter = new TravelAdapter(options);

        RecyclerView recyclerView = findViewById(R.id.travels_recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);

        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                adapter.deleteItem(viewHolder.getAdapterPosition());
                adapter.notifyDataSetChanged();
            }
        }).attachToRecyclerView(recyclerView);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
  • Are you sure it's the same user? Have you checked the UID for both of them? Have you also created an index? – Alex Mamo Feb 03 '21 at 05:14
  • @AlexMamo Yes, I checked the UID and it's the same for both. What do you mean with index? – Leonil Sulude Feb 03 '21 at 11:20
  • I'm talking about this [index](https://stackoverflow.com/questions/50305328/firestore-whereequalto-orderby-and-limit1-not-working). Did you? – Alex Mamo Feb 03 '21 at 11:22
  • As mentioned by Alex Mamo, you have to create an index or the query won't work, try creating the index with the instructions on his answer and let us know if it worked. – Ralemos Feb 03 '21 at 15:04
  • I created the index and now it's working, thank you. – Leonil Sulude Feb 03 '21 at 16:43

1 Answers1

0

Posting this as a Community Wiki as it's based on @AlexMamo's comments.

The problem is that you are did not create an index for this query, you can find instructions on how to create by following that documentation or by following Alex's Answer.

Ralemos
  • 5,571
  • 2
  • 9
  • 18