在sqlite中存储微调器的选定值

时间:2016-04-01 04:33:43

标签: android sqlite spinner

我需要将从微调器中选择的值存储到sqlite数据库中已经存在的表中。我无法从互联网上得到一个清晰的想法,因为它只显示如何在db中输入微调器值而不是所选值。是实现微调器(静态)

的代码
 Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner);

    // Create an ArrayAdapter using the string array and a default spinner
    ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter
            .createFromResource(this, R.array.man_array,
                    android.R.layout.simple_spinner_item);

    // Specify the layout to use when the list of choices appears
    staticAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Apply the adapter to the spinner
    staticSpinner.setAdapter(staticAdapter);

    Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner);

    String[] items = new String[] {"rate_of_flow", "30", "40", "50" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, items);

我的应用程序可以同时使用动态和静态spinner。我没有任何偏好。你能帮我吗?

2 个答案:

答案 0 :(得分:1)

为什么不简单地获取所选项目并将所选值插入数据库,如此处

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String selected = parent.getItemAtPosition(position).toString();
            //Code to insert selected string in database
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

答案 1 :(得分:1)

试试这个:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String string = (String) spinner.getItemAtPosition(spinner.getSelectedItemPosition());
            Toast.makeText(YourActivity.this, "Selected Item "+string, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}