0

I am pretty new to android application development. In my app ,I am trying to add money to the wallet which is unsuccessful. The logcat does not show any error in the execution.

Here i am posting myDbAdapter.java(database), add_amount.java and activity_add_amount.xml.

The add_amount.java is as follows:

public class Add_Amount extends AppCompatActivity {
    TextView amount;
    String total;
    TextView up;
    private no.nordicsemi.android.nrftoolbox.myDbAdapter mydb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__amount);
        up=(TextView)findViewById(R.id.textView_wallet);
        amount=(TextView)findViewById(R.id.editText_amount);
        String Amount=(String)amount.getText().toString();
        mydb = new no.nordicsemi.android.nrftoolbox.myDbAdapter(this);
        Cursor rs = mydb.getData(1);
        rs.moveToFirst();
        String wall=rs.getString(rs.getColumnIndex(no.nordicsemi.android.nrftoolbox.myDbAdapter.CONTACTS_COLUMN_WALLET));
        total=Amount+wall;

    }
    public void addamt(View v){
        boolean b=mydb.updateWallet(total,1);
        if(b==true)
            message(getApplicationContext(),"updated");
        else
            message(getApplicationContext(),"not updated");
    }
}

The updateWallet function in myDbAdapter.java is as follows:

    public boolean updateWallet(String amount,Integer id)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("wallet",amount);
        db.update("contacts",contentValues,"id = ? ",new String[] { Integer.toString(id)});
        return true;
    }
}

When I click the button (Add amount) after entering any amount , toast message "updated" is displayed. But it is not getting updated.

Where is the possible error ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

Problem

new String[] { Integer.toString(1)} . As a result, row updated WHERE ID=1.

You should pass DYNAMIC value instead of static 1. You can pass email.

public boolean updateWallet(String amount,final int _id)
    {
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues contentValues=new ContentValues();
        contentValues.put("wallet",amount);
        db.update("contacts",contentValues,"id = ? ",new String[] { Integer.toString(_id)});
        return true;
    }
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

As long as there are no SQL issues (e.g. column/table not found i.e. exceptions raised) then irrespective of whether or not an update occurred true will be returned by the code you currently have.

However, just because the update doesn't fail with an exception (e.g. column/table name not found), it doesn't mean that updates were made.

For example; if you passed an number for an id that doesn't exist the update will not issue an exception, rather nothing will be updated as there is no row to update.

As the update method returns the number of updated rows as an int, you could use this by replacing :-

    db.update("contacts",contentValues,"id = ? ",new String[] { Integer.toString(id)});
    return true;

with something along the lines of :-

    return (db.update("contacts",contentValues,"id = ? ",new String[] { Integer.toString(_id)}) > 0);

Doing so will split your issue into two :-

  • If 0 is returned and the Toast is not updated; then for some reason the update didn't happen (e.g. no row with the specified id exists).

  • If > 0 is returned and the Toast displays updated; then the issue is likely to do with whatever method you are using to determine that no rows appear to have been updated.

Perhaps you could make use of the methods found here that can be used to display database/table information to the Log.

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Toast displayed "updated ". Now from the link mentioned above, where do need to insert those codes?? @MikeT – VINNY KINGER Nov 27 '17 at 11:42
  • @VINNYKINGER use `getAllRowsfromTable` to create Cursor for `logCursorData` method, use them both before and after the update in `updateWallet` method (or elsewhere if need be). – MikeT Nov 27 '17 at 12:46
  • when i am inserting that code its displaying error near "public static Cursor getAllRowsfromTable " saying :inner class cannot have static declaration. . – VINNY KINGER Nov 29 '17 at 06:45
  • You need to create a new Java Class file called `CommonSQLiteUtilities.java`, then copy the code from the link into that file. You can then use `Cursor csr = CommonSQLiteUtilities.getAllRowsFromTable(db,"contacts",true,null); followed by CommonSQLiteUtilities.LogCursorData(csr); – MikeT Nov 29 '17 at 09:32