选择微调项目后设置变量 - Android

时间:2011-12-28 13:40:58

标签: android variables spinner toast

我想在微调器中拾取项目后设置变量。由于我是Java的新手,我不知道最好的方法是做什么。我选择在项目选中之后测试该项目,但我无法让它工作。 必须创建一个新变量,初学者的值必须为0.5,高级的值必须为1,Pro的值必须为1.5。只要我正在开发我的应用程序,我还希望它在选择项目时显示带有变量值的Toast消息。

希望有人可以帮助我,因为我现在卡住了。

我的Java代码:

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

    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) {
            Object test = spinner.getSelectedItem();
            if "Pro".equals(test) {
                // Do something
                Toast.makeText(parent.getContext(), "The planet is " +
                    parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
            }
        }

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

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}   

layout xml

    <LinearLayout
    android:orientation="vertical"
    android:padding="10dip"
    android:id="@+id/linearLayout_Experience"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/experience_prompt" />
    <Spinner 
        android:id="@+id/spinner_experience"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="false"
        android:prompt="@string/experience_prompt" />
</LinearLayout>

我的旋转器的字符串:

<resources>

<string name="experience_prompt">Select your experience</string>
<string-array name="experience_array">
    <item>Beginner</item>
    <item>Advanced</item>
    <item>Pro</item>
</string-array>

3 个答案:

答案 0 :(得分:0)

我的头顶(可能是拼写错误和其他错误):

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();
        } else if(pos == 1) {
            // Advanced
            Toast.makeText(parent.getContext(), "Advanced = 1", Toast.LENGTH_LONG).show();
        } else {
            // Pro
            Toast.makeText(parent.getContext(), "Pro = 1.5", Toast.LENGTH_LONG).show();
        }
    }

您可以使用HashMap对其进行大量修改以保存您在此处建议的不同级别的键:值对,但如果您只有三个级别,那么这应该可以正常工作。

答案 1 :(得分:0)

试试这个:

import android.app.Activity;
import android.view.*;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class AlbumsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final 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);


        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                Object test = spinner.getSelectedItem();
                Toast.makeText(AlbumsActivity.this, "The planet is " +
                        spinner.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // your code here
            }

        });

    }
}

答案 2 :(得分:0)

尝试将Object test = spinner.getSelectedItem();替换为String test = spinner.getSelectedItem().toString();

同样parent.getItemAtPosition(pos).toString()将返回Spinner内部视图的Id而不是文本。要使用此文本:

TextView TV=parent.getItemAtPosition(pos);

并将parent.getItemAtPosition(pos).toString()替换为TV.getText().toString();