I am using Firebase-UI with firestore, Now there are chances that no documents are returned against a particular query, so how would I know that?
progressBar.setVisibility(View.VISIBLE);
SharedPreferences prefs = getActivity().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String city = prefs.getString("selectedCity", "pakistan");
Toast.makeText(getActivity(), city, Toast.LENGTH_SHORT).show();
Query query = db.collection("cities/"+city+"/"+category);
FirestoreRecyclerOptions<ExploreModel> options = new FirestoreRecyclerOptions.Builder<ExploreModel>()
.setQuery(query, ExploreModel.class)
.build();
adapter = new ExploreAdapter(options);
recyclerView.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
adapter.setOnItemClickListener(new ExploreAdapter.OnItemClickListener() {
@Override
public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
// ExploreModel exploreModel = documentSnapshot.toObject(ExploreModel.class);
// String id = documentSnapshot.getId();
String path = documentSnapshot.getReference().getPath();
Toast.makeText(getActivity(), "Position" + position + " path :" + path, Toast.LENGTH_SHORT).show();
Intent detailActivityIntent = new Intent(getActivity(), SingleItemDetailsActivity.class);
detailActivityIntent.putExtra("document_path", path);
detailActivityIntent.putExtra("category",category);
startActivity(detailActivityIntent);
}
});
There is an empty screen if no results/ documents are returned, so I want to show a proper empty screen etc.
You arent't getting anything because the name of your fieds within the model class do not match the name of the fields in your database. See mDescription
vs description
? Both must be the same.
To solve this, you either change the model class to look like this:
public class ExploreModel {
private String title, description, imageUrl, location;
public ExploreModel() {}
public ExploreModel(String title, String description, String imageUrl, String location) {
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.location = location;
}
public String getTitle() { return title; }
public String getDescription() { return description; }
public String getImageUrl() { return imageUrl; }
public String getLocation() { return location; }
}
Or you can use annotations:
public class ExploreModel {
private String title, description, imageUrl, location;
public ExploreModel() {}
public ExploreModel(String title, String description, String imageUrl, String location) {
this.title = title;
this.description = description;
this.imageUrl = imageUrl;
this.location = location;
}
@PropertyName("mTitle")
public String getTitle() { return title; }
@PropertyName("mDescription")
public String getDescription() { return description; }
@PropertyName("mImageUrl")
public String getImageUrl() { return imageUrl; }
@PropertyName("mLocation")
public String getLocation() { return location; }
}