E / RecyclerView:未连接适配器;跳过布局。无数据显示

问题描述 投票:0回答:1

我正在尝试从Firebase获取数据,并通过适配器将其放入回收器视图。但是,没有数据正在显示。我在主要活动中的代码:

 DatabaseReference databaseReference;
RecyclerView recyclerView;
ArrayList<Menu> menuList;
EditMenuAdapter editMenuAdapter;

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

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    menuList = new ArrayList<Menu>();

    databaseReference = FirebaseDatabase.getInstance().getReference().child("menu");

    databaseReference.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {

                Menu menu = dataSnapshot1.getValue(Menu.class);
                menuList.add(menu);

            }

            editMenuAdapter = new EditMenuAdapter(EditMenuActivity.this, menuList);
            recyclerView.setAdapter(editMenuAdapter);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

            Toast.makeText(EditMenuActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();

        }
    });

我的适配器类:

public class EditMenuAdapter extends RecyclerView.Adapter<EditMenuAdapter.MyViewHolder> {

Context context;
ArrayList<Menu> menuArrayList;

public EditMenuAdapter(Context c, ArrayList<Menu> menus) {

    context = c;
    menuArrayList = menus;

}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.menu_list_layout, parent, false));
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    holder.dishNameTextView.setText(menuArrayList.get(position).getDishName());
    holder.dishPriceTextView.setText(menuArrayList.get(position).getDishPrice());

}

@Override
public int getItemCount() {
    return menuArrayList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder {

    TextView dishNameTextView;
    TextView dishPriceTextView;
    Switch dishActiveSwitch;

    public MyViewHolder(View view) {

        super(view);

        dishNameTextView = (TextView) view.findViewById(R.id.dishNameTextView);
        dishPriceTextView = (TextView) view.findViewById(R.id.dishPriceTextView);
        dishActiveSwitch = (Switch) view.findViewById(R.id.dishActiveSwitch);

    }

}

}

布局资源文件是:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".EditMenuActivity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="397dp"
    android:layout_height="599dp"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/doneButton"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="15dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="20dp"
    android:text="Done"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/recyclerView" />

日志显示:E / RecyclerView:未连接适配器;跳过布局。没有数据显示。

仅在屏幕上显示开关。请帮助!

android firebase firebase-realtime-database android-recyclerview recycler-adapter
1个回答
0
投票

onCreate中创建适配器实例并将其附加到RV:


protected void onCreate(Bundle savedInstanceState) {
  ...
  editMenuAdapter = new EditMenuAdapter(this, new ArrayList<>());
  recyclerView.setAdapter(adapter);
  ...
}

响应Firebase onDataChange

public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  ...
  editMenuAdapter.setItems(menuList);
  runOnUiThread(() -> editMenuAdapter.notifyDataSetChanged());
}
© www.soinside.com 2019 - 2024. All rights reserved.