没有适配器;跳过layout.RecyclerView

时间:2018-02-21 05:17:17

标签: android android-recyclerview

public class FoodList extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;

    FirebaseDatabase database;
    DatabaseReference foodList;
    String CategoryId="";

    FirebaseRecyclerAdapter<Food,FoodViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);

       foodList=FirebaseDatabase.getInstance().getReference().child("Foods");

        recyclerView = findViewById(R.id.recycler_food);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        if (getIntent() != null)
            CategoryId = getIntent().getStringExtra(CategoryId);

        if (CategoryId != null && !CategoryId.isEmpty()) {
            loadListFood(CategoryId);
        }

    }

    private void loadListFood(String CategoryId) {
        adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>(Food.class,R.layout.food_item,FoodViewHolder.class,
                foodList.orderByChild("MenuId").equalTo(CategoryId)) {
            @Override
            protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {

                viewHolder.food_name.setText(model.getName());
                Picasso.with(getBaseContext()).load(model.getImage())
                        .into(viewHolder.food_image);

                final Food local=model;
                viewHolder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        Intent foodDetail=new Intent(FoodList.this,FoodDetail.class);
                        foodDetail.putExtra(FoodId,adapter.getRef(position).getKey());
                        startActivity(foodDetail);
                    }
                });
            }
        };
        recyclerView.setAdapter(adapter);
        Log.d("TAG",""+adapter.getItemCount());




    }
}

我已经设置了适配器,但为什么它显示下面提到的logcat ..请帮助..

02-21 10:35:22.965 29222-29402/com.example.rajatraturi.myrestaur V/FA: Activity resumed, time: 144456223
02-21 10:35:22.977 29222-29222/com.example.rajatraturi.myrestaur E/RecyclerView: No adapter attached; skipping layout
02-21 10:35:23.004 29222-29222/com.example.rajatraturi.myrestaur E/RecyclerView: No adapter attached; skipping layout
02-21 10:35:27.968 29222-29402/com.example.rajatraturi.myrestaur V/FA: Inactivity, disconnecting from the service
02-21 10:35:32.615 29222-29402/com.example.rajatraturi.myrestaur V/FA: Recording user engagement, ms: 9675
02-21 10:3

2 个答案:

答案 0 :(得分:0)

Firebase适配器异步工作。如果在呈现时找不到任何适配器,则系统会记录该错误。这不是问题。这是它假设工作的方式,除非Recycler View在设置适配器后仍然没有呈现任何内容。

答案 1 :(得分:0)

AS Samuel Robert建议firebase适配器异步工作。如果在呈现时没有找到任何适配器,则系统会记录该错误。这不是问题。这是它假设工作的方式,除非RecycleView在设置适配器后也不会渲染任何东西。

尝试创建单独的适配器类extends FirebaseRecyclerAdapter

尝试以下代码:

public class FoodListAdapter extends FirebaseRecyclerAdapter<Food,FoodViewHolder> {

private Context context;
private DatabaseReference foodList=FirebaseDatabase.getInstance().getReference().child("Foods");

FoodListAdapter(String CategoryId,Context context){
    this.context=context;
    super(Food.class,R.layout.food_item,FoodViewHolder.class,foodList.orderByChild("MenuId").equalTo(CategoryId));
}

@Override
protected void populateViewHolder(FoodViewHolder viewHolder, Food model, int position) {

    viewHolder.food_name.setText(model.getName());
    Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);

    final Food local=model;
    viewHolder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {
            Intent foodDetail=new Intent(context,FoodDetail.class);
            foodDetail.putExtra(FoodId,adapter.getRef(position).getKey());
            context.startActivity(foodDetail);
        }
    });
}

}

使用如下:

public class FoodList extends AppCompatActivity {

private RecyclerView recyclerView;
private String CategoryId="";
private FoodListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_food_list);

    recyclerView = findViewById(R.id.recycler_food);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);

    if (getIntent() != null)
        CategoryId = getIntent().getStringExtra(CategoryId);

    adapter = new FoodListAdapter(CategoryId,FoodList.this);
    recyclerView.setAdapter(adapter);
}
}