填充recyclerview与SQLite数据库?数据库、recyclerview、SQLite

2023-09-07 00:06:42 作者:╰情場guàn犭

有什么不妥的典型的Andr​​oid光标。但我仍然困惑于如何实现它,有没有人在这里有一个例子或有没有人有另一种简单的解决方案?

我的Recyclerview适配器

 公共类WatchlistAdapter扩展RecyclerView.Adapter< WatchlistAdapter.MyViewHolder> {    私人LayoutInflater mInflater;    ArrayList的<游戏和GT;数据=新的ArrayList<>(); //一种方式,以便它永远不会等于null    私人诠释的位置;    公共WatchlistAdapter(上下文的背景下,ArrayList的<游戏和GT; MDATA){        mInflater = LayoutInflater.from(上下文);        数据= MDATA;    }    //所谓的每一次    @覆盖    公共MyViewHolder onCreateViewHolder(ViewGroup中的父母,INT viewType){        查看查看= mInflater.inflate(R.layout.watchlist_item,父母,假);        MyViewHolder持有人=新MyViewHolder(视图);        回到持有人;    }    @覆盖    公共无效onBindViewHolder(最终MyViewHolder持有人,最终诠释位置){        游戏currentGame = data.get(位置);        holder.gameTitle.setText(currentGame.get_name());        holder.releasedate.setText(currentGame.get_releaseDate());        holder.platform.setText(currentGame.get_platform());        //捕捉的位置被装载在上下文菜单中之前:        holder.itemView.setOnLongClickListener(新View.OnLongClickListener(){            @覆盖            公共布尔onLongClick(视图v){                的setPosition(holder.getPosition());                返回false;            }        });    }    公众诠释getItemPosition(){        返回的位置;    }    公共无效setPosition两种(INT位置){        this.position =位置;    }    @覆盖    公众诠释getItemCount(){        返回data.size();    }    静态类MyViewHolder扩展RecyclerView.ViewHolder实现View.OnCreateContextMenuListener {        私人TextView的gameTitle;        私人TextView的RELEASEDATE;        私人TextView的平台;        公共MyViewHolder(查看ItemView控件){            超(ItemView控件);            gameTitle =(TextView中)itemView.findViewById(R.id.gameTitle);            RELEASEDATE =(TextView中)itemView.findViewById(R.id.releasedateText);            平台=(TextView中)itemView.findViewById(R.id.platformText);            itemView.setOnCreateContextMenuListener(本);        }        @覆盖        公共无效onCreateContextMenu(文本菜单菜单视图V,ContextMenu.ContextMenuInfo menuInfo){            MenuInflater吹气=新MenuInflater(v.getContext());            inflater.inflate(R.menu.context_menu,菜单);        }    }} 
Javascript怎么连接SQLite数据库

解决方案

据pretty容易的事,我只是用游标检索在我的数据库中的所有数据,并将它们存储到一个黑色物体,然后一个数组名单。

 公开的ArrayList<游戏和GT; getAllData(){    ArrayList的<游戏和GT; allGames =新的ArrayList<>();    SQLiteDatabase分贝= getWritableDatabase();    的String [] =列{COLUMN_ID,COLUMN_NAME,COLUMN_PLATFORM,COLUMN_DATE};    光标光标= db.query(TABLE_GAMES,列,NULL,NULL,NULL,NULL,NULL);    如果(光标=空&放大器;!&放大器; cursor.moveToFirst()){        做{            //创建一个新的游戏对象,并从光标检索数据要被存储在该游戏对象            游戏游戏=新游戏();            //每个步骤是一个2部分的过程中,找到列的索引第一,使用发现的列的数据            //该指数最后我们设置游戏的空白对象包含我们的数据            game.set_id(cursor.getInt(cursor.getColumnIndex(COLUMN_ID)));            game.set_name(cursor.getString(cursor.getColumnIndex(列)));            game.set_platform(cursor.getString(cursor.getColumnIndex(COLUMN_PLATFORM)));            game.set_releaseDate(cursor.getString(cursor.getColumnIndex(COLUMN_DATE)));            allGames.add(游戏);        }而(cursor.moveToNext());    }    返回allGames;} 

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

解决方案

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