无法在 android 中查看 RecyclerView 上的数据

Could''t view the data on RecyclerView in android

我正在尝试查看 RecyclerView 上的数据。我从这两行中得到一个错误

 mAdapter = new Adapter(stu); in this line getting Error on Adapter is abstract

我是 android 的初学者。有人可以帮我解决这个问题吗?查看 RecyclerView 上的数据。我的数据库名称是 StudentDB 它有一个 table 名称 record.record table 我需要在 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;
    RecyclerView.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 List<student> stud;
        if (c.moveToFirst()) {
            do {
                student stu = new student();
                stu.id = c.getString(id);
                stu.name = c.getString(name);
                stu.age = c.getString(age);
                //you need to add the Student object stu not the ArrayList Object stud
                mAdapter = new Adapter(stu);
                recyclerView.setAdapter(mAdapter);
            } while (c.moveToNext());

        }
    }

}

学生Class

public class student {

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

RecyclerView setAdapter 之前你需要 setLayoutManager https://developer.android.com/guide/topics/ui/layout/recyclerview

public class MyActivity extends Activity {
private RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager layoutManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    recyclerView.setHasFixedSize(true);

    // use a linear layout manager
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);

    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();

    final List<Student> studentList;
    if (c.moveToFirst()) {
        do {
            Student stu = new Student();
            stu.id = c.getString(id);
            stu.name = c.getString(name);
            stu.age = c.getString(age);
            studentList.add(student);
        } while (c.moveToNext());

    }

    // specify an adapter (see also next example)
    mAdapter = new MyAdapter(studentList);
    recyclerView.setAdapter(mAdapter);
}
// ...
}


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Student> studentList;

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView textView;
    public MyViewHolder(TextView v) {
        super(v);
        textView = v;
    }
}

// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(List<Student> studentList) {
    this.studentList = studentList;
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    ...
    MyViewHolder vh = new MyViewHolder(v);
    return vh;
}

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    // - get element from your dataset at this position
    // - replace the contents of the view with that element
    holder.textView.setText(studentList.get(position).getName());

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
    return studentList.size();
}

}