在RecyclerView显示SQLite的数据 [英] Display SQLite data in RecyclerView

查看:1599
本文介绍了在RecyclerView显示SQLite的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,但我没有找到任何一步一步的指导,如何dislpay SQLite的数据RecyclerView。任何人都可以解释我怎样才能做到这一点?

我的 DataBaseAdapter:

 公共类DataBaseAdapter {

DataBaseHelper帮手;

公共DataBaseAdapter(上下文的背景下){
    辅助=新DataBaseHelper(上下文);
    }

众长insertData(字符串名称,字符串卡,字符串code){

    SQLiteDatabase DB = helper.getWritableDatabase();
    ContentValues​​ contentValues​​ =新ContentValues​​();
    contentValues​​.put(DataBaseHelper.NAME,姓名);
    contentValues​​.put(DataBaseHelper.CARD,卡);
    contentValues​​.put(DataBaseHelper code,code);
    长ID = db.insert(DataBaseHelper.TABLE_NAME,空,contentValues​​);
    返回ID;
}

公共字符串的getData(字符串名称){
    //选择_id,姓名,卡,code
    SQLiteDatabase DB = helper.getWritableDatabase();
    的String []列= {DataBaseHelper.NAME,DataBaseHelper.CARD,DataBaseHelper code};
    光标光标= db.query(DataBaseHelper.TABLE_NAME,列,DataBaseHelper.NAME +=+姓名+',NULL,NULL,NULL,NULL);
    StringBuffer的缓冲区=新的StringBuffer();
    而(cursor.moveToNext()){
        INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        INT index4 = cursor.getColumnIndex(DataBaseHelper code);

        字符串PersonName的= cursor.getString(索引2);
        串卡= cursor.getString(INDEX3);
        字符串code = cursor.getString(index4);
        buffer.append(姓名++卡++ code +\ N);

    }
    返回buffer.toString();
}

公共字符串getAllData(){
    //选择_id,姓名,卡,code
    SQLiteDatabase DB = helper.getWritableDatabase();
    的String []列= {DataBaseHelper.UID,DataBaseHelper.NAME,DataBaseHelper.CARD,DataBaseHelper code};
    光标光标= db.query(DataBaseHelper.TABLE_NAME,列,NULL,NULL,NULL,NULL,NULL);
    StringBuffer的缓冲区=新的StringBuffer();
    而(cursor.moveToNext()){
        INT索引1 = cursor.getColumnIndex(DataBaseHelper.UID);
        INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        INT index4 = cursor.getColumnIndex(DataBaseHelper code);

        INT CID = cursor.getInt(指数1);
        字符串名称= cursor.getString(索引2);
        串卡= cursor.getString(INDEX3);
        字符串code = cursor.getString(index4);
        buffer.append(CID ++姓名++卡++ code +\ N);
    }
    返回buffer.toString();

}

静态类DataBaseHelper扩展SQLiteOpenHelper {
    私有静态最后弦乐DATABASE_NAME =cardsdatabase;
    私有静态最后弦乐TABLE_NAME =CARDSTABLE;
    私有静态最后弦乐UID =_id;
    私有静态最后字符串名称=名称;
    私有静态最后弦乐卡=一卡通;
    私有静态最后弦乐code =code;
    私有静态最终诠释DATABASE_VERSION = 1;
    私有静态最后弦乐CREATE_TABLE =创建表
            + TABLE_NAME +(+ UID
            +整数主键自动增量,+姓名
            +文本不为空,+卡+转非空,+ code
            +文字NOT NULL);;
    私有静态最后弦乐DROP_TABLE =DROP TABLE IF EXISTS
            + TABLE_NAME;
    私人上下文的背景下;

    公共DataBaseHelper(上下文的背景下){

        超(背景下,DataBaseHelper.DATABASE_NAME,空,DataBaseHelper.DATABASE_VERSION);
        this.context =背景;
        吐司面包= Toast.makeText(背景下,构造被称为,Toast.LENGTH_LONG);
        toast.show();
    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        // CREATE TABLE CARDSTABLE(_id INTEGER PRIMARY KEY AUTOINCREMENT,名称VARCHAR(255));
        尝试 {
            db.execSQL(CREATE_TABLE);
            吐司面包= Toast.makeText(背景下,的onCreate称为,Toast.LENGTH_LONG);
            toast.show();

        }赶上(的SQLException E){
            吐司面包= Toast.makeText(背景下,+ E,Toast.LENGTH_SHORT);
            toast.show();
        }


    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){

        尝试 {
            吐司面包= Toast.makeText(背景下,onUpgrade称为,Toast.LENGTH_LONG);
            toast.show();
            db.execSQL(DROP_TABLE);
            的onCreate(DB);
        }赶上(的SQLException E){
            吐司面包= Toast.makeText(背景下,+ E,Toast.LENGTH_LONG);
            toast.show();
        }

    }
}
 

您的答案将是我真的有帮助,谢谢

编辑:

类公司Databeans:

 公共类{公司Databeans
//私有变量
INT _id;
字符串_name;
字符串_card_value;
字符串_card_ code;

//空的构造
公众公司Databeans(){

}
//构造
公众公司Databeans(INT ID,字符串名称,字符串_card_value,字符串_card_ code){
    this._id = ID;
    this._name =名称;
    this._card_value = _card_value;
    this._card_ code = _card_ code;
}

//构造
公众公司Databeans(字符串名称,字符串_card_value,字符串_card_ code){
    this._name =名称;
    this._card_value = _card_value;
    this._card_ code = _card_ code;
}
//获取ID
公众诠释的getID(){
    返回this._id;
}

//设置ID
公共无效SETID(INT ID){
    this._id = ID;
}

//获取名称
公共字符串的getName(){
    返回this._name;
}

//设置名称
公共无效setname可以(字符串名称){
    this._name =名称;
}

//获取CardValue
公共字符串getCardValue(){
    返回this._card_value;
}

//设置CardValue
公共无效setCardValue(字符串_card_value){
    this._card_value = _card_value;
}
//获取卡code
公共字符串getCard code(){
    返回this._card_ code;
}

//设置卡code
公共无效setCard code(字符串_card_ code){
    this._card_ code = _card_ code;
}
 

}

这些行我添加到我的 DataBaseAdapter

 公众公司Databeans getDat(字符串名称){
        豆公司Databeans = NULL;
        SQLiteDatabase DB = this.getReadableDatabase();
        的String []列= {DataBaseHelper.NAME,DataBaseHelper.CARD,DataBaseHelper code};
        光标光标= db.query(DataBaseHelper.TABLE_NAME,列,DataBaseHelper.NAME +=+姓名+',NULL,NULL,NULL,NULL);
        如果(cursor.moveToFirst()){
            INT指数= cursor.getColumnIndex(DataBaseHelper.UID);
            INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
            INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
            INT index4 = cursor.getColumnIndex(DataBaseHelper code);
            INT ID = cursor.getInt(指数);
            字符串PersonName的= cursor.getString(索引2);
            串卡= cursor.getString(INDEX3);
            字符串code = cursor.getString(index4);
            豆=新公司Databeans(ID,姓名,卡号,code);
        }
        返回豆;
    }
    公开名单<  - 公司Databeans GT; gelAllDat(){
        名单<  - 公司Databeans GT;名单=新的ArrayList<>();
        SQLiteDatabase DB = this.getReadableDatabase();
        的String []列= {DataBaseHelper.NAME,DataBaseHelper.CARD,DataBaseHelper code};
        光标光标= db.query(DataBaseHelper.TABLE_NAME,列,DataBaseHelper.NAME,NULL,NULL,NULL,NULL);
        而(cursor.moveToNext()){
            INT指数= cursor.getColumnIndex(DataBaseHelper.UID);
            INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
            INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
            INT index4 = cursor.getColumnIndex(DataBaseHelper code);
            INT CID = cursor.getInt(指数);
            字符串名称= cursor.getString(索引2);
            串卡= cursor.getString(INDEX3);
            字符串code = cursor.getString(index4);
            豆公司Databeans =新公司Databeans(CID,姓名,卡号,code);
            list.add(豆);
        }
        返回列表;

    }
 

和最后一个问题?我需要改变我的插入数据 DataBaseAdapter

编辑2:

我说这code我DataBaseAdapter:

 公开名单<  - 公司Databeans GT; getAllCards(){
    名单<  - 公司Databeans GT;名单=新的ArrayList<>();
    查询字符串=SELECT * FROM+ DataBaseHelper.TABLE_NAME;
    SQLiteDatabase DB = helper.getReadableDatabase();
    光标光标= db.rawQuery(查询,NULL);
    而(cursor.moveToNext()){
        // INT指数= cursor.getColumnIndex(DataBaseHelper.UID);
        INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        INT index4 = cursor.getColumnIndex(DataBaseHelper code);
        // INT CID = cursor.getInt(指数);
        字符串名称= cursor.getString(索引2);
        串卡= cursor.getString(INDEX3);
        字符串code = cursor.getString(index4);
        豆公司Databeans =新公司Databeans(姓名,卡号,code);
        list.add(豆);
    }

    返回列表;

}

公众公司Databeans的getData(字符串名称){
    //选择_id,姓名,卡,code
    SQLiteDatabase DB = helper.getReadableDatabase();
    的String []列= {DataBaseHelper.NAME,DataBaseHelper.CARD,DataBaseHelper code};
    豆公司Databeans = NULL;
    光标光标= db.query(DataBaseHelper.TABLE_NAME,列,DataBaseHelper.NAME +=+姓名+',NULL,NULL,NULL,NULL);
    StringBuffer的缓冲区=新的StringBuffer();
    而(cursor.moveToNext()){
        INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        INT index4 = cursor.getColumnIndex(DataBaseHelper code);

        字符串PersonName的= cursor.getString(索引2);
        串卡= cursor.getString(INDEX3);
        字符串code = cursor.getString(index4);
        //buffer.append(name ++卡++ code +\ N);
        豆=新公司Databeans(姓名,卡号,code);

    }
    返回豆;
}
 

解决方案

您可以用豆开始,以遏制和型号的相关信息,并使其更容易实现。

 公共类{公司Databeans
    保护INT ID;
    受保护的字符串名称;
    保护串证;
    保护字符串code;
    //二传手,getter和构造
    ...
}
 

随着创建公司Databeans,你可以改变你的方法或公司Databeans的列表,返回类型,填补每一种方法里面,而不是返回一个字符串的所有领域。

 公众公司Databeans的getData(字符串名称){
    ...
    豆公司Databeans = NULL;
    如果(cursor.moveToFirst()){
        INT指数= cursor.getColumnIndex(DataBaseHelper.UID);
        INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        INT index4 = cursor.getColumnIndex(DataBaseHelper code);
        INT ID = cursor.getInt(指数);
        字符串PersonName的= cursor.getString(索引2);
        串卡= cursor.getString(INDEX3);
        字符串code = cursor.getString(index4);
        豆=新公司Databeans(ID,姓名,卡号,code);
    }
    返回豆;
}

公开名单<  - 公司Databeans GT; getAllData(){
    名单<  - 公司Databeans GT;名单=新的ArrayList<>();
    ...
    而(cursor.moveToNext()){
        INT指数= cursor.getColumnIndex(DataBaseHelper.UID);
        INT索引2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        INT INDEX3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        INT index4 = cursor.getColumnIndex(DataBaseHelper code);
        INT CID = cursor.getInt(指数);
        字符串名称= cursor.getString(索引2);
        串卡= cursor.getString(INDEX3);
        字符串code = cursor.getString(index4);
        豆公司Databeans =新公司Databeans(CID,姓名,卡号,code);
        list.add(豆);
    }
    返回列表;
}
 

现在,当你打电话给你的方法,你有一个目标公司Databeans(S),现在你需要写你的适配器,显示在RecyclerView的信息。

首先需要在你的活动链接,并设置RecyclerView。

  mRecyclerView =(RecyclerView)findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(真正的);
mRecyclerView.setLayoutManager(新LinearLayoutManager(本));
mRecyclerView.setItemAnimator(新DefaultItemAnimator());
mRecyclerView.setAdapter(新DataBeanAdapter(dbAdapter.getAllData(),R.layout.item));
 

在您需要创建DataBeanAdapter和<一href="https://developer.android.com/reference/android/support/v7/widget/RecyclerView.ViewHolder.html"相对=nofollow> ViewHolder 。

 公共类DataBeanAdapter扩展RecyclerView.Adapter&LT; D​​ataBeanAdapter.ViewHolder&GT; {
    私人列表&LT;  - 公司Databeans GT;项目;
    私人诠释itemLayout;

    公共DataBeanAdapter(名单&LT;  - 公司Databeans GT;的项目,诠释itemLayout){
        this.items =项目;
        this.itemLayout = itemLayout;
    }

    @覆盖
    公共ViewHolder onCreateViewHolder(ViewGroup中的父母,INT viewType){
        视图V = LayoutInflater.from(parent.getContext())膨胀(itemLayout,父母,假)。
        返回新ViewHolder(V);
    }

    @覆盖
    公共无效onBindViewHolder(ViewHolder持有人,INT位置){
        项目公司Databeans = items.get(位置);
        holder.name.setText(item.getName());
        holder.card.setText(item.getCard());
        //所有的事情你要显示的项
    }

    @覆盖
    公众诠释getItemCount(){
        返回items.size();
    }

    公共静态类ViewHolder扩展RecyclerView.ViewHolder {
        公共TextView的名称;
        公众的TextView卡;

        公共ViewHolder(查看ItemView控件){
            超(ItemView控件);
            NAME =(TextView中)itemView.findViewById(R.id.name);
            卡=(TextView中)itemView.findViewById(R.id.card);
        }
    }
}
 

本ID的,布局和ViewHolder取决于谁,你每个项目在RecyclerView要显示的属性。

i search a lot, but i didnt find any step-by-step guide how to dislpay SQLite data in RecyclerView. Can anyone explain me how can i do this?

My DataBaseAdapter :

public class DataBaseAdapter {

DataBaseHelper helper;

public DataBaseAdapter(Context context) {
    helper = new DataBaseHelper(context);
    }

public long insertData(String name, String card, String code) {

    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(DataBaseHelper.NAME, name);
    contentValues.put(DataBaseHelper.CARD, card);
    contentValues.put(DataBaseHelper.CODE, code);
    long id = db.insert(DataBaseHelper.TABLE_NAME, null, contentValues);
    return id;
}

public String getData(String name){
    //select _id,Name,Card,Code
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {DataBaseHelper.NAME, DataBaseHelper.CARD, DataBaseHelper.CODE};
    Cursor cursor = db.query(DataBaseHelper.TABLE_NAME, columns,DataBaseHelper.NAME+" = '"+name+"'", null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.moveToNext()) {
        int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);

        String personName = cursor.getString(index2);
        String card = cursor.getString(index3);
        String code = cursor.getString(index4);
        buffer.append(name + " " + card + " " + code + "\n");

    }
    return buffer.toString();
}

public String getAllData() {
    //select _id,Name,Card,Code
    SQLiteDatabase db = helper.getWritableDatabase();
    String[] columns = {DataBaseHelper.UID, DataBaseHelper.NAME, DataBaseHelper.CARD, DataBaseHelper.CODE};
    Cursor cursor = db.query(DataBaseHelper.TABLE_NAME, columns, null, null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.moveToNext()) {
        int index1 = cursor.getColumnIndex(DataBaseHelper.UID);
        int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);

        int cid = cursor.getInt(index1);
        String name = cursor.getString(index2);
        String card = cursor.getString(index3);
        String code = cursor.getString(index4);
        buffer.append(cid + " " + name + " " + card + " " + code + "\n");
    }
    return buffer.toString();

}

static class DataBaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "cardsdatabase";
    private static final String TABLE_NAME = "CARDSTABLE";
    private static final String UID = "_id";
    private static final String NAME = "Name";
    private static final String CARD = "Card";
    private static final String CODE = "Code";
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_TABLE = "create table "
            + TABLE_NAME + " (" + UID
            + " integer primary key autoincrement, " + NAME
            + " text not null, " + CARD + " ext not null, " + CODE
            + " text not null);";
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS "
            + TABLE_NAME;
    private Context context;

    public DataBaseHelper(Context context) {

        super(context, DataBaseHelper.DATABASE_NAME, null, DataBaseHelper.DATABASE_VERSION);
        this.context = context;
        Toast toast = Toast.makeText(context, "constructor called", Toast.LENGTH_LONG);
        toast.show();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //CREATE TABLE CARDSTABLE(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(255));
        try {
            db.execSQL(CREATE_TABLE);
            Toast toast = Toast.makeText(context, "onCreate called", Toast.LENGTH_LONG);
            toast.show();

        } catch (SQLException e) {
            Toast toast = Toast.makeText(context, "" + e, Toast.LENGTH_SHORT);
            toast.show();
        }


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        try {
            Toast toast = Toast.makeText(context, "onUpgrade called", Toast.LENGTH_LONG);
            toast.show();
            db.execSQL(DROP_TABLE);
            onCreate(db);
        } catch (SQLException e) {
            Toast toast = Toast.makeText(context, "" + e, Toast.LENGTH_LONG);
            toast.show();
        }

    }
}

Your answers will be realy helpful for me, thanks

EDIT:

DataBean class :

public class DataBean {
//private variables
int _id;
String _name;
String _card_value;
String _card_code;

// Empty constructor
public DataBean(){

}
// constructor
public DataBean(int id, String name, String _card_value, String _card_code){
    this._id = id;
    this._name = name;
    this._card_value = _card_value;
    this._card_code = _card_code;
}

// constructor
public DataBean(String name, String _card_value, String _card_code){
    this._name = name;
    this._card_value = _card_value;
    this._card_code = _card_code;
}
// getting ID
public int getID(){
    return this._id;
}

// setting id
public void setID(int id){
    this._id = id;
}

// getting name
public String getName(){
    return this._name;
}

// setting name
public void setName(String name){
    this._name = name;
}

// getting CardValue
public String getCardValue(){
    return this._card_value;
}

// setting CardValue
public void setCardValue(String _card_value){
    this._card_value = _card_value;
}
// getting CardCode
public String getCardCode(){
    return this._card_code;
}

// setting CardCode
public void setCardCode(String _card_code){
    this._card_code = _card_code;
}

}

These lines i add to my DataBaseAdapter:

public DataBean getDat(String name){
        DataBean bean=null;
        SQLiteDatabase db=this.getReadableDatabase();
        String[] columns = {DataBaseHelper.NAME, DataBaseHelper.CARD, DataBaseHelper.CODE};
        Cursor cursor = db.query(DataBaseHelper.TABLE_NAME, columns,DataBaseHelper.NAME+" = '"+name+"'", null, null, null, null);
        if (cursor.moveToFirst()) {
            int index = cursor.getColumnIndex(DataBaseHelper.UID);
            int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
            int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
            int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);
            int id = cursor.getInt(index);
            String personName = cursor.getString(index2);
            String card = cursor.getString(index3);
            String code = cursor.getString(index4);
            bean = new DataBean(id, name, card, code);
        }
        return bean;
    }
    public List<DataBean> gelAllDat(){
        List<DataBean> list = new ArrayList<>();
        SQLiteDatabase db=this.getReadableDatabase();
        String[] columns = {DataBaseHelper.NAME, DataBaseHelper.CARD, DataBaseHelper.CODE};
        Cursor cursor = db.query(DataBaseHelper.TABLE_NAME, columns,DataBaseHelper.NAME, null, null, null, null);
        while (cursor.moveToNext()) {
            int index = cursor.getColumnIndex(DataBaseHelper.UID);
            int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
            int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
            int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);
            int cid = cursor.getInt(index);
            String name = cursor.getString(index2);
            String card = cursor.getString(index3);
            String code = cursor.getString(index4);
            DataBean bean = new DataBean(cid, name, card, code);
            list.add(bean);
        }
        return list;

    }

And last question? i need to change my insert data in DataBaseAdapter?

EDIT 2:

I added this code to my DataBaseAdapter:

public List<DataBean> getAllCards(){
    List<DataBean> list=new ArrayList<>();
    String query = "SELECT  * FROM " + DataBaseHelper.TABLE_NAME;
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    while (cursor.moveToNext()) {
        //int index = cursor.getColumnIndex(DataBaseHelper.UID);
        int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);
        //int cid = cursor.getInt(index);
        String name = cursor.getString(index2);
        String card = cursor.getString(index3);
        String code = cursor.getString(index4);
        DataBean bean = new DataBean(name, card, code);
        list.add(bean);
    }

    return list;

}

public DataBean getData(String name) {
    //select _id,Name,Card,Code
    SQLiteDatabase db = helper.getReadableDatabase();
    String[] columns = {DataBaseHelper.NAME, DataBaseHelper.CARD, DataBaseHelper.CODE};
    DataBean bean=null;
    Cursor cursor = db.query(DataBaseHelper.TABLE_NAME, columns, DataBaseHelper.NAME + " = '" + name + "'", null, null, null, null);
    StringBuffer buffer = new StringBuffer();
    while (cursor.moveToNext()) {
        int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);

        String personName = cursor.getString(index2);
        String card = cursor.getString(index3);
        String code = cursor.getString(index4);
        //buffer.append(name + " " + card + " " + code + "\n");
        bean = new DataBean( name,card, code);

    }
    return bean;
}

解决方案

You can start with a Bean to contain and model the information and make it more easy to implement.

public class DataBean{
    protected int id;
    protected String name;
    protected String card;
    protected String code;
    //Setter, Getters and constructor
    ...
}

With the DataBean created, you can change the return types of your methods to DataBean or a List and filled inside each method instead of return a String with all the fields.

public DataBean getData(String name){
    ...
    DataBean bean = null;
    if (cursor.moveToFirst()) {
        int index = cursor.getColumnIndex(DataBaseHelper.UID);
        int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);
        int id = cursor.getInt(index);
        String personName = cursor.getString(index2);
        String card = cursor.getString(index3);
        String code = cursor.getString(index4);
        bean = new DataBean(id, name, card, code);    
    }
    return bean;
}

public List<DataBean> getAllData() {
    List<DataBean> list = new ArrayList<>();
    ...
    while (cursor.moveToNext()) {
        int index = cursor.getColumnIndex(DataBaseHelper.UID);
        int index2 = cursor.getColumnIndex(DataBaseHelper.NAME);
        int index3 = cursor.getColumnIndex(DataBaseHelper.CARD);
        int index4 = cursor.getColumnIndex(DataBaseHelper.CODE);
        int cid = cursor.getInt(index);
        String name = cursor.getString(index2);
        String card = cursor.getString(index3);
        String code = cursor.getString(index4);
        DataBean bean = new DataBean(cid, name, card, code);
        list.add(bean);
    }
    return list;
}

Now when you call your methods you have a DataBean object(s), now you need write your Adapter to show the information in the RecyclerView.

First need link and setup the RecyclerView in your Activity.

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setAdapter(new DataBeanAdapter(dbAdapter.getAllData(), R.layout.item));

After you need create the DataBeanAdapter and the ViewHolder.

public class DataBeanAdapter extends RecyclerView.Adapter<DataBeanAdapter.ViewHolder>{
    private List<DataBean> items;
    private int itemLayout;

    public DataBeanAdapter(List<DataBean> items, int itemLayout){
        this.items = items;
        this.itemLayout = itemLayout;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DataBean item = items.get(position);
        holder.name.setText(item.getName());
        holder.card.setText(item.getCard());
        //All the thing you gonna show in the item
    }

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

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        public TextView card;

        public ViewHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            card = (TextView) itemView.findViewById(R.id.card);
        }
    }
}

The id's, layout and the attributes of the ViewHolder depending who you gonna show per item in the RecyclerView.

这篇关于在RecyclerView显示SQLite的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆