使用SQLite数据库填充recyclerview?

时间:2015-08-19 17:06:22

标签: android sqlite android-recyclerview

典型的安卓游戏没什么问题。但是我仍然对如何实现它感到困惑,这里的任何人都有一个例子,或者有没有人有另一个更容易的解决方案?

我的Recyclerview适配器

    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 个答案:

答案 0 :(得分:0)

这很容易做到,我只是用光标检索数据库中的所有数据并将它们存储到黑色对象中,然后存储到数组列表中。

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