SQLite和Activity更新

时间:2012-01-17 13:44:09

标签: android sqlite android-activity

我在Android上有一个整洁,有效的SQLite数据库,用于记录物种观察。然后我使用JSON(成功)验证针对远程数据库上的一组参数输入的观察结果。验证时,数据库中的标志会更新,表示此过程已完成。但是,当我查看我的主要活动时(文本,验证后带有标记的观察值不会更新。

我的活动订单是:

  • 主要活动(数据库中的obs = 0)
  • Obs Entry活动
  • 主要活动(数据库中的标志A,obs = 1)
  • 数据管理活动,验证按钮
  • 验证已完成的活动,主按钮
  • 主要活动(数据库中的标志A,obs = 1)

如果我退出,或者暂停一段时间,然后返回主要活动,数据库正在被正确轮询,而obs = 1,来自db的标志B.

所以我知道我的数据库是正确的,SQL也是正确的。可能是因为我在错误的位置声明我的按钮以使Activity正确恢复()?

我的代码:

final Button verifySightingsButton = (Button) findViewById(R.id.uploadprocess);
if (verifies == 0) {
    verifySightingsButton.setTextColor(getResources().getColor(R.color.red));
} else {
    verifySightingsButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // do stuff
        }
    });
}

其中'verifies'是标志A记录的数量。使用onCreate()方法调用该按钮。如果我将它实例化为类变量会有什么不同吗? 提前谢谢 - 让我觉得无能为力!

更新:这是处理标志验证和更新的代码(实际上使用的是XML,而不是用于其他查询的JSON):

// Get details for an observation within loop
ArrayList d = (ArrayList) allVerifyRows.get(i);
// Build the URL to verify the data
String aURL = buildVerifyURL(d);
// Parse the XML
aValid = ParseVerificationXML.verifyXMLdata(aURL);
db.open();
Long bouCode = new Long((String) speciesList.get((String) d.get(3)));
boolean insert = db.updateNote(
    ((Long) d.get(0)).longValue(),
    ((Long) d.get(1)).longValue(),
    // and some other variables being updated
db.close();

以及检查数据库中是否存在以及未经过验证的记录的代码:

NotesDbAdapter db = new NotesDbAdapter(this);
db.open();
Cursor c = db.fetchAllNotes();
species = new ArrayList<String>();
while (c.moveToNext()) {
    String str1 = c.getString(2); // Date
    species.add(str1);
}
c.close();

Cursor v = db.performNullCountForVerification();
if (v.moveToFirst()) {
    stillToVerify = v.getInt(0); // Non-Verified records
    readyForUpload = stillToVerify;
}
v.close();
stillToVerify = species.size() - stillToVerify;
db.close();

1 个答案:

答案 0 :(得分:1)

您可以使用主活动的onResume方法更新显示的值。每次活动出现在屏幕上时都会调用onResume方法,而onCreate只会在创建活动时调用。