Android更新行问题
在我们正在组合的应用程序中,我试图更新来自不同活动的行,但它抛出错误或不更新任何内容,因为一旦我从一个活动移动到下一个活动,我似乎无法获取当前的 rowId。任何想法都会很棒。
这来自活动:
Cursor value;
db.open();
long rowId = value.getLong(value.getColumnIndex("_id"));
boolean id = db.updateA(rowId, a1, a2, a3, a4);
db.close();
如果我设置“rowId = 1”,它会很好地更新该行,但我想获取刚刚在不同活动中创建的行。
这是在 DBHelper 文件中:
public boolean updateA(long rowId, String a1,String a2, String a3, String a4) {
ContentValues args = new ContentValues();
args.put(C_A1, a1);
args.put(KEY_A2, a2);
args.put(KEY_A3, a3);
args.put(KEY_A4, a4);
return db.update(TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
In the app we are putting together I am trying to update rows from different Activities but it is throwing an error or not updating anything because I can't seem to get the current rowId once I move from one activity to the next. Any thoughts would be great.
This comes from the Activity:
Cursor value;
db.open();
long rowId = value.getLong(value.getColumnIndex("_id"));
boolean id = db.updateA(rowId, a1, a2, a3, a4);
db.close();
If I set the "rowId = 1" it updates that row just fine but I want to get the row that was just created in a different activity.
This is in the DBHelper file:
public boolean updateA(long rowId, String a1,String a2, String a3, String a4) {
ContentValues args = new ContentValues();
args.put(C_A1, a1);
args.put(KEY_A2, a2);
args.put(KEY_A3, a3);
args.put(KEY_A4, a4);
return db.update(TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要更新记录,我建议在创建时将记录存储在内存中的数据对象中,并将其传递给需要执行更新的其他活动。数据对象应该有一个属于它自己的 ID 的属性,因此您可以使用它。
没有对需要更新的记录的引用将是一场灾难。另外,如果您知道总是需要更新记录,为什么不在插入时执行呢?
If you need to update the record I would suggest storing the record upon creation in a data object in memory and passing that to the other Activity that needs to do the update. The data object should have a property for its own ID, so you can just use that.
Not having a reference to the record you need to update will be a disaster. Also, if you know you always need to update the record, why don't you just do it on insert?
我不明白。
您有一个活动 A,它打开一个数据库、更新一行并关闭该数据库。这是在 SQLiteOpenHelper 类的帮助下完成的。
现在您转到活动 B,然后怎么办?将您的主键(您知道,您使用它作为 A)与 putExtra 一起发布到 B (或者只是将整行放入 B)。如果将 rowid 放入 B,则可以打开、重新读取该行、根据需要进行更新并再次关闭。如果您只是将整行放入 B,那么这就是您需要做的全部 - 但我不喜欢那样(例如,如果 B 崩溃会发生什么?)。
如果您在不知道这两个活动做了什么的情况下更新这两个活动(并且没有重新读取行),那么您最终得到的结果纯属运气。
I don't get that.
You have an activity A that opena a DB, updates a row and closes the DB. This is done with the help of SQLiteOpenHelper class.
Now you move to activity B and now what? Post your primary key (you know it, you used it an A) with putExtra to B (or just put the whole row to B). If you put the rowid to B you can open, reread the row, update if needed and close again. If you just put the whole row to B, well that's all you need to do - but I wouldn't like that (e.g. what happens if B crashes?).
If you update in both activities without knowledge what both activities did (and without re-reading the rows) it's pure luck what you end with.
试试这个:
Try this: