如何使用firebase适配器使用cardview从recyclelerview将数据传递到另一个活动

时间:2017-05-10 07:52:09

标签: android firebase firebase-realtime-database android-recyclerview android-cardview

我使用firebase实时数据库实现了使用cardview的recyclelerview,我在将数据传递给另一个活动时遇到问题,当用户点击cardview时,数据应该从cardview传递到另一个名为“DetailActivity”的活动,然后DetailActivity应该打开< / p>

这是firebase适配器

    FirebaseRecyclerAdapter<Food,FoodViewHolder> adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
            Food.class,
            R.layout.food_item,
            FoodViewHolder.class,

            //referencing the node where we want the database to store the data from our Object
            mDatabaseReference.child("users").child(userId).child("food").getRef()
    ) {


        @Override
        protected void populateViewHolder(FoodViewHolder viewHolder, final Food model, int position) {
            if(tvNoMovies.getVisibility()== View.VISIBLE){
                tvNoMovies.setVisibility(View.GONE);
            }
            viewHolder.tvFoodName.setText(model.getFoodName());
            viewHolder.ratingBar.setRating(model.getFoodRating());
            viewHolder.tvFoodCategory.setText(model.getFoodCategory());
            viewHolder.tvFoodAvailableUpto.setText(model.getFoodAvailableUpto());
            viewHolder.tvFoodPrice.setText(model.getFoodPrice());
            Picasso.with(getActivity().getApplicationContext()).load(model.getFoodPoster()).into(viewHolder.ivFoodPoster);

        }
    };
    mRecyclerView.setAdapter(adapter);

这是我的viewholder.class

    public static class FoodViewHolder extends RecyclerView.ViewHolder{

    TextView tvFoodName;
    RatingBar ratingBar;
    ImageView ivFoodPoster;
    TextView tvFoodCategory;
    TextView tvFoodAvailableUpto;
    TextView tvFoodPrice;

    public FoodViewHolder(View v) {
        super(v);
        tvFoodName = (TextView) v.findViewById(R.id.tvName);
        ratingBar = (RatingBar) v.findViewById(R.id.rating_bar);
        ivFoodPoster = (ImageView) v.findViewById(R.id.imgThumb);
        tvFoodCategory = (TextView) v.findViewById(R.id.tvCategory);
        tvFoodAvailableUpto= (TextView) v.findViewById(R.id.tvavailable);
        tvFoodPrice = (TextView) v.findViewById(R.id.tvprice);
    }

}

这是我的Food.class

    public class Food {
public String foodName;
public String foodPoster;
public float  foodRating;
public String foodCategory;
public String foodAvailableUpto;
public String foodPrice;

public Food(){

}
public Food(String foodName,String foodPoster,float foodRating, String foodCategory , String foodAvailableUpto , String foodPrice){
    this.foodName = foodName;
    this.foodPoster = foodPoster;
    this.foodRating  = foodRating;
    this.foodCategory = foodCategory;
    this.foodAvailableUpto = foodAvailableUpto;
    this.foodPrice = foodPrice;
}

Here is the screenshot of recycleview with cardview

1 个答案:

答案 0 :(得分:0)

现在,当用户点击食物名称时,您可以将用户发送到其他活动:

 FirebaseRecyclerAdapter<Food,FoodViewHolder> adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(
                Food.class,
                R.layout.food_item,
                FoodViewHolder.class,

                //referencing the node where we want the database to store the data from our Object
                mDatabaseReference.child("users").child(userId).child("food").getRef()
        ) {


            @Override
            protected void populateViewHolder(FoodViewHolder viewHolder, final Food model, int position) {
                if(tvNoMovies.getVisibility()== View.VISIBLE){
                    tvNoMovies.setVisibility(View.GONE);
                }
                viewHolder.tvFoodName.setText(model.getFoodName());
                  viewHolder.tvFoodName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(),Activity2.class);//replace activity2 with ur intended activity
intent.putExtra();//put whatever data you need to send
                 startActivity(intent)
            }
        });
                viewHolder.ratingBar.setRating(model.getFoodRating());
                viewHolder.tvFoodCategory.setText(model.getFoodCategory());
                viewHolder.tvFoodAvailableUpto.setText(model.getFoodAvailableUpto());
                viewHolder.tvFoodPrice.setText(model.getFoodPrice());
                Picasso.with(getActivity().getApplicationContext()).load(model.getFoodPoster()).into(viewHolder.ivFoodPoster);

            }
        };
        mRecyclerView.setAdapter

(适配器);