嗨,我正在开发应用程序,该应用程序包含来自解析的 json 通知,应用程序将其存储在 SQLite 中主要 Activity 是我的代码
public class Receiver extends ParsePushBroadcastReceiver {
@Override
public void onPushReceive(Context context, Intent intent) {
super.onPushReceive(context, intent);
Log.e("Push", "Clicked");
Intent i = new Intent(context, splashscreen.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Log.d("Alert: ", "Inserting from onReceive");
Bundle extras = intent.getExtras();
String jsonData = extras.getString("com.parse.Data");
JSONObject jsonObject;
try {
jsonObject = new JSONObject(jsonData);
String alert = jsonObject.getString("alert");
Log.d("Insert: ",alert);
String Message = jsonObject.getString("Message");
Log.d("Title: ",Message);
String Date = jsonObject.getString("Date");
Log.d("Date: ",Date);
DatabaseHandler db=new DatabaseHandler(context);
db.addnotification(new notification(alert,Message,Date));
Log.d("Reading: ", "Reading all notifications..");
List<notification> notifications = db.getAllnotifications();
for (notification cn : notifications) {
String log = "Id: "+cn.getID()+" ,Title: " + cn.gettitle() + " ,Message: " + cn.getmessage()+ " ,Date: " + cn.gettimedate();
// Writing notifications to log
Log.d("Name: ", log);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
最佳答案
进行中,,,
一个示例 dao 类如下,
public class DocumentRepository {
public static final String TABLE_DOCUMENT = "document";
public static final String COLUMN_ID = BaseColumns._ID;
public static final String RECORD_ID = "record_id";
public static final String UUID_DOCUMENT_NAME = "unique_file_name";
public static final String DOCUMENT_TABLE_CREATE = "create table if not exists "
+ TABLE_DOCUMENT + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + RECORD_ID
+ " integer , " + UUID_DOCUMENT_NAME
+ " text );";
private Context context;
public DocumentRepository(Context context) {
this.context = context;
}
public ArrayList<Document> geDocuments(Integer customerId) {
ArrayList<Document> documents = new ArrayList<>();
String query = "SELECT * FROM " + TABLE_DOCUMENT + " WHERE " + RECORD_ID + " = '" + customerId + "'";
SQLiteDatabase database = ......// create a instance of your SqliteDatabase implementation
Cursor cursor = database.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
Document document = new Document();
document.setId(cursor.getInt(0));
document.setUniqueIdentifier(cursor.getString(3));
documents.add(document);
} while (cursor.moveToNext());
}
return documents;
}
}
在此示例中
geDocuments()
会给你对象列表。关于android - 使用基本适配器和 ListView 显示来自 SQLite 的变量数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32406666/