public class ToDoAdapter extends RecyclerView.Adapter<ToDoAdapter.MyViewHolder> { Context context; ArrayList<ToDoItems> toDoItems; public ToDoAdapter(Context c, ArrayList<ToDoItems> p) { context = c; toDoItems = p; } @NonNull @Override public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.front_page_layout, viewGroup, false)); } @Override public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) { myViewHolder.itemTitle.setText(toDoItems.get(i).getItemTitle()); myViewHolder.itemSubject.setText(toDoItems.get(i).getItemSubject()); myViewHolder.itemDueDate.setText(toDoItems.get(i).getItemDueDate()); } @Override public int getItemCount() { return toDoItems.size(); } class MyViewHolder extends RecyclerView.ViewHolder { TextView itemTitle; TextView itemSubject; TextView itemDueDate; public MyViewHolder(@NonNull View itemView) { super(itemView); itemTitle = itemView.findViewById(R.id.itemTitle); itemSubject = itemView.findViewById(R.id.itemSubject); itemDueDate = itemView.findViewById(R.id.itemDueDate); } } }
我的Main.class文件
//Working with data mainPageRecyclerView = findViewById(R.id.mainPageRecyclerView); mainPageRecyclerView.setLayoutManager(new LinearLayoutManager(this)); list = new ArrayList<ToDoItems>(); addNewTask.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent a = new Intent(MainActivity.this,NewTaskAct.class); startActivity(a); } }); //get data from firebase dbReference = FirebaseDatabase.getInstance().getReference().child("to-do-list"); dbReference.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(@NonNull DataSnapshot dataSnapshot) { //set code to retrieve data and replace layout for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()) { ToDoItems p = dataSnapshot1.getValue(ToDoItems.class); list.add(p); } toDoAdapter = new ToDoAdapter(MainActivity.this, list); mainPageRecyclerView.setAdapter(toDoAdapter); toDoAdapter.notifyDataSetChanged(); }
我可以在firebase数据库中看到数据,但它不会从数据库中拉回信息,而我在android studio RUN中仅收到以下错误-> E / RecyclerView:未连接适配器;跳过布局我在学习的过程中编写了一个简单的应用程序,可以将信息写入firebase,但是当我的应用程序更改视图并需要从数据库调用数据时,我不断收到上述错误
我已经在youtube上浏览了其他堆栈溢出问题,但无法理解适用于我的项目的解决方案
正在跳过您的Recyclerview,因为在绘制布局时没有连接适配器。当前,您正在通过事件回调设置适配器,该事件回调在绘制布局并跳过Recyclerview之后仍将返回。您需要setAdapter() 直接在“活动”中调用,onCreate然后更新适配器的数据,以确保notifydatasetchanged在事件回调中进行调用。
setAdapter()
onCreate
notifydatasetchanged
在活动的onCreate内部:
// Create an empty adapter since you don't have initial data // (You may need to alter the constructor of your Adapter class // to keep it from trying to process empty/null data so it doesn't break) MyAdapter myAdapter = new MyAdapter(null); // Set the Recyclerview's Adapter so it isn't skipped on the layout pass myRecyclerView.setAdapter(myAdapter);
然后,在事件回调中:
// Update your Adapter with the new data using an update function you define in your Adapter myAdapter.updateData(myNewData); // Notify the Adapter that the data has changed myAdapter.notifyDataSetChanged();