0

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

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

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

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

4 回答 4

2

你的错误:

通过将文本与错误的微调器对象进行比较,您犯了错误:

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);
}
于 2013-05-16T06:54:16.993 回答
1

尝试这个

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

        }
于 2013-05-16T06:54:07.353 回答
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);

        }
    }
});
于 2013-05-16T06:50:53.410 回答
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);

        }
    }
});
于 2013-05-16T08:21:39.457 回答