我想添加一个带有两个文本的项目列表,请帮我这个,我的数据库类是:
DBHelper.java
public class DBHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "NewDBtest";
private final static int DATABASE_VERSION = 1;
private final static String TABLE_NAME = "table1";
private final static String COL_ID = "id";
private final static String COL_imei = "imei";
private final static String COL_contact = "contactNo";
private static final String KEY_ListItem = "listitem";
public DBHelper(Context context) {
//1. Initialised database
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//2. Create table
String createTableQuery = "CREATE TABLE " + TABLE_NAME + "(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_imei + " TEXT," +
COL_contact + " TEXT," +
KEY_ListItem + " TEXT)";
db.execSQL(createTableQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//
onCreate(db);
}
void addListItem(ArrayList<String> listItem) {
SQLiteDatabase db = this.getWritableDatabase();
// ContentValues values = new ContentValues();
for (int i = 0; i < listItem.size(); i++) {
ContentValues values = new ContentValues();
Log.e("vlaue inserting==", "" + listItem.get(i));
values.put(KEY_ListItem, listItem.get(i));
db.insert(TABLE_NAME, null, values);
}
db.close(); // Closing database connection
}
Cursor getListItem() {
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
return cursor;
}
// ### OWN CRUD METHODS ###
// Create / Insert
public long insert(User user) {
// 1. init database object
SQLiteDatabase DataBase = this.getWritableDatabase();
// 2. init ContentValues object
ContentValues row = new ContentValues();
row.put(COL_imei, user.getImeiNo());
row.put(COL_contact, user.getContact_1());
//3. insert row into table
long id = DataBase.insert(TABLE_NAME, null, row);
//4. disconnect database
DataBase.close();
if (id != 0) {
return id;
}
return 0;
}
public List<User> readAll() {
//1.Init database
SQLiteDatabase DataBase = this.getReadableDatabase();
//2.create query
String selectAllQuery = "Select * from " + TABLE_NAME;
//3. fire query and catch output into cursor object
Cursor result = DataBase.rawQuery(selectAllQuery, null);
// 4. Iterate through a result
if (result != null) {
//4.1 move pointer to first row
boolean isFirstRow = result.moveToFirst();
//4.2 create list of users object
List<User> list = new ArrayList<>();
//result.moveToFirst();
if (isFirstRow) {
do {
String idOfName = result.getString(result.getColumnIndex(COL_ID));
String name = result.getString(result.getColumnIndex(COL_imei));
String last_name = result.getString(result.getColumnIndex(COL_contact));
User object = new User();
object.setId1(idOfName);
object.setImeiNo(name);
object.setContact_1(last_name);
list.add(object);
}
while (result.moveToNext());
return list;
}
}
return null;
}
}
所以,添加列表项是我想要存储在“插入”功能中的列表意味着当我从另一个类调用insert时它将存储文本以及列表,
答案 0 :(得分:0)
您无法直接添加列表。你可以在循环中调用这个方法addListItem()。
答案 1 :(得分:-2)
您无法直接在列中插入列表,但可以使用JSON,
执行此操作请考虑以下代码以供参考,您可以根据自己的要求进行操作,
首先在JSON中转换您的列表,然后在字符串中转换
JSONObject json = new JSONObject();
json.put("listObject", new JSONArray(listItem.get(i)));
// If you have one object then use JSONObject instead of JSONArray
String arrayList = json.toString();
values.put(KEY_ListItem, arrayList);
通过这种方式,您可以插入数据
要从数据库中获取数据,请使用以下代码
JSONObject json = new JSONObject(KEY_ListItem);
ArrayList items = json.optJSONArray("listObject");
这将帮助您满足您的要求。