无法在FirebaseRecyclerAdapter类

时间:2018-02-04 15:31:44

标签: android android-recyclerview firebaseui

我尝试使用Firebase在recyclerView中显示图像和三个textView,但我得到的只是一个空白页面。我认为onBindViewHolder方法存在问题,但我似乎无法找到它。

的onCreate

    mFoodList=(RecyclerView)findViewById(R.id.foodList);
    mFoodList.setHasFixedSize(true);
    mFoodList.setLayoutManager(new LinearLayoutManager(this));
    mDatabase= FirebaseDatabase.getInstance().getReference().child("Item");
    mQuery=mDatabase.orderByKey();

    mOptions=new FirebaseRecyclerOptions.Builder<Food>().setQuery(mQuery, Food.class).build();
    mAuth=FirebaseAuth.getInstance();
    mAuthListener=new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if(firebaseAuth.getCurrentUser()==null){
                Intent loginIntent=new Intent(MenuActivity.this,MainActivity.class);
                loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(loginIntent);
            }
        }
    };

onStart - 这就是我遇到问题的地方。

    @Override
  protected void onStart() {
    super.onStart();
    mAuth.addAuthStateListener(mAuthListener);
    FBRA = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(mOptions) {
        @Override
        protected void onBindViewHolder(@NonNull FoodViewHolder viewHolder, int position, @NonNull Food model) {
            viewHolder.setName(model.getName());
            viewHolder.setPrice(model.getPrice());
            viewHolder.setDesc(model.getDesc());
            viewHolder.setImage(getApplicationContext(),model.getImage());
        }

        @Override
        public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.singlemenuitem, parent, false);

            return new FoodViewHolder(view);
        }

    };
    mFoodList.setAdapter(FBRA);
}

FoodViewHolder

    public static class FoodViewHolder extends RecyclerView.ViewHolder{
    View mView;
    public FoodViewHolder(View itemView) {
        super(itemView);
        mView=itemView;
    }

    public void setName(String name){
        TextView food_name=(TextView)mView.findViewById(R.id.foodName);
        food_name.setText(name);
    }

    public void setPrice(String price){
        TextView food_price=(TextView)mView.findViewById(R.id.foodPrice);
        food_price.setText(price);
    }

    public void setDesc(String desc){
        TextView food_desc=(TextView)mView.findViewById(R.id.foodDesc);
        food_desc.setText(desc);
    }

    public void setImage(Context ctx, String image){
        ImageView food_image=(ImageView)mView.findViewById(R.id.foodImage);
        Picasso.with(ctx).load(image).into(food_image);
    }
}

2 个答案:

答案 0 :(得分:0)

除非您使用的是FirebaseRecyclerOptions.Builder#setLifecycleOwner(this),否则您必须手动拨打FirebaseRecyclerAdapter#[start/stop]Listening()。我建议你阅读documentation

答案 1 :(得分:0)

As said by SUPERCILEX,您正在使用FirebaseRecyclerOption.Builder, 因此您需要手动启动和停止FirebaseRecyclerView监听。

因此将Onstart()

mFoodList.startListening();

和OnStop()

mFoodList.stopListening();