I have a firebase database with a table Trips which i created to record all the trips which user make. In trips database i have a node called done which return the boolean value true if the trip is done or false if trip is canceled or not done.
I want to display all data in recyclerview from a trip database which ridermodel node equal to current user and done node equals to true(boolean). But when i populate data in a recyclerview i have managed to populate all the data for current user using orderByChild , but when i try to filter data to get only data with true value in done node i cant and the result i get all the data including data which is false in done node.
private void loadTripplannerList() {
String user = FirebaseAuth.getInstance().getCurrentUser().getUid();
List<TripPlannerModel> tempList = new ArrayList<>();
DatabaseReference tripRef = FirebaseDatabase.getInstance().getReference(Common.Trip);
query = tripRef.orderByChild("rider").equalTo(user);
options = new FirebaseRecyclerOptions.Builder<TripPlannerModel>()
.setLifecycleOwner(this)
.setQuery(query, TripPlannerModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<TripPlannerModel, CompletedTripAdapter>(options) {
@Override
protected void onBindViewHolder(@NonNull final CompletedTripAdapter holder, int position,
@NonNull final TripPlannerModel model) {
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()) {
Boolean isDone = itemSnapshot.child("done").getValue(Boolean.class);
if (isDone.equals(true)){
compositeDisposable.add(iGoogleAPI.getDirections("driving",
"less_driving",
model.getOrigin(), model.getDestination(),
getString(R.string.google_api_key))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(returnResult -> {
try {
//parse json
JSONObject jsonObject = new JSONObject(returnResult);
JSONArray jsonArray = jsonObject.getJSONArray("routes");
JSONObject object = jsonArray.getJSONObject(0);
JSONArray legs = object.getJSONArray("legs");
JSONObject legObjects = legs.getJSONObject(0);
String start_address = legObjects.getString("start_address");
holder.txt_pickup.setText(Common.formatAdress(start_address));
} catch (Exception e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}));
TripPlannerModel model = itemSnapshot.getValue(TripPlannerModel.class);
tempList.add(model);
}
try {
marker_me = "color:holo_red|" + model.getOrigin();
// marker_dest = "color:orange|label:7|San Francisco,USA";
key = getString(R.string.google_api_key);
marker_me = URLEncoder.encode(marker_me, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//String STATIC_MAP_API_ENDPOINT = "https://maps.googleapis.com/maps/api/staticmap?size=" +width+ "x300&path=" + path + "&markers=" + marker_me + "&markers=" + marker_dest + "&key=" + key;
String STATIC_MAP_API_ENDPOINT = "https://maps.googleapis.com/maps/api/staticmap?size=" + width / 2 + "x300&markers=" + marker_me + "&zoom=14&style=feature:poi|element:labels|visibility:off&key=" + key;
;
Long timeStamp = model.getComplete_time();
holder.txt_trip_date.setText(UserUtils.getDate(timeStamp));
Picasso.get()
.load(STATIC_MAP_API_ENDPOINT)
.into(holder.img_map);
holder.txt_destination.setText(model.getDestinationString());
holder.txt_trip_status.setText(R.string.status_complete);
holder.setItemClickListerner(new IRecyclerClickListerner() {
@Override
public void onItemClickListerner(View view, int pos) {
//
Toast.makeText(getActivity(), adapter.getRef(position).getKey(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(), TripHistorySinglePage.class);
intent.putExtra("destination", getItem(position).getDestinationString());
startActivity(intent);
}
});
}
}
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError){
Toast.makeText(getContext(), "" + databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}