这是代码:
package com.project.sqlite;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseDataHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 3;
// Database Name
private static final String DATABASE_NAME = "simplify_1";
// DATA table name
private static final String TABLE_DATA = "FORMS";
// DATA Table Columns names
private static final String KEY_DATA_ID = "id";
private static final String KEY_OBJECT_ID = "objectID";
private static final String KEY_TYPE = "type";
private static final String KEY_DATE = "date";
private static final String KEY_JSON = "json";
public DatabaseDataHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_DATA_TABLE = "CREATE TABLE " + TABLE_DATA + "("
+ KEY_DATA_ID + " INTEGER PRIMARY KEY," + KEY_OBJECT_ID + " TEXT," + KEY_TYPE + " TEXT,"
+ KEY_DATE + " DATE," + KEY_JSON + " TEXT" + ")";
db.execSQL(CREATE_DATA_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DATA);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Add new DATA
public void addData(Data data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_OBJECT_ID, data.getObjectID());
values.put(KEY_TYPE, data.getType());
values.put(KEY_DATE, data.getDate());
values.put(KEY_JSON, data.getJson());
// Inserting Row
db.insert(TABLE_DATA, null, values);
db.close(); // Closing database connection
}
// Getting All DATA
public List<Data> getAllData() {
List<Data> dataList = new ArrayList<Data>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_DATA;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Data data = new Data();
data.setID(Integer.parseInt(cursor.getString(0)));
data.setObjectID(cursor.getString(1));
data.setType(cursor.getString(2));
data.setDate(cursor.getString(3));
data.setJson(cursor.getString(4));
// Adding contact to list
dataList.add(data);
} while (cursor.moveToNext());
}
// return user list
return dataList;
}
}
这是我的课程,我想这样做:
import java.util.TimerTask;
import com.project.sqlite.DatabaseDataHandler;
import android.util.Log;
public class Data_synch extends TimerTask {
DatabaseDataHandler db = new DatabaseDataHandler(this);
@Override
public void run() {
}
}
你能告诉我正确的方法吗?
UPDATE 我对这一行感到惊讶:
DatabaseDataHandler db = new DatabaseDataHandler(this);
应该定义什么上下文?
答案 0 :(得分:1)
您是否尝试过google的gson将json对象转换为java对象。太棒了。
这是下载链接gson 2.2.4
只需将其添加到libs文件夹即可。在一行代码中,您可以将java对象转换为json,反之亦然。
样品:
让我们说你的json对象就是这个
JSONObject myJson = {"name":"Tony Stark","group":"avengers","occupation":"superhero"}
您的java类必须具有匹配的州名
class JsonToJAVA {
string name,
string group,
string occupation,
}
以下是您在一行中进行转换的方式:
JsonToJAVA j = gson.fromJson(myJson.toString(), JsonToJAVA.class);
只是在某处初始化gson
Gson gson = new Gson();
你有一个代表json的类(j以上)的实例。随意在JsonToJAVA类中添加getter setter或任何其他方法。
在GSON中有另一种方法toJson()将java类转换为json。
希望这有帮助。