我需要根据下拉列表中的选择更新表。 正在进行一些计算,我不确定它们是否在正确的位置。
我想更新变量 dconsumed ,下拉列表中有5个选择,将不同的数字添加到该变量,然后在单击按钮后再次保存在数据库中< strong>添加。
这是我的代码
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});
}
}
答案 0 :(得分:1)
您可以在以下情况下使用updateConsumed方法:
case 0:
dconsumed = dconsumed+0.1;
updateConsumed(dconsumed.toString());
break;