在spinner.onItemSelectedItem中获取editText的值

时间:2017-03-15 13:34:28

标签: android android-edittext android-spinner

我有一个微调器,我将使用循环加载它,我也将它插入我的数据库中的默认值,在我执行循环后,如果用户选择一个项目,它将从我的数据库中获取所选项目所以如果CursorAnswer不等于默认值(" shananaking")和"选择#1"然后我将CursorAnswer的当前值设置为我的EditText,所以如果它的真实我将更新我的布尔值gotAnswer为真并打破它所以如果gotAnswer为真我将获取我的EditText中的文本然后将其插入我的数据库但问题是我在editText中得到一个空值,请帮助!!!

public void initSpinner(){
    ArrayList<String> ArrayListItems = new ArrayList<String>();
    EditTextAnswer = (EditText) findViewById(R.id.editTextAnswer);

    final Spinner SpinnerItems = (Spinner)findViewById(R.id.spinnerItems);
    for(int i = 0; i <= 100; i++){
        ArrayListItems.add(String.valueOf(i));
        if(i == 0){
            mydb.InsertItemsZero(mydb, i, "Choose #1");
        }else mydb.InsertItemsZero(mydb, i, "shananaking");
    }

    ArrayAdapter<String> ItemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, ArrayListItems);
    SpinnerItems.setAdapter(ItemAdapter);

    SpinnerItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            mGetSpinner = Integer.parseInt(SpinnerItems.getSelectedItem().toString());


            DatabaseHelper mydb = new DatabaseHelper(context);
            Cursor rs = mydb.RetrieveItems(mGetSpinner);
            if (rs.getCount() != 0) {
                rs.moveToFirst();
                do {
                    CursorItemID = rs.getString(0);
                    CursorItems = rs.getString(1);
                    CursorAnswer = rs.getString(2);

                    if (!CursorAnswer.equals("") && !CursorAnswer.equals("shananaking") && !CursorAnswer.equals("Choose #1")) {
                        Toast.makeText(getApplicationContext(), CursorAnswer, Toast.LENGTH_LONG).show();
                        EditTextAnswer.setText(CursorAnswer);
                        gotAnswer = true;
                        break;
                    } else if (CursorAnswer.equals("Choose #1")) {
                        EditTextAnswer.setText("Choose #1");
                        normalAnswer = true;
                        //break;
                    } else {
                        Toast.makeText(getApplicationContext(), "No Answer", Toast.LENGTH_LONG).show();
                        newAnswer = true;
                        break;
                    }

                } while (rs.moveToNext());
            }


            if (gotAnswer) {
                String Editable = EditTextAnswer.getText().toString();
                Toast.makeText(getApplicationContext(), Editable, Toast.LENGTH_LONG).show();
                mydb.UpdateDummy(mydb, mGetSpinner, Editable);
                gotAnswer = false;
            }
            if (normalAnswer) {
                Toast.makeText(getApplicationContext(), "Normal Answer", Toast.LENGTH_LONG).show();
                mydb.UpdateDummy(mydb, mGetSpinner, "Choose #1");
                normalAnswer = false;
            }
            if (newAnswer) {
                String Editable = EditTextAnswer.getText().toString();
                Toast.makeText(getApplicationContext(), Editable, Toast.LENGTH_LONG).show();
                mydb.UpdateDummy(mydb, mGetSpinner, Editable);
                newAnswer = false;
            }
        }

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

        }
    });

}

0 个答案:

没有答案