java - 如何在android中使用onItemSelected

标签 java android sqlite spinner

我需要根据我在下拉列表中的选择更新我的表格。 正在进行一些计算,但我不确定它们是否在正确的位置。

我想更新变量dconsumed,下拉列表中有5个选择,一个不同的数字将被添加到该变量中,然后单击按钮后将再次保存到数据库中<强>添加。

这是我的代码

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});
    }

}

最佳答案

您可以在开关案例中使用 updateConsumed 方法,如下所示:

case 0:
 dconsumed = dconsumed+0.1;
 updateConsumed(dconsumed.toString());
 break;

关于java - 如何在android中使用onItemSelected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53715784/

相关文章:

android - ListView 性能很慢

python - SQLite 如何向列中添加数据?

Java SQLite : no such column error

triggers - 插入触发器插入值返回

java - 我的合并排序实现中的运行时错误

java - apache HttpClient,通过表单 : use StringBody instead of FileBody 上传文件

java - JPanel 中没有应用颜色?

android - 使用 Intent 使用 Gmail 6.11.6 发送 HTML 电子邮件

java - Android Dialog如何从自定义适配器Dialog适配器获取文本

java - 从二叉搜索树中递归删除