开发者

Spinner ignores conditional statements

开发者 https://www.devze.com 2023-03-16 08:29 出处:网络
So i got my spinner with entries from a StringArray. String[] spinnerEntries = { getResources().getString(R.string.oldPoint),

So i got my spinner with entries from a StringArray.

String[] spinnerEntries = {
                getResources().getString(R.string.oldPoint),
                getResources().getString(R.string.newPoint),
                getResources().getString(R.string.gpsLocati开发者_StackOverflow中文版on) };

Spinner spinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, spinnerEntries);

I want to set the searchPoint depending on spinner selection, but it totally igonores all cases except for the first one (oldPoint). What am I doing wrong? Wheter i got too tired or too stupid, but i just don't see the problem.

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    String spinnerItem = (String) parent.getItemAtPosition(pos);
    if (spinnerItem.equals(getString(R.string.oldPoint))) {
        searchPoint = R.string.oldPoint;
    } else if (spinnerItem.equals(R.string.newPoint)) {
        searchPoint = R.string.newPoint;
    } else if (spinnerItem.equals(R.string.gpsLocation)) {
        searchPoint = R.string.gpsLocation;
    }
}


In the first conditional, you're correctly calling getString() on the constant name. However, on the second two else blocks, you're not.

// Correct
if (spinnerItem.equals(getString(R.string.oldPoint))) {
    searchPoint = R.string.oldPoint;

// Need to call getString() on R.string.newPoint
} else if (spinnerItem.equals(R.string.newPoint)) {
    searchPoint = R.string.newPoint;

// Need to call getString() on R.string.gpsLocation
} else if (spinnerItem.equals(R.string.gpsLocation)) {
    searchPoint = R.string.gpsLocation;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消