Populate recyclerview with a SQLite database?

时间秒杀一切 提交于 2019-12-19 04:56:55

问题


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

回答1:


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;
}


来源:https://stackoverflow.com/questions/32101607/populate-recyclerview-with-a-sqlite-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!