我只想获得用户收藏夹中具有所需城市的项目。 例如,如果用户有20个收藏夹项,并且想要过滤特定城市中存在的收藏夹,该如何过滤呢?
对不起,我的英语。如果不清楚,请问我。
答案 0 :(得分:0)
看看下面的数据库结构:
root : {
user_01 : {
fav_01 : {
data : val,
city : "city_01"
}
fav_02 : {
data : val,
city : "city_02"
}
fav_03 : {
data : val,
city : "city_01"
}
}
}
现在在andorid中进行这样的查询:
String user = "user_01";
String city_name = "city_01";
int limit = 10;
DatabaseReference favRef= FirebaseDatabase.getInstance().getReference("root/").child(user);
Query query = favRef.orderByChild("city").equalTo(city_name).limitToFirst(limit);
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.e(TAG, "onDataChange: "+dataSnapshot.toString());
//you will get fav_01 and fav_3 objects
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e(TAG, "onCancelled: "+databaseError.toString());
}
});
希望它可以帮助您 快乐的编码:)
答案 1 :(得分:0)
package com.you.package.name;
import android.support.annotation.NonNull;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
/**
* Created by harkal on 26-10-2018.
*/
public class Filter {
private static final String TAG = "";
private String city;
private String user;
private OnFoodDataCallBack onFoodDataCallBack;
public void setOnFoodDataCallBack(OnFoodDataCallBack onFoodDataCallBack) {
this.onFoodDataCallBack = onFoodDataCallBack;
}
public Filter(String city, String user) {
this.city = city;
this.user = user;
}
public void init(){
DatabaseReference userFavRef = FirebaseDatabase.getInstance().getReference("Path/To/Users/");
userFavRef.child(user).child("favourites").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
//now get the favs with the filtered city
int initKey = 0;
final int totalFavs = (int)dataSnapshot.getChildrenCount();
final ArrayList<String> foodsWithCity = new ArrayList<>();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
initKey = initKey+1;
String foodKey = snapshot.getKey();//this is the key for every fav food
//now query all the food nodes with the city
DatabaseReference foodRef = FirebaseDatabase.getInstance().getReference("Path/To/Foods/");
Query query = foodRef.child(foodKey).orderByChild("city").equalTo(city);
final int finalInitKey = initKey;
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
//there is a food item with the queried city
foodsWithCity.add(dataSnapshot.getKey());
if(finalInitKey == totalFavs){
if(onFoodDataCallBack != null){
onFoodDataCallBack.onSuccess(foodsWithCity);
}
}
}else{
//there is no food with this city
if(finalInitKey == totalFavs){
if(onFoodDataCallBack != null){
onFoodDataCallBack.onFailed("there is no food with this city");
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
if(onFoodDataCallBack != null){
onFoodDataCallBack.onFailed(databaseError.toString());
}
}
});
}
}else{
if(onFoodDataCallBack != null){
onFoodDataCallBack.onFailed("no fav data about user");
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
if(onFoodDataCallBack != null){
onFoodDataCallBack.onFailed(databaseError.toString());
}
}
});
}
public interface OnFoodDataCallBack{
void onSuccess(ArrayList<String> foodWithCities);
void onFailed(String error);
}
}
您可以通过以下方式使用代码:
Filter filter = new Filter("washingaton", "user_01");
filter.setOnFoodDataCallBack(new Filter.OnFoodDataCallBack() {
@Override
public void onSuccess(ArrayList<String> foodWithCities) {
//here is the data you wanted
}
@Override
public void onFailed(String error) {
//handle the error and change ui accordingly
Log.e(TAG, "onFailed: "+error);
}
});
filter.init();
希望有帮助
快乐的编码:)