Android Spinners项目选择

时间:2013-05-16 06:46:43

标签: android spinner

我需要一个示例逻辑代码...... 我有旋转器,我有3个项目----->饼干,薯片,巧克力, 我的Main.xml只包含一个微调器和搜索按钮。

如果我从微调器中选择芯片并单击搜索按钮,它应该导航到芯片页面。 如果我从微调器中选择Biscuits并单击搜索按钮,则应导航到饼干页面。

请帮我解决这个问题  这是代码

public class ShoppingActivity extends Activity {
Spinner sp;
Button b;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b=(Button)findViewById(R.id.button1);
    sp=(Spinner)findViewById(R.id.spinner1);
    //sp.setOnItemSelectedListener((OnItemSelectedListener) this);

    String[] itemz={"Biscuits","Chips","Chocolates"};

    ArrayAdapter<String> aa=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,itemz);
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp.setAdapter(aa);

    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            if(sp.equals("Biscuits")){
                Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
                startActivity(i);

            }
            if(sp.equals("Chips")){
                Intent j=new Intent(ShoppingActivity.this, Chips.class);
                startActivity(j);

            }
            if(sp.equals("Chocolates")){
                Intent k=new Intent(ShoppingActivity.this, Chocolates.class);
                startActivity(k);

            }
        }
    });


}

}

4 个答案:

答案 0 :(得分:2)

你的错误:

通过使用spinner对象分隔文本是错误的:

if(sp.equals("Biscuits")){
    Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
    startActivity(i);
}

解决方案:

尝试以下解决方案,根据您的要求获取所选项目文本并实施解决方案:

Spinner mySpinner = (Spinner)findViewById(R.id.spinner1);
String selectdItemText= mySpinner.getSelectedItem().toString();

if(selectdItemText.equals("Biscuits")){
    Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
    startActivity(i);
}

答案 1 :(得分:1)

试试这个

        if(sp.getSelectedItem().toString().equals("Biscuits")){
            Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
            startActivity(i);

        }

答案 2 :(得分:0)

您可以实现微调器的方法onItemSelected

String itemSelected = "";

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        itemSelected =(String)parent.getItemAtPosition(pos);
    }
    public void onNothingSelected(AdapterView<?> parent) {
    }
});

b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(itemSelected.equals("Biscuits")){
            Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
            startActivity(i);

        }
        if(itemSelected.equals("Chips")){
            Intent j=new Intent(ShoppingActivity.this, Chips.class);
            startActivity(j);

        }
        if(itemSelected.equals("Chocolates")){
            Intent k=new Intent(ShoppingActivity.this, Chocolates.class);
            startActivity(k);

        }
    }
});

答案 3 :(得分:0)

我已编辑了您的代码

b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(sp.getSelectedItem().toString().equals("Biscuits")){
            Intent i=new Intent(ShoppingActivity.this, Biscuits.class);
            startActivity(i);

        }
        if(sp.getSelectedItem().toString().equals("Chips")){
            Intent j=new Intent(ShoppingActivity.this, Chips.class);
            startActivity(j);

        }
        if(sp.getSelectedItem().toString().equals("Chocolates")){
            Intent k=new Intent(ShoppingActivity.this, Chocolates.class);
            startActivity(k);

        }
    }
});