提供错误未连接适配器;跳过布局

时间:2018-06-25 07:16:15

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

未显示空白屏幕的Firebase recyclerview适配器

after clicking on food item menu pic1

shows blank screen no adapter attached pic2

FoodList.java

public class FoodList extends AppCompatActivity {

    RecyclerView recycler_food;
    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);

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

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

        if (getIntent() != null)
            categoryId = getIntent().getStringExtra("CategoryId");
        if (categoryId != null && categoryId.isEmpty()) {
            loadListFood(categoryId);
        }
    }

    private void loadListFood(String categoryId) {

        Query food = null;
        assert food != null;
        FirebaseRecyclerOptions<Food> options =
                new FirebaseRecyclerOptions.Builder<Food>()
                        .setQuery(food, Food.class).build();

        adapter = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(options) {
            @Override
            public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.food_item, parent, false);

                return new FoodViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull FoodViewHolder holder, int position, @NonNull Food model) {
                holder.food_name.setText(model.getName());
                Picasso.with(getBaseContext()).load(model.getImage()).into(holder.food_image);
                final Food local = model;
                holder.setItemClickListener(new ItemClickListener() {
                    @Override
                    public void onClick(View view, int position, boolean isLongClick) {
                        Toast.makeText(FoodList.this, "" + local.getName(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        recycler_food.setAdapter(adapter);
        adapter.startListening();
    }
}

FoodViewHolder.java

public class FoodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView food_name;
    public ImageView food_image;

    private ItemClickListener itemClickListener;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public FoodViewHolder(View itemView) {
        super(itemView);

        food_name = itemView.findViewById(R.id.food_name);
        food_image = itemView.findViewById(R.id.food_image);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onClick(view,getAdapterPosition(),false);
    }

}

Logcat错误

    06-26 18:29:40.629 13335-13335/com.example.sharma.digimenu E/RecyclerView: No adapter attached; skipping layout
06-26 18:29:40.631 13335-13395/com.example.sharma.digimenu D/FA: Logging event (FE): screen_view(_vs), Bundle[{firebase_event_origin(_o)=auto, firebase_previous_class(_pc)=Home, firebase_previous_id(_pi)=-1158027862533495814, firebase_screen_class(_sc)=FoodList, firebase_screen_id(_si)=-1158027862533495813}]
06-26 18:29:40.647 13335-13335/com.example.sharma.digimenu E/RecyclerView: No adapter attached; skipping layout
06-26 18:29:40.674 13335-13335/com.example.sharma.digimenu I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@eda8955 time:88445751
06-26 18:29:40.678 13335-13395/com.example.sharma.digimenu V/FA: Activity resumed, time: 232650897
06-26 18:29:45.682 13335-13395/com.example.sharma.digimenu V/FA: Inactivity, disconnecting from the service
06-26 18:30:40.754 13335-14012/com.example.sharma.digimenu V/FA: Recording user engagement, ms: 60148
06-26 18:30:40.755 13335-14012/com.example.sharma.digimenu V/FA: Connecting to remote service
06-26 18:30:40.761 13335-14012/com.example.sharma.digimenu V/FA: Activity paused, time: 232711039
06-26 18:30:40.772 13335-14012/com.example.sharma.digimenu D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=60148, firebase_screen_class(_sc)=FoodList, firebase_screen_id(_si)=-1158027862533495813}]
06-26 18:30:41.554 13335-14012/com.example.sharma.digimenu V/FA: Connection attempt already in progress
06-26 18:30:41.554 13335-14012/com.example.sharma.digimenu D/FA: Connected to remote service
06-26 18:30:41.556 13335-14012/com.example.sharma.digimenu V/FA: Processing queued up service tasks: 2
06-26 18:30:47.159 13335-14012/com.example.sharma.digimenu V/FA: Inactivity, disconnecting from the service
06-26 18:31:58.743 13335-14279/com.example.sharma.digimenu V/FA: Connecting to remote service
06-26 18:31:58.808 13335-13335/com.example.sharma.digimenu I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@eda8955 time:88583885
06-26 18:31:58.969 13335-14279/com.example.sharma.digimenu V/FA: Activity resumed, time: 232789034
06-26 18:31:58.978 13335-14279/com.example.sharma.digimenu D/FA: Connected to remote service
06-26 18:31:58.978 13335-14279/com.example.sharma.digimenu V/FA: Processing queued up service tasks: 1

未显示空白屏幕的Firebase recyclerview适配器

after clicking on food item menu pic1

shows blank screen no adapter attached pic2

1 个答案:

答案 0 :(得分:0)

根据我在代码中看到的,您永远不会将适配器设置为recyclerview。

最后在loadMenu()中执行此操作:

     recycler_menu.setAdapter(adapter);

更新

尝试添加此内容:

recycler_menu.setHasFixedSize(true);

在此之下:

recycler_menu.setLayoutManager(layoutManager);

并更改此内容:

 public class MenuViewHolder extends RecyclerView.ViewHolder

对此:

 public static class MenuViewHolder extends RecyclerView.ViewHolder

针对用户的STEPS:

以此替换loadMenu()中的所有内容

private void loadMenu(){

//make options

 FirebaseRecyclerOptions<Category> options =
                new FirebaseRecyclerOptions.Builder<Category>()
                        .setQuery(category, Category.class)
                        .build();

//adapter provided by UI

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(options) {
    @Override
    public MenuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        // layout called R.layout.menu_item

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.menu_item, parent, false);

        return new MenuViewHolder (view);
    }

    @Override
    protected void onBindViewHolder(MenuViewHolder holder, int position, Category model) {

  //do binding stuff
   holder.txtMenuName.setText(model.getName());
        Picasso.with(getBaseContext()).load(model.getImage()).into(holder.imageView);
        final Category clickItem = model;
        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {
                Toast.makeText(Home.this, "" + clickItem.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }




    }
};



 //set adapter
  recycler_menu.setAdapter(adapter);
 //start listening
  adapter.startListening();

}