如何在FirebaseRecyclerAdapter中发出2个查询

时间:2018-05-31 17:15:17

标签: java android firebase firebase-realtime-database firebaseui

我有2个查询来自不同的源(两个firebase数据库实时),我想在FirebaseRecyclerAdapter中显示。我一直在反对这个特殊问题,但我想不出任何事情。

我如何实现这一点并将其合并到下面的代码中。

喜欢JSON结构

 "Likes" : {
  "-LAJRnaAaCGrbv8-cXvD" : {
    "5SWi7kiLTdW4WS1MDV5Knxeo0qt1" : {
      "-LDqyWmTa-sbgKpxOztv" : {
        "Likes" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1"
      }
    },
    "6Zv4wIWV3WOyHxgABXkecK18IJ03" : {
      "-LDlj87M--DZVBgBJTUT" : {
        "Likes" : "6Zv4wIWV3WOyHxgABXkecK18IJ03"
      }
    },
    "ktLMV4x9kGN43Ggrx5YWhkaG7BL2" : {
      "-LDkQ-jAwb7oI3N4T__R" : {
        "Likes" : "ktLMV4x9kGN43Ggrx5YWhkaG7BL2"
      }
    }
  }
},

配方JSON结构

 "PostRecipe" : {
  "-LAJRnaAaCGrbv8-cXvD" : {
    "appetizer" : "Appetizer",
    "cooking_time" : "40 mins",
    "desert" : "Dessert",
    "difficult_level" : "5",
    "main_course" : "Main course",
    "name" : "Smokey Party Jollof",
    "number_of_people" : "5",
    "photo1" : "https://firebasestorage.googleapis.com/v0/b/chefcook-1865d.appspot.com/o/recipe_photos%2F43262?alt=media&token=07103c5d-9924-4e02-985c-fbc02920228f",
    "photo2" : "https://firebasestorage.googleapis.com/v0/b/chefcook-1865d.appspot.com/o/recipe_photos%2F43237?alt=media&token=1d398256-0058-4f78-a13f-66301acc4f75",
    "photo3" : "something",
    "photo4" : "something",
    "pizza_fritas" : "Pizza, fritas",
    "prepare_time" : "3hrs",
    "side_dish" : "Side dish",
    "tips" : "Blended stew base is the best base for making traditional stew.",
    "userId" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1",
    "vegetarian" : "Vegetarian"
  }
}

},

MainRecipeFragment.java

...  
mDatabase = FirebaseDatabase.getInstance().
            getReference().child("Post").child("PostRecipe").orderByChild("photo1").startAt(n);

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

mAdapter = new FirebaseRecyclerAdapter<RecipeModel, NewViewHolder>(RecipeModel.class, R.layout
            .activty_content_list,
            NewViewHolder.class, mDatabase) {
        @Override
        protected void populateViewHolder(final NewViewHolder viewHolder, final RecipeModel model,
                                          final int
                position) {
            final DatabaseReference postRef = getRef(position);

            Log.e(TAG , "adpter" + model.getName() + model.getPhoto1());


            // Set click listener for the whole post view
            final String cheeseKey = postRef.getKey();
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Launch PostDetailActivity
                    Intent intent = new Intent(getActivity(), RecipeDetails.class);
                    intent.putExtra(RecipeDetails.EXTRA_CHEESE_KEY, cheeseKey);

                    startActivity(intent);
                }
            });

            // Bind Post to ViewHolder
            Context context =  getActivity();
            viewHolder.bindToCheese(model, context);

        }
    };

ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder {

String LOG_TAG = ViewHolder.class.getSimpleName();

private View mView;
private ImageView mImageView;
private TextView mTextView;
private static final String TAG = ViewHolder.class.getSimpleName();


public ViewHolder(View view) {
    super(view);
    mView = view;
    mImageView =  view.findViewById(R.id.soup_thumbnail);

    mTextView = view.findViewById(R.id.soup_title);

    //getActivity();


}


public void bindToCheese(Cheeses cheeses, Context c) {


  mTextView.setText(cheeses.getTitle());
    Log.d(TAG, "title"+ cheeses.getTitle());

        Picasso.with(c).load(String.valueOf(cheeses.getImagerUrl())).placeholder(R.mipmap.ic_launcher).fit().into(mImageView);

    Log.e(LOG_TAG, "imageurl"+ cheeses.getImagerUrl() + " title" + cheeses.getTitle()) ;

    //Here, how do i reach Like and extract information inside.
    //I tried the below log, but it is returning null
    Log.e(LOG_TAG, "likes"+ cheeses.getLikes());

}

1 个答案:

答案 0 :(得分:1)

您正在以关系方式思考数据库,只是轻松实现,而不是循环2次并获得2个查询,您只需1种方法即可完成。 (这就是firebase结构允许轻松实现的原因)

例如,假设您有食谱和喜欢,您可以在内部食谱中添加每个食谱,然后循环一次进入该表,获取喜欢,获取食谱并在您的循环中显示所有内容recyclerview,无需考虑查询2个不同的节点。

所以,我建议你在数据库中进行重组,将喜欢的内容放在每个食谱中,然后循环一次。

例如,你有这个

Dog[] mydogs = new Dog[3]; 
int x = 0; 
if (x<3) { 
   mydogs[x] = new Dog();
   mydogs[x].size = 12;
   mydogs[x].bark(); //Yip Yip 
   x = x+1;
 }

void bark() { 
    if (size > 60) {
        System.out.println("Woof, Woof ");
    } else if (size > 14) {
        System.out.println("Ruff, Ruff "); 
    } else { 
       System.out.println("Yip, Yip ");
 }

这与此食谱相对应

"Likes" : {
  "-LAJRnaAaCGrbv8-cXvD" : {
    "5SWi7kiLTdW4WS1MDV5Knxeo0qt1" : {
      "-LDqyWmTa-sbgKpxOztv" : {
        "Likes" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1"
      }

所以,只需将其添加到 "PostRecipe" : { "-LAJRnaAaCGrbv8-cXvD" : { "appetizer" : "Appetizer", "cooking_time" : "40 mins", "desert" : "Dessert", "difficult_level" : "5", "main_course" : "Main course",... 内,就可以在pushID内添加食谱和食谱,这样您只需循环一次,并通过1个查询通过所有食谱< / p>

这是你需要做的事情

PostRecipe

然后,通过这种结构,您只需查询一次表格即可得到每个食谱的相似内容。

在你的RecipeModel中,你应该拥有你需要的所有变量的setter和getter,例如name,appetizers,photo1等等,确保变量的名称与firebase数据库中的相同。

所以,要获取数据,你应该做这样的事情

请记住,您的mRef应该从您的UserID级别开始。那是 "PostRecipe" : { "-LAJRnaAaCGrbv8-cXvD" : { "appetizer" : "Appetizer", "likes":{ "5SWi7kiLTdW4WS1MDV5Knxeo0qt1" : { "-LDqyWmTa-sbgKpxOztv" : { "Likes" : "5SWi7kiLTdW4WS1MDV5Knxeo0qt1" } "cooking_time" : "40 mins", "desert" : "Dessert", "difficult_level" : "5", "main_course" : "Main course",...

mRef.child("PostRecipe").child(uid).addValue....