Reputation: 139
I need to update my table based on my choice in the dropdown list. There are some calculations going on and I'm not sure if they are in the right place.
I want to update the variable dconsumed, there are 5 choices in the dropdown list, a different number will be added to this variable and then will be saved in the database again after clicking the button add.
here is my code
public class WaterActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
SQLiteOpenHelper openHelper;
SQLiteDatabase db;
Cursor cursor;
TextView percantage,outof;
Button addbtn;
double waterneeded;
double neededinounces;
double weightinpound;
String userId;
double dconsumed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_water);
// DB
openHelper=new DatabaseHelper(this);
db = openHelper.getWritableDatabase();
// get id from homepage
userId = getIntent().getExtras().getString("id");
cursor = db.rawQuery("SELECT * FROM " + R_TABLE_NAME + " WHERE " + DatabaseHelper.R_COL_1 + "=? ", new String[]{userId});
if(cursor!=null){
if (cursor.getCount()>0){
cursor.moveToNext();
String STRconsumed = cursor.getString(cursor.getColumnIndex(DatabaseHelper.R_COL_9));
String STRweight = cursor.getString(cursor.getColumnIndex(DatabaseHelper.R_COL_10));
//string to double
double dconsumed = Double.valueOf(STRconsumed).doubleValue();
double dweight = Double.valueOf(STRweight).doubleValue();
//calculate water needed
weightinpound = dweight * 2.2046;
neededinounces = weightinpound * 2/3;
waterneeded = neededinounces * 0.0295735;
//show in text view
percantage = (TextView) findViewById(R.id.percID);
percantage.setText((dconsumed/waterneeded)*100+"%");
outof = (TextView) findViewById(R.id.outofID);
outof.setText( dconsumed + " L out of " + String.format("%.1f", waterneeded) + " L");
}else{
Toast.makeText(getApplicationContext(),"..",Toast.LENGTH_LONG).show();
}
}
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("0.1 L");
categories.add("0.2 L");
categories.add("0.3 L");
categories.add("0.33 L");
categories.add("0.6 L");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
// To select item
spinner.setOnItemSelectedListener(this);
addbtn = (Button)findViewById(R.id.buttonAdd);
addbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String consumedStr = Double.toString(dconsumed);
updateConsumed(consumedStr);
Toast.makeText(getApplicationContext(),"Cup is added!",Toast.LENGTH_LONG).show();
//Intent i = new Intent(WaterActivity.this, WaterActivity.class);
//startActivity(i);
}
});
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//String sSelected=parent.getItemAtPosition(position).toString();
//Toast.makeText(this,sSelected,Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
// Whatever you want to happen when the first item gets selected
dconsumed = dconsumed + 0.1;
//String strconsumed = Double.toString(consumed);
//updateConsumed(strconsumed);
break;
case 1:
dconsumed = dconsumed + 0.2;
break;
case 2:
dconsumed = dconsumed + 0.3;
break;
case 3:
dconsumed = dconsumed + 0.33;
break;
case 4:
dconsumed = dconsumed + 0.6;
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
public void updateConsumed(String consumed){
ContentValues cv = new ContentValues();
cv.put(DatabaseHelper.R_COL_9,consumed);
db.update(R_TABLE_NAME, cv,"ID=?",new String[] {userId});
}
}
Upvotes: 0
Views: 137
Reputation: 108
You can use updateConsumed method in your switch cases as below:
case 0:
dconsumed = dconsumed+0.1;
updateConsumed(dconsumed.toString());
break;
Upvotes: 1