Android 如果我的Sqlite数据库得到更新,则更新当前活动
我正在更新我的活动,如果我的Sqlite数据库中有任何更新,该活动应该得到更新 无论如何,我的数据库已成功更新,但如果我将其状态更新为True,则应禁用此按钮 将按钮状态更新为True的我的代码:Android 如果我的Sqlite数据库得到更新,则更新当前活动,android,android-activity,android-sqlite,Android,Android Activity,Android Sqlite,我正在更新我的活动,如果我的Sqlite数据库中有任何更新,该活动应该得到更新 无论如何,我的数据库已成功更新,但如果我将其状态更新为True,则应禁用此按钮 将按钮状态更新为True的我的代码: public void InsertEtatReject(String etatReject,int id,String userId) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values =
public void InsertEtatReject(String etatReject,int id,String userId)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(EtatReject,etatReject);
db.update(TABLE_NAME, values,"userID=? AND id=?",new String[]{userId,Integer.toString(id)});
db.close();
}
我的按钮代码:
//Verify if button state is True or False
etat = databaseHelper.getEtatReject(id,username);
if (etat.equals("False"))
{
rejete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
AlertDialog.Builder builder2 = new AlertDialog.Builder(DescriptionList.this);
// Set the dialog title
builder2.setTitle("Pourquoi cette annonce est inconvenable ?")
.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
})
// Set the action buttons
.setPositiveButton("Envoyer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
final String itemname = (String) items[selectedPosition];
final int finalId = id;
//Update Button state
databaseHelper.InsertEtatReject("True",finalId,username);
if(itemname.equals("Autre"))
{
Toast.makeText(DescriptionList.this, "Autre ", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(DescriptionList.this, "Annonce rejetée :" + itemname, Toast.LENGTH_LONG).show();
}
}
})
.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// removes the dialog from the screen
}
})
.show();
}
});
}
else{
rejete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(DescriptionList.this,"Vous avez déja rejeteé cette annonce",Toast.LENGTH_SHORT).show();
}
});
为了从数据库中获取数据,我使用以下方法:
public String getEtatReject(int id,String userId)
{
SQLiteDatabase db = this.getWritableDatabase();
String etat = null ;
String SelectQuery = "SELECT * FROM "+TABLE_NAME +" WHERE userID=? AND id=?";
Cursor cursor = db.rawQuery(SelectQuery,new String[]{userId,Integer.toString(id)});
try {
if(cursor.moveToFirst())
{
do {
etat = cursor.getString(25);
}
while (cursor.moveToNext());
}
}
finally {
cursor.close();
db.close();
}
return etat;
}
有很多方法可以做到这一点,但我建议创建某种类型的接口,您可以将其传递到数据库更新方法中,然后让该方法调用接口的回调方法,如下所示:
public interface MyButtonCallback {
void updateFromDb(String etat)
}
将回调作为一个参数添加到geteTraject
方法中,然后在通常返回的位置调用它的update方法
public void getEtatReject(int id,String userId, MyButtonCallback callback)
{
SQLiteDatabase db = this.getWritableDatabase();
String etat = null ;
String SelectQuery = "SELECT * FROM "+TABLE_NAME +" WHERE userID=? AND id=?";
Cursor cursor = db.rawQuery(SelectQuery,new String[]{userId,Integer.toString(id)});
try {
if(cursor.moveToFirst())
{
do {
etat = cursor.getString(25);
}
while (cursor.moveToNext());
}
}
finally {
cursor.close();
db.close();
}
callback.updateFromDb(etat);
}
然后使用按钮代码中已有的逻辑实现MyButtonCallback接口
//Verify if button state is True or False
@Override
public void updateFromDb(String etat)
{
if (etat.equals("False"))
{
rejete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
AlertDialog.Builder builder2 = new AlertDialog.Builder(DescriptionList.this);
// Set the dialog title
builder2.setTitle("Pourquoi cette annonce est inconvenable ?")
.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
}
})
// Set the action buttons
.setPositiveButton("Envoyer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
final String itemname = (String) items[selectedPosition];
final int finalId = id;
//Update Button state
databaseHelper.InsertEtatReject("True",finalId,username);
if(itemname.equals("Autre"))
{
Toast.makeText(DescriptionList.this, "Autre ", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(DescriptionList.this, "Annonce rejetée :" + itemname, Toast.LENGTH_LONG).show();
}
}
})
.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// removes the dialog from the screen
}
})
.show();
}
});
}
else{
rejete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(DescriptionList.this,"Vous avez déja rejeteé cette annonce",Toast.LENGTH_SHORT).show();
}
});
}
确保包含此按钮逻辑的类实现MyButtonCallback,并且在调用GeteTraject
时,只需将this
作为MyButtonCallback
的参数,如下所示:
geteTraject(id,userId,this)
希望这有帮助
注意:如果您可以在将代码提交到堆栈溢出之前对其进行格式化,这将使回答者更快地解决问题,更快地得到答案
如果您使用的是Android studio,则自动格式化代码的快捷方式有:
选项+命令+L
关于马科斯
或
Ctrl+Alt+L
在Windows/Linux上@cricket\u 007我已经编辑了我的答案。好的,那么您在代码中尝试了什么?数据库无法修改按钮,而且按钮本身也不会更新,所以我不太理解这个问题question@cricket_007我正在从我的数据库中检查状态是真是假,我正在通过InsertEtatRejectYes方法提升状态,这会更新数据库,但是,正如我所说,这不会自动改变UI或按钮的任何内容。回答:我按照你的步骤做,我会让它工作:D!