选择项目时设置双精灵Spinner - Android

时间:2012-01-05 13:50:44

标签: android textview double spinner

我创建了一个带有3个选项的微调器:Beginner,Advanced和Pro。当选择其中一个选项时,我想创建一个toast并设置一个double。我需要double稍后使用该值,以使用edittext中的2个其他值进行计算。 出于开发目的,我想设置具有该值的textview,以进行检查。 我尝试过一些东西,但是我不能把它搞定。 textview一直显示0.0

希望有人可以帮助我。

我的java代码:

package tim.steunebrink.tab;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AlbumsActivity  extends Activity{


private TextView ExpValue;
private double expchosen;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.albums_layout);

    ExpValue = (TextView) findViewById(R.id.ExpValue);

    Spinner spinner = (Spinner) findViewById(R.id.spinner_experience);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.experience_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    class MyOnItemSelectedListener implements OnItemSelectedListener {

        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            if (pos == 0) {
                // Beginner
                Toast.makeText(parent.getContext(), "Beginner = 0.5", Toast.LENGTH_LONG).show();
                double expchosen = new Double("0.5");

            } else if(pos == 1) {
                // Advanced
                Toast.makeText(parent.getContext(), "Advanced = 1.0", Toast.LENGTH_LONG).show();
                double expchosen = new Double("1.0");

            } else {
                // Pro
                Toast.makeText(parent.getContext(), "Pro = 1.5", Toast.LENGTH_LONG).show();
                double expchosen = new Double("1.5");

            }


            ExpValue.setText(Double.toString(expchosen));

        }

        public void onNothingSelected(AdapterView<?> parent) {
            // Do Nothing
        }
    }

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}   
}

2 个答案:

答案 0 :(得分:2)

进行这些更改

class MyOnItemSelectedListener implements OnItemSelectedListener {
        double expchosen ;
        public void onItemSelected(AdapterView<?> parent,
                View view, int pos, long id) {
            if (pos == 0) {
                // Beginner
                Toast.makeText(parent.getContext(), "Beginner = 0.5", Toast.LENGTH_LONG).show();
                expchosen= new Double("0.5");
            } else if(pos == 1) {
                // Advanced
                Toast.makeText(parent.getContext(), "Advanced = 1.0", Toast.LENGTH_LONG).show();
                expchosen = new Double("1.0");
            } else {
                // Pro
                Toast.makeText(parent.getContext(), "Pro = 1.5", Toast.LENGTH_LONG).show();
                expchosen = new Double("1.5");
            }
            ExpValue.setText(Double.toString(expchosen));
        }

        public void onNothingSelected(AdapterView<?> parent) {
            // Do Nothing
        }
    }

答案 1 :(得分:1)

以下代码可能对您有用。

申请

ExpValue.invalidate();

行后

ExpValue.setText(Double.toString(expchosen));