如果我的 Sqlite 数据库得到更新,则更新当前 activity

Update current activity if my Sqlite DataBase get updated

我正在努力更新我的 activity 如果我的 Sqlite 数据库中有任何更新,它应该得到更新。

无论如何,我的数据库已成功更新,但如果我将他的状态更新为 True ,我应该禁用此按钮。

我将按钮状态更新为 True 的代码:

  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)
}

将回调作为参数添加到您的 getEtatReject 方法中,然后在通常 return.

的地方调用它的更新方法
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();

      }
  });
}

确保包含此按钮逻辑的 class implements MyButtonCallback 并且在调用 getEtatReject 时只需将 this 作为 MyButtonCallback 的参数,就像这样:

getEtatReject (id, userId, this)

希望对您有所帮助!

注意:如果您可以在将代码提交到 Stack Overflow 之前对其进行格式化,那么回答者可以更轻松地快速解决您的问题,更快地获得答案。

如果您使用的是 Android studio,那么自动格式化您的代码的快捷方式是:

选项 + 命令 + L

在 macOS 上

Ctrl + Alt + L

在 Windows/Linux