0

I've tried the following method to do the task but it isn't working but doesn't show any errors at all. This is the comparing part:

Cursor res = myDb.showKharcha();
final String selected= parent.getItemAtPosition(position).toString();
while(res.moveToNext()) {
    if(selected == res.getString(1)) {
        int num = Integer.parseInt(editText.getText().toString());
        num = num + Integer.parseInt(res.getString(2));
        boolean isUpdated = myDb.updateData(res.getString(0),selected,num);
        if (isUpdated == true) {
            Toast.makeText(add_trans.this,"Kharcha updated to "+ selected,Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(add_trans.this, "Kharcha not added", Toast.LENGTH_LONG).show();
        }

This is the showKharcha function in DatabaseHelper class:

public Cursor showKharcha(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor tyt = db.rawQuery("select * from " + TABLE_NAME,null);
    return tyt;
}

The database Update code is:

public boolean updateData(String ID, String KharchaType, Integer Kharcha){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();

    contentValues.put(COL_1,ID);
    contentValues.put(COL_2,KharchaType);
    contentValues.put(COL_3,Kharcha);
    db.update(TABLE_NAME,contentValues, "ID = ?",new String[] { ID });
    return true;

}
gi_toe
  • 17
  • 8

2 Answers2

0

You have talked about comparing the value of the selected value from the Spinner. Use this final String text = spinner.getSelectedItem().toString();

instead of

final String selected= parent.getItemAtPosition(position).toString();.

It'd be better if you set an OnItemSelectedListener so that when the user selects, the comparison automatically happens:

parent.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
final String text = spinner.getSelectedItem().toString();
...rest of the code fro comparison...
}
});
Aakash Verma
  • 3,705
  • 5
  • 29
  • 66
  • i hadn't used `final` before executing the program but it showed error saying that local variable selected is accessed from within inner class and needs to be declared final. – gi_toe Jul 03 '17 at 11:18
  • Then add final and check. I will update my answer accordingly. – Aakash Verma Jul 03 '17 at 11:20
0
  • Use final String text = spinner.getSelectedItem().toString(); to get selected item from Spinner

And

  • Try if(selected.equals(res.getString(1))) for comparison

Refer this for more details about == and equals

ELITE
  • 5,815
  • 3
  • 19
  • 29
  • Thanks this helped but there's another problem now. When the item from spinner matches with the data from database, i need to add `num` to the data already present in the row and store the sum but it isn't doing so. – gi_toe Jul 03 '17 at 11:25
  • you should write database update code in `myDb.updateData(res.getString(0),selected,num);` method – ELITE Jul 03 '17 at 11:26
  • Nice played, @ELITE. – Aakash Verma Jul 03 '17 at 11:50
  • How come your edit uses the same String variable "text" as I did when Ishan had used "selected"? – Aakash Verma Jul 03 '17 at 12:14
  • `parent.getItemAtPosition(position).toString();` also does the same, but i'm not 100% sure... – ELITE Jul 03 '17 at 12:29