Is it normal that at the time of execution Android does not consult db?

1

I do not know how to handle my problem, I have my application, it's quite big and it has a lot of code, it does everything perfectly but I have a problem in execution time, I have checkbox s which when they are marked they make a modification in the internal database of the application, and when they are unmarked they perform the same process, a modification to the database, the 2 modify the value that is assigned as showing in the application said element in a gridview , when it is marked it is shown and when it is unchecked it is hidden.

The problem comes when I frame or uncheck the checkbox : There are times when you make the modification in the database and other times not, hence my question, if it is normal that at random execution time make the changes in the database and if there is any way to control this so that it always perform. I leave some code to be understood.

Methods of modifying the database:

public int updateShowMain1(String articulo){
    SQLiteDatabase db = this.getReadableDatabase();
    ContentValues valores = new ContentValues();
    valores.put("showMain", 1);
    Log.i("","Recibe la consulta");
    return db.update("articles",valores,"description = ?", new String[]{articulo});

    //return db.execSQL("UPDATE articles SET showMain = 1 WHERE description = articulo");

}

public int updateShowMain0(String articulo){
    SQLiteDatabase db = this.getReadableDatabase();
    ContentValues valores = new ContentValues();
    valores.put("showMain", 0);

    return db.update("articles",valores,"description = ?", new String[]{articulo});

    //return db.execSQL("UPDATE articles SET showMain = 1 WHERE description = articulo");

}

Checkbox Code:

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(checkBox.isChecked()) {
                Utility.dbHelper.updateShowMain1(articulo.getText().toString());
                Log.i("","Ejecuta la consulta");
                Log.i("","articulo"+ articulo.getText().toString());
            }
            if (!checkBox.isChecked()){
                Utility.dbHelper.updateShowMain0(articulo.getText().toString());
                Log.i("","Ejecuta la consulta");
                Log.i("","articulo"+ articulo.getText().toString());
            }
        }
    });

Greetings

    
asked by J. Burrueco 02.01.2019 в 11:48
source

1 answer

2

Assuming there is no problem calling getReadableDatabase () , method which allows to create or open a database.

Actually it seems to me that the problem is the content of the text within EditText , since the methods you use use this text to determine update a record based on its description.

Therefore, in order to update the data, the description field must be equal to:

articulo.getText().toString()

The possible reasons why it would not happen updating the data that:

  
  • The description does not exist in your table.
  •   
  • The description exists but the text contained within articulo.getText (). toString () has some space.
  •   

If the text has any space, the update of the data will NOT be performed, for this I suggest using the method trim ()

articulo.getText().toString().trim()

in this way you would remove the blanks contained within EditText .

    
answered by 07.01.2019 / 16:56
source