Не удалось просмотреть данные о RecyclerView в Android - PullRequest
0 голосов
/ 05 марта 2019

Я пытаюсь просмотреть данные на RecyclerView.Я получил сообщение об ошибке из этих двух строк

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

Я новичок в 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;
    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());

        }
    }

}

Студенческий класс

public class student {

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

1 Ответ

0 голосов
/ 05 марта 2019

Вам нужно setLayoutManager до setAdapter для RecyclerView 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();
}

}

...