如果我的Sqlite数据库得到更新,请更新当前活动

时间:2016-09-15 14:26:48

标签: android android-activity android-sqlite

我正在努力更新我的活动,如果我的Sqlite数据库中有任何更新,我的活动应该更新。

无论如何,My DataBase已成功更新,但如果我将状态更新为True,则应该禁用此按钮。

我将Button状态更新为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;
}

1 个答案:

答案 0 :(得分:1)

有很多方法可以做到这一点,但我建议创建一些可以传递到数据库更新方法的接口,然后让该方法调用接口的回调方法,如下所示:

public interface MyButtonCallback {

    void updateFromDb(String etat)
}

将回调添加为getEtatReject方法中的参数,然后调用它通常返回的更新方法。

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();

      }
  });
}

确保包含此按钮逻辑implements MyButtonCallback的类以及在调用getEtatReject时,只需将this作为MyButtonCallback的参数,如下所示:

getEtatReject (id, userId, this)

希望这有帮助!

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

如果您使用的是Android工作室,则自动格式化代码的快捷方式为:

选项 + 命令 + L

在macOS上

Ctrl + Alt + L

在Windows / Linux上