S
S
suda9112016-02-14 23:54:58
Java
suda911, 2016-02-14 23:54:58

How to properly use RecyclerView.Adapter in Android Studio?

I want to use RecycleView in my application. I read popular manuals and made an almost exact copy in my application, but it still does not work. The data is taken from the Cursor to the database. From the cursor I pass them to my List items=new ArrayList<>(); But on startup it still gives an error. Here is a listing of the adapter and the main window

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {


    List<MyItem> items;
    RVAdapter(List<MyItem> items){
        this.items = items;
    }

    @Override
    public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
        PersonViewHolder pvh = new PersonViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(RVAdapter.PersonViewHolder personViewHolder, int i) {
        personViewHolder.personName.setText(items.get(i).name);
        personViewHolder.personAge.setText(items.get(i).cell);
    }

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

        public static class PersonViewHolder extends RecyclerView.ViewHolder {
            CardView cv;
            TextView personName;
            TextView personAge;
            PersonViewHolder(View itemView) {
                super(itemView);
                cv = (CardView)itemView.findViewById(R.id.cv);
                personName = (TextView)itemView.findViewById(R.id.person_name);
                personAge = (TextView)itemView.findViewById(R.id.person_cell);
            }
        }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }
}

This is from the main
List<MyItem> items=new ArrayList<>();

        final Cursor cursor = mSqLiteDatabase.query("Kroliki", null, null, null, null, null, null ) ;
        if(cursor.moveToFirst()){
            do{
                String name = cursor.getString(cursor.getColumnIndex(DBHelper.IDENT));
                int cell = cursor.getInt(cursor.getColumnIndex(DBHelper.Cell));
                items.add(new MyItem(name, cell));
            }while (cursor.moveToNext());
        }
        cursor.close();

        RecyclerView rv = (RecyclerView)findViewById(R.id.rv);
        rv.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(this);
        rv.setLayoutManager(llm);


        RVAdapter adapter = new RVAdapter(items);
        rv.setAdapter(adapter);

I can not understand. Looks like the data is there. but it still throws an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
razer89, 2016-02-15
@suda911

Replace line
to the line:

personViewHolder.personAge.setText(String.valueOf(items.get(i).cell));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question