错误:未连接适配器,在使用recyclerview和firebase时将布局跳过到类中

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

我已经尝试了很多解决此问题,但未能解决。它说我的适配器没有连接。但是当我在onCreate中声明一个适配器并运行时,它显示以下代码的空白活动:

mainactivity.java

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    DatabaseReference collegereference;
    private CollegeAdapter collegeAdapter;
    List<College> collegelist = new List<College>(); //lot of functions under this which i didnt put in this question

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recycler);
        collegereference = FirebaseDatabase.getInstance().getReference().child("colleges");         
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        collegeAdapter = new CollegeAdapter(MainActivity.this,collegelist);
        //initialised adapter after getting 'adapter not attached, skipping activity'
        //i am not getting this error anymore but when i run, a blank acitvity pops up
        recyclerView.setAdapter(collegeAdapter);
        collegereference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
                {
                    collegelist.add(dataSnapshot1.getValue(College.class));
                }
                recyclerView.setAdapter(collegeAdapter);
                collegeAdapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(MainActivity.this, "unsuccessful", Toast.LENGTH_SHORT).show();
            }
        });

    }

}

这是我的适配器类:

CollegeAdapter.java

public class CollegeAdapter extends RecyclerView.Adapter<CollegeAdapter.CollegeViewHolder>  {

    Context c;
    private List<College> collegelist;

    public CollegeAdapter(Context c, List<College> collegelist)
    {
        LayoutInflater manager = LayoutInflater.from(c);
        this.c = c;
        this.collegelist = collegelist;
    }

    @NonNull
    @Override
    public CollegeAdapter.CollegeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
    {
        return new CollegeViewHolder(LayoutInflater.from(c).inflate(R.layout.collegecardview,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull CollegeAdapter.CollegeViewHolder holder, int position) 
    {
        holder.collegename.setText(collegelist.get(position).getName());
        holder.rating.setText(collegelist.get(position).getRating());
    }

    @Override
    public int getItemCount() 
    {
        return collegelist.size(); //points to null, when there was no adapter initialised in onCreate
    }

    public class CollegeViewHolder extends RecyclerView.ViewHolder 
    {
        TextView collegename,rating;

        public CollegeViewHolder(@NonNull View itemView) 
        {
            super(itemView);
            collegename = itemView.findViewById(R.id.collegename);
            rating = itemView.findViewById(R.id.rating);
        }
    }
}

我的Cardview布局是:

mainactivity.xml

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="LinearLayoutManager"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

我的Cardview活动:collegecardview.xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:orientation="horizontal">
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="College Name"
        android:textSize="15sp"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/collegename"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Rating"
        android:textStyle="bold"
        android:textSize="15sp"/>
    <TextView
        android:id="@+id/rating"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/collegeimage"/>
</LinearLayout>

现在我收到此错误:

04-28 06:27:37.832 15830-15830/? I/art: Not late-enabling -Xcheck:jni (already on)
04-28 06:27:37.896 15830-15830/? W/System: ClassLoader referenced unknown path: /data/app/com.example.collegefirebase-2/lib/x86_64
04-28 06:27:37.917 15830-15830/com.example.collegefirebase I/FirebaseInitProvider: FirebaseApp initialization successful
04-28 06:27:37.940 15830-15830/com.example.collegefirebase W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-28 06:27:38.006 15830-15830/com.example.collegefirebase I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
04-28 06:27:38.006 15830-15830/com.example.collegefirebase I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
04-28 06:27:38.239 15830-15853/com.example.collegefirebase D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-28 06:27:38.335 15830-15853/com.example.collegefirebase I/OpenGLRenderer: Initialized EGL, version 1.4
04-28 06:27:38.339 15830-15853/com.example.collegefirebase W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
04-28 06:27:38.350 15830-15853/com.example.collegefirebase D/EGL_emulation: eglCreateContext: 0x7fc29c10a6e0: maj 2 min 0 rcv 2
04-28 06:27:38.366 15830-15853/com.example.collegefirebase D/EGL_emulation: eglMakeCurrent: 0x7fc29c10a6e0: ver 2 0 (tinfo 
0x7fc29ca05a80)
04-28 06:27:38.408 15830-15853/com.example.collegefirebase D/EGL_emulation: eglMakeCurrent: 0x7fc29c10a6e0: ver 2 0 (tinfo 0x7fc29ca05a80)

预先感谢。

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

首先,您以错误的方式创建了列表,这就是为什么您看到很多功能的原因:

代替此:

List<College> collegelist = new List<College>();

执行此操作:

List<College> collegelist = new ArrayList<College>();

并在adapter方法内添加onDataChange()初始化:

collegereference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren())
        {
            collegelist.add(dataSnapshot1.getValue(College.class));
        }

        //create adapter here

        collegeAdapter = new CollegeAdapter(MainActivity.this,collegelist);

        //set the adapter

        recyclerView.setAdapter(collegeAdapter);

    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Toast.makeText(MainActivity.this, "unsuccessful", Toast.LENGTH_SHORT).show();
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.