从Sqlite数据显示并在Android的recyclerview中显示数据

嗨,下面的代码是我第一次从APi调用,下一次要显示sqlite存储的数据,但是下一次不显示任何数据。

SQLiteDatabaseHandler.java:

public class SQLiteDatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "AccountDB";
    private static final String TABLE_NAME = "Accounts";
    private static final String KEY_ACCOUNTNAME = "account_name";
    private static final String KEY_OWNERNSHIP = "ownership";
    private static final String KEY_FACILITY = "facility";
    private static final String KEY_STREET = "street";
    private static final String KEY_CITY="city";
    private static final String KEY_DISTRICT="district";
    private static final String KEY_STATE="state";
    private static final String KEY_EMAIL="email";
    private static final String KEY_MOBILE="mobile";
    private static final String[] COLUMNS = { KEY_ACCOUNTNAME, KEY_OWNERNSHIP, KEY_FACILITY,
            KEY_STREET,KEY_CITY ,KEY_DISTRICT,KEY_STATE,KEY_EMAIL,KEY_MOBILE};

    public SQLiteDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATION_TABLE = "CREATE TABLE Accounts ( "
                + "account_name TEXT," + "KEY_OWNERNSHIP TEXT, "
                + "KEY_FACILITY TEXT, " + "KEY_STREET TEXT, "
                + "KEY_CITY TEXT, " + "KEY_DISTRICT TEXT, "
                + "KEY_STATE TEXT, " + "KEY_EMAIL TEXT, "
                + "KEY_MOBILE TEXT )";

        db.execSQL(CREATION_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // you can implement here migration process
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        this.onCreate(db);
    }

    public void deleteOne(AccountModel accountModel) {
        // Get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, "account_name = ?", new String[] { String.valueOf(accountModel.getAccountName()) });
        db.close();
    }

    public AccountModel getAccountModel(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, // a. table
                COLUMNS, // b. column names
                " id = ?", // c. selections
                new String[] { String.valueOf(id) }, // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit

        if (cursor != null)
            cursor.moveToFirst();

        AccountModel accountModel = new AccountModel();
        accountModel.setAccountName(cursor.getString(0));
        accountModel.setOwnershipType(cursor.getString(1));
        accountModel.setFacilityType(cursor.getString(2));
        accountModel.setBill_street(cursor.getString(3));
        accountModel.setBill_city(cursor.getString(4));
        accountModel.setBillingDistrict(cursor.getString(5));
        accountModel.setBillingState(cursor.getString(6));
        accountModel.setEmail(cursor.getString(7));
        accountModel.setPhone(cursor.getString(8));

        return accountModel;
    }

    public List<AccountModel> allAccountModels() {

        List<AccountModel> accountModels = new LinkedList<AccountModel>();
        String query = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query, null);
        AccountModel accountModel = null;

        if (cursor.moveToFirst()) {
            do {
                accountModel = new AccountModel();
                accountModel.setAccountName(cursor.getString(0));
                accountModel.setOwnershipType(cursor.getString(1));
                accountModel.setFacilityType(cursor.getString(2));
                accountModel.setBill_street(cursor.getString(3));
                accountModel.setBill_city(cursor.getString(4));
                accountModel.setBillingDistrict(cursor.getString(5));
                accountModel.setBillingState(cursor.getString(6));
                accountModel.setEmail(cursor.getString(7));
                accountModel.setPhone(cursor.getString(8));
                accountModels.add(accountModel);
            } while (cursor.moveToNext());
        }

        return accountModels;
    }

    public void addAccountModel(ArrayList<AccountModel> accountModel) {


        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_ACCOUNTNAME, accountModel.get(0).getAccountName());
        values.put(KEY_OWNERNSHIP, accountModel.get(1).getOwnershipType());
        values.put(KEY_FACILITY, accountModel.get(2).getFacilityType());
        values.put(KEY_STREET, accountModel.get(3).getBill_street());
        values.put(KEY_DISTRICT, accountModel.get(4).getBillingDistrict());
        values.put(KEY_CITY, accountModel.get(5).getBill_city());
        values.put(KEY_STATE, accountModel.get(6).getBillingState());
        values.put(KEY_EMAIL, accountModel.get(7).getEmail());
        values.put(KEY_MOBILE, accountModel.get(8).getPhone());

        // insert
        db.insert(TABLE_NAME,null, values);
        db.close();
    }

    public int updateAccountModel(AccountModel accountModel) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_ACCOUNTNAME, accountModel.getAccountName());
        values.put(KEY_OWNERNSHIP, accountModel.getOwnershipType());
        values.put(KEY_FACILITY, accountModel.getFacilityType());
        values.put(KEY_STREET, accountModel.getBill_street());
        values.put(KEY_DISTRICT, accountModel.getBillingDistrict());
        values.put(KEY_CITY, accountModel.getBill_city());
        values.put(KEY_STATE, accountModel.getBillingState());
        values.put(KEY_EMAIL, accountModel.getEmail());
        values.put(KEY_MOBILE, accountModel.getPhone());

        int i = db.update(TABLE_NAME, // table
                values, // column/value
                "id = ?", // selections
                new String[] { String.valueOf(accountModel.getAccountName()) });

        db.close();

        return i;
    }

}

下面的pojo类包含字符串列表。

AccountModel.java:

public class AccountModel {

    private String accountName;
    private String ownershipType;
    private String facilityType;
    private String email;
    private String phone;
    private String assigned;
    private String bill_street;
    private String bill_city;
    private String createdtime;
    private String modifiedtime;
    private String modifiedby;
    private String account_no;
    private String bill_country;
    private String bill_pin;

    public AccountModel() {
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getOwnershipType() {
        return ownershipType;
    }

    public void setOwnershipType(String ownershipType) {
        this.ownershipType = ownershipType;
    }

    public String getFacilityType() {
        return facilityType;
    }

    public void setFacilityType(String facilityType) {
        this.facilityType = facilityType;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAssigned() {
        return assigned;
    }

    public void setAssigned(String assigned) {
        this.assigned = assigned;
    }

    public String getBill_street() {
        return bill_street;
    }

    public void setBill_street(String bill_street) {
        this.bill_street = bill_street;
    }

    public String getBill_city() {
        return bill_city;
    }

    public void setBill_city(String bill_city) {
        this.bill_city = bill_city;
    }

    public String getBillingDistrict() {
        return billingDistrict;
    }

    public void setBillingDistrict(String billingDistrict) {
        this.billingDistrict = billingDistrict;
    }

    public String getBillingState() {
        return billingState;
    }

    public void setBillingState(String billingState) {
        this.billingState = billingState;
    }

    public String getBillingPIN() {
        return billingPIN;
    }

    public void setBillingPIN(String billingPIN) {
        this.billingPIN = billingPIN;
    }

    public String getCreatedtime() {
        return createdtime;
    }

    public void setCreatedtime(String createdtime) {
        this.createdtime = createdtime;
    }

    public String getModifiedtime() {
        return modifiedtime;
    }

    public void setModifiedtime(String modifiedtime) {
        this.modifiedtime = modifiedtime;
    }

    public String getModifiedby() {
        return modifiedby;
    }

    public void setModifiedby(String modifiedby) {
        this.modifiedby = modifiedby;
    }

    public String getAccount_no() {
        return account_no;
    }

    public void setAccount_no(String account_no) {
        this.account_no = account_no;
    }

    public String getBill_country() {
        return bill_country;
    }

    public void setBill_country(String bill_country) {
        this.bill_country = bill_country;
    }

    public AccountModel(String accountName, String ownershipType, String facilityType, String email, String phone, String assigned, String bill_street, String bill_city, String billingDistrict, String billingState,String account_no, String createdtime, String modifiedtime, String modifiedby, String bill_country,String bill_pin) {
        this.accountName = accountName;
        this.ownershipType = ownershipType;
        this.facilityType = facilityType;
        this.email = email;
        this.phone = phone;
        this.assigned = assigned;
        this.bill_street = bill_street;
        this.bill_city = bill_city;
        this.billingDistrict = billingDistrict;
        this.billingState = billingState;
        this.billingPIN = bill_pin;
        this.account_no=account_no;
        this.createdtime=createdtime;
        this.modifiedtime=modifiedtime;
        this.modifiedby=modifiedby;
        this.bill_country=bill_country;
    }

    private String billingDistrict;
    private String billingState;
    private String billingPIN;



}

下面的代码是我从Api调用的,将响应添加到modelclass并设置适配器并通过recylerview显示 api:

 AccountModel accountModel = new AccountModel(accountName,ownershipType, facilityType, email, phone, assigned, bill_street, bill_city,billingDistrict,billingState,account_no,createdtime,modifiedtime,modifiedby,bill_country,billingPIN);
                listAccount.add(accountModel);

                recyclerViewAccount.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
                recyclerViewAccount.setAdapter(accountAdapter);

fragement.java:

db = new SQLiteDatabaseHandler(getContext());
        recyclerViewAccount = rootView.findViewById(R.id.recyclerOpportunity);
        listAccount = new ArrayList<>();
        accountAdapter = new AccountAdapter(requireContext(),listAccount,this,this);
        recyclerViewAccount.setAdapter(accountAdapter);
评论