我有一个来自API的ArrayList,我的POJO(普通旧Java对象)具有所需的getter和setter。
@SerializedName("staff")
@Expose
private List<String> staff = new ArrayList<String>();
@SerializedName("departments")
@Expose
private List<String> departments = new ArrayList<String>();
我想将员工和部门添加到 SQLite表中。 我现有的表具有其余的字符串值。但是我无法将 ArrayList 添加到表中。
public static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME + "" +
" (" + ID + " TEXT PRIMARY KEY not null, " +
//need to change product id into String or TEXT
CLIENTS_NAME + " TEXT not null," +
CLIENTS_ADDRESS1 + " TEXT not null," +
CLIENTS_ADDRESS2 + " TEXT not null," +
CLIENTS_ADDRESS3 + " TEXT not null," +
CLIENTS_ADDRESS4 + " TEXT not null," +
TYPE + " TEXT not null," +
CLIENTS_CONTACT + " TEXT not null)" ;
如何添加Arraylist? 我试过了this但它不起作用。 任何链接或提示将不胜感激。
这是将其余字符串值插入数据库。注意,我还没有将 staff 添加到数据库表中,因为我不知道如何在数据库中插入一个Arraylist表
public void addProducts(Clients_POJO products) {
//CRUD , adding Products
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Constants.ClientsDATABASE.ID, products.getId());
values.put(Constants.ClientsDATABASE.CLIENTS_NAME, products.getName());
values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS1, products.getAddress1());
values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS2, products.getAddress2());
values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS3, products.getAddress3());
values.put(Constants.ClientsDATABASE.CLIENTS_ADDRESS4, products.getAddress4());
values.put(Constants.ClientsDATABASE.TYPE, products.getType());
values.put(Constants.ClientsDATABASE.CLIENTS_CONTACT, products.getContact());
try {
db.insert(Constants.ClientsDATABASE.TABLE_NAME, null, values);
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
答案 0 :(得分:0)
我找到了解决方案而没有使用GSON转换它。 让部门的POJO值为ArrayList。
@SerializedName("departments")
@Expose
private List<String> departments = new ArrayList<String>();
要创建数据库,我们将列作为字符串存储在表中。
public static final String INPUT_DEPARTMENTS = "departments";
public static final String CREATE_TABLE_QUERY = "CREATE TABLE " + TABLE_NAME + "" +
" (" + ID + " TEXT PRIMARY KEY not null, " +
INPUT_DEPARTMENTS + " TEXT not null)" ;
现在在SQLite数据库中存储和检索数据时.Storing - &gt;
public void addProducts(Clients_POJO products) {
//CRUD , adding Products
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Constants.ClientsDATABASE.INPUT_DEPARTMENTS, String.valueOf(products.getDepartments()));
try {
db.insert(Constants.ClientsDATABASE.TABLE_NAME, null, values);
} catch (Exception e) {
Log.d(TAG, e.getMessage());
}
为了检索数据,我们会将其转换回arraylist以将值存储在POJO中。
SQLiteDatabase db = this.getWritableDatabase();
final ProductFetchListener mListener = listener;
Cursor cursor = db.rawQuery(Constants.ClientsDATABASE.GET_PRODUCTS_QUERY,null);
final List<Clients_POJO> productsListDB = new ArrayList<>();
if (cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
Clients_POJO products = new Clients_POJO();
products.setFromDatabase(true);
现在我们将其转换为arraylist。
String s = cursor.getString(cursor.getColumnIndex(Constants.ClientsDATABASE.INPUT_DEPARTMENTS));
List<String> myList = new ArrayList<String>(Arrays.asList(s.split(",")));
products.setDepartments(myList);