1

There's nothing wrong with the typical android cursor. But I'm still confused on how to implement it, does anyone here have an example or does anyone have another easier solution?

My Recyclerview Adapter

    public class WatchlistAdapter extends RecyclerView.Adapter<WatchlistAdapter.MyViewHolder> {

    private LayoutInflater mInflater ;
    ArrayList<Games> data = new ArrayList<>(); //a way so it never equals null
    private int position;

    public WatchlistAdapter(Context context, ArrayList<Games> mData){
        mInflater = LayoutInflater.from(context);
        data = mData;
    }

    //called every time
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.watchlist_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        Games currentGame= data.get(position);
        holder.gameTitle.setText(currentGame.get_name());
        holder.releasedate.setText(currentGame.get_releaseDate());
        holder.platform.setText(currentGame.get_platform());

        //to capture the position before the context menu is loaded:
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                setPosition(holder.getPosition());
                return false;
            }
        });
    }

    public int getItemPosition(){
        return position;
    }

    public void setPosition(int position){
        this.position = position;
    }


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

    static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnCreateContextMenuListener{
        private TextView gameTitle;
        private TextView releasedate;
        private TextView platform;
        public MyViewHolder(View itemView) {
            super(itemView);
            gameTitle = (TextView) itemView.findViewById(R.id.gameTitle);
            releasedate = (TextView) itemView.findViewById(R.id.releasedateText);
            platform = (TextView) itemView.findViewById(R.id.platformText);
            itemView.setOnCreateContextMenuListener(this);
        }

        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuInflater inflater = new MenuInflater(v.getContext());
            inflater.inflate(R.menu.context_menu, menu);
        }
    }
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
James A
  • 135
  • 1
  • 4
  • 17
  • What do you mean easier? And your sample does not interact with `SQLite` anyhow. – eleven Aug 19 '15 at 17:10
  • This thread may shed some light ..http://stackoverflow.com/questions/26312301/is-it-possible-to-make-cursoradapter-be-set-in-recycleview-just-like-listview – Mark Aug 19 '15 at 17:25
  • see https://gist.github.com/Shywim/127f207e7248fe48400b – pskink Aug 19 '15 at 17:58
  • 1
    I am assuming you are not aware of other alternative ways to populate data from database inside RecyclerView which is why your question targets SQLite, if thats the case, check this alternate way of populating a RecyclerView from database with Realm http://stackoverflow.com/questions/26517855/using-the-recyclerview-with-a-database/33568015#33568015 – PirateApp Nov 08 '15 at 04:55

1 Answers1

0

It was pretty easy to do, I just used a cursor to retrieve all the data in my database and stored them into a black object then an array list.

 public ArrayList<Games> getAllData() {
    ArrayList<Games> allGames = new ArrayList<>();
    SQLiteDatabase db = getWritableDatabase();
    String[] columns = {COLUMN_ID, COLUMN_NAME, COLUMN_PLATFORM, COLUMN_DATE};
    Cursor cursor = db.query(TABLE_GAMES, columns, null, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        do {
            //create a new Games object and retrieve the data from the cursor to be stored in this Games object
            Games game = new Games();
            //each step is a 2 part process, find the index of the column first, find the data of that column using
            //that index and finally set our blank Games object to contain our data
            game.set_id(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));
            game.set_name(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
            game.set_platform(cursor.getString(cursor.getColumnIndex(COLUMN_PLATFORM)));
            game.set_releaseDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));

            allGames.add(game);
        } while (cursor.moveToNext());
    }
    return allGames;
}
James A
  • 135
  • 1
  • 4
  • 17
  • 9
    **no, no, no**! why do you people iterate over the `Cursor` and duplicate its data storing the whole data set in the array? just use the adapter which is designed to work with a `Cursor` interface! the same applies to `ListView` where folks use `ArrayAdapter` wherndealing with sqlite db (instead of `SimpleCursorAdapter`) – pskink Aug 19 '15 at 19:33
  • And how do we use it exactly? – James A Aug 19 '15 at 19:40
  • 2
    just extend `CursorRecyclerAdapter` from my link above and implement one abstract method: `onBindViewHolderCursor`, that's all – pskink Aug 19 '15 at 19:45
  • 1
    Please watch your language (and your attitude). We expect all users here to adhere to our ["Be Nice" policy](https://stackoverflow.com/help/be-nice). – Cody Gray - on strike Sep 30 '17 at 16:09