android - Populate recyclerview with a SQLite database? -
there's nothing wrong typical android cursor. i'm still confused on how implement it, here have example or have easier solution?
my recyclerview adapter
public class watchlistadapter extends recyclerview.adapter<watchlistadapter.myviewholder> { private layoutinflater minflater ; arraylist<games> data = new arraylist<>(); //a way 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 position before context menu 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); } } }
it pretty easy do, used cursor retrieve data in database , stored them black object 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()) { { //create new games object , retrieve data cursor stored in games object games game = new games(); //each step 2 part process, find index of column first, find data of column using //that index , set our blank games object 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; }
Comments
Post a Comment