0
即時通訊初學者與android編程。我有簡單的問題..我不知道該怎麼開始。我帶了別人的代碼。我需要獲取所有信息保存在這個數據庫表中,並將其轉換爲json字符串。我怎麼能這樣做?Json字符串到java對象
下面的代碼:
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 我得到一個eror在這一行:
DatabaseDataHandler db = new DatabaseDataHandler(this);
應界定什麼情況?
請參閱鏈接http://stackoverflow.com/questions/18857884/how-to-convert-arraylist-of-custom-class-to-jsonarray-in-java將您的列表更改爲JSONArray,然後調用toString( )來獲取JSON字符串。 – Priya
「this」是用於引用當前實例的關鍵字。這裏的「this」是TimerTask而不是Activity。您必須傳遞活動或應用程序上下文。 – drulabs
我如何獲得上下文?我應該從哪裏拿到它? – user3416113