适配器是抽象的,无法在Android中的RecyclerView上实例化

时间:2019-03-05 05:38:43

标签: android android-recyclerview

我正在尝试在RecyclerView上查看数据。这两行出现错误。

 `mAdapter = new Adapter(stu);`

在此行中,获取适配器错误是抽象的 我是android的初学者。有人可以帮助我解决此问题。在RecyclerView上查看数据。我的数据库名称是StudentDB,它有一个表名record.record表数据我需要在RecyclerView上查看。

我在下面附上完整的代码

view.xml

<LinearLayout 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:id="@+id/rs1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view1">    
    <android.support.v7.widget.RecyclerView
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />    
</LinearLayout>

view.java

public class view1 extends AppCompatActivity {
private RecyclerView recyclerView;
private Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;

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

    SQLiteDatabase db = openOrCreateDatabase("StudentDB", Context.MODE_PRIVATE, null);
    final Cursor c = db.rawQuery("select * from record", null);
    int id = c.getColumnIndex("id");
    final int name = c.getColumnIndex("name");
    final int age = c.getColumnIndex("age");
    c.moveToFirst();

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

    final ArrayList<student> stud = new ArrayList<>();
    if (c.moveToFirst()) {
        do {
            student stu = new student();
            stu.id = c.getString(id);
            stu.name = c.getString(name);
            stu.age = c.getString(age);
            stud.add(stu);

        } while (c.moveToNext());

        //you need to add the Student object stu not the ArrayList Object stud
        if (stud != null && stud.size() > 0) {
            mAdapter = new Adapter(stud);
            recyclerView.setAdapter(mAdapter);
        }
    }
}

}

学生班

public class student {

    String id;
    String name;
    String age;
    String titles;
}

5 个答案:

答案 0 :(得分:3)

您正在将Student对象添加到adapter。您需要将学生列表添加到适配器。像这样更改代码

        final List<student> stud = new ArrayList();
        if (c.moveToFirst()) {
            do {
                student stu = new student();
                stu.id = c.getString(id);
                stu.name = c.getString(name);
                stu.age = c.getString(age);

                stud.add(stu); // add your object to list

            } while (c.moveToNext());

              mAdapter = new Adapter(stud);
              recyclerView.setAdapter(mAdapter);

答案 1 :(得分:0)

尝试这样设置适配器,

final List<student> stud  = new ArrayList();
    if (c.moveToFirst()) {
        do {
            student stu = new student();
            stu.id = c.getString(id);
            stu.name = c.getString(name);
            stu.age = c.getString(age);
            stud.add(stu); 
        } while (c.moveToNext());
          mAdapter = new Adapter(stud);
          layoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
    recyclerView.setLayoutManager(layoutManager);
          recyclerView.setAdapter(mAdapter);

还将布局更改为

<LinearLayout 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=".view1">    
<android.support.v7.widget.RecyclerView
android:id="@+id/rs1"
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteX="8dp"
    tools:layout_editor_absoluteY="8dp" />    

答案 2 :(得分:0)

您正在do while loop中设置适配器。并且需要重构代码,创建custom adaptor ....从活动view1类到它自己的类,进行数据库调用。并且您需要熟悉泛型和内部类才能使用自定义适配器。

答案 3 :(得分:0)

尝试

 final ArrayList<student> stud = new ArrayList<>();
        if (c.moveToFirst()) {
            do {
                student stu = new student();
                stu.id = c.getString(id);
                stu.name = c.getString(name);
                stu.age = c.getString(age);
                stud.add(stu);

           } while (c.moveToNext());

           //you need to add the Student object stu not the ArrayList Object stud
            if(stud!=null&&stud.size()>0){
               DummyAdapter mAdapter = new DummyAdapter(this,stud);
                recyclerView.setAdapter(mAdapter);
               }

像这样创建新的适配器类

public class DummyAdapter extends RecyclerView.Adapter<DummyAdapter.MyViewHolder> {
    private Context mContext;
    ArrayList<student> studentArraylist;

    public DummyAdapter(Context mContext,ArrayList<student> studentArraylist) {
        this.mContext = mContext;
        this.studentArraylist  = studemtArraylist;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    //add your layout design file here
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_packing_dummy, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    Student stud = studentArraylist.get(position);
  //perform your rest functionality
    holder.txt.settext(stud.name)

    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
      //access your layout ids here for example
       TextView txt;
        public MyViewHolder(View itemView) {
            super(itemView);
            txt = itemView.findViewById(R.id.txt);
        }
    }
}

答案 4 :(得分:0)

尝试一下

$middlewarePriority