为什么“ selectedCountry”,“ selectedAge”,“ selectedGender”返回null?

时间:2019-06-07 07:08:23

标签: java android spinner android-spinner

当我运行我的应用程序并转到“ EditProfile”活动时。然后,我将立即收到吐司消息“某事有误”,这意味着变量“ selectedCountry”,“ selectedAge”和“ selectedGender”为空。

 public class EditProfile extends AppCompatActivity {

UserInfo userInfo;
UserInfo profileDetails;
Spinner spinnerFrom;
Spinner spinnerAge;
Spinner spinnerGender;
EditText hobbyEdit;
Button btnDone;
TextView textView;
String selectedCountry;
String selectedAge;
String selectedGender;

DatabaseHelper mydb = new DatabaseHelper(this, "MyUsers", null, 5);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_profile);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), Profile.class);
            intent.putExtra("id", userInfo.getId());
            intent.putExtra("username", userInfo.getUsername());
            startActivity(intent);
        }
    });

    Bundle extras = getIntent().getExtras();
    int id = extras.getInt("id");
    final String username = extras.getString("username");


    // Username
    userInfo = new UserInfo(id, username);
    textView = (TextView) findViewById(R.id.usernameUnedit);
    textView.setText(username);



    // Country
    spinnerFrom = (Spinner) findViewById(R.id.spinnerFrom);
    Locale[] locales = Locale.getAvailableLocales();
    final ArrayList<String> countries = new ArrayList<String>();
    countries.add("-");
    for (Locale locale : locales) {
        String country = locale.getDisplayCountry();
        if (country.trim().length() > 0 && !countries.contains(country)) {
            countries.add(country);
        }
    }

    Collections.sort(countries);

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

    countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerFrom.setAdapter(countryAdapter);
    spinnerFrom.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedCountry = parent.getItemAtPosition(position).toString();

        }

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

        }
    });



    // Age
    spinnerAge = (Spinner) findViewById(R.id.spinnerAge);
    final ArrayList<String> ages = new ArrayList<String>();
    ages.add("-");
    for (int i = 18; i < 100; i++) {
        ages.add(String.valueOf(i));
    }

    ArrayAdapter<String> ageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ages);
    ageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerAge.setAdapter(ageAdapter);
    spinnerAge.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedAge = parent.getItemAtPosition(position).toString();

        }

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

        }
    });



    // Gender
    spinnerGender = (Spinner) findViewById(R.id.spinnerGender);
    final ArrayList<String> genders = new ArrayList<String>();
    genders.add("-");
    genders.add("Male ♂");
    genders.add("Female ♀");
    genders.add("Other");

    ArrayAdapter<String> genderAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, genders);
    genderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerGender.setAdapter(genderAdapter);
    spinnerGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedGender = parent.getItemAtPosition(position).toString();

        }

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

        }
    });



    // Hobby
    hobbyEdit = (EditText) findViewById(R.id.hobbyEdit);



    // Button done
    btnDone = (Button) findViewById(R.id.btnDone);
    btnDone.setEnabled(false);

/* Stuck at this if statement below */
    if (selectedCountry != null && selectedAge != null && selectedGender != null) {
        if (!mydb.isStoredProfileDetails(username, selectedCountry, selectedAge, selectedGender, hobbyEdit.getText().toString())) {
            btnDone.setEnabled(true);
            btnDone.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    profileDetails = new UserInfo(selectedCountry, selectedAge, selectedGender, hobbyEdit.getText().toString());
                    mydb.updateTable(username, profileDetails);

                }
            });
        } else {
            Toast.makeText(this, "Something is wrong 2.", Toast.LENGTH_LONG).show();
        }
    } else {
        Toast.makeText(this, "Something is wrong.", Toast.LENGTH_LONG).show();
    }
}
}

我希望从这3个下拉列表(微调列表)中选择一个项目,并且所选项目的值在数据库中不存在,然后可以单击“完成”按钮。单击该按钮后,它将通过更新从下拉列表(纺锤)中选择的值存储到数据库中。

不幸的是,由于一直返回“ else”语句,因此我陷入了给定代码中所述的if语句。

3 个答案:

答案 0 :(得分:0)

将变量设置为...

String selectedCountry = "";
String selectedAge = "";
String selectedGender = "";

并检查条件为...

if (!selectedCountry.equals("") && !selectedAge.equals("") && !selectedGender.equals("")) {
//your code goes here
}

答案 1 :(得分:0)

您必须在Spinner的OnItemSelectedListener中检查这两个条件,如果两个条件都返回true,则启用按钮。然后像这样在onCreateView的按钮上调用setonClickListener。

 spinnerGender.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedGender = parent.getItemAtPosition(position).toString();
            //Conditions to enable button
            if (selectedCountry != null && selectedAge != null && selectedGender != null) {
            if (!mydb.isStoredProfileDetails(username, selectedCountry, selectedAge, selectedGender, hobbyEdit.getText().toString())) {
                btnDone.setEnabled(true);
            }

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

        }
    });

在所有微调器中添加这些行。

答案 2 :(得分:0)

在您的onCreate方法中,您已实例化视图并将侦听器附加到它们。很好。但是,这并不意味着这些侦听器中的逻辑将在同一时间和顺序执行。您刚刚定义了侦听器的行为。完成所有初始化之后,您的代码将直接检查问题中提到的三个字符串的值。它不等待用户与微调框互动。

您想要的是在用户从微调器中选择某些内容时触发“完成”按钮中的更改。因此,设置字符串并由此检查是否可以启用“完成”按钮的逻辑必须进入微调器的侦听器中。

我不是给您直接的代码解决方案,而是您可以采用的方法。