Android的:如何从微调获得价值,并基于价值重定向到另一个页面 [英] Android: How to get value from Spinner, and based on value to redirect to another page

查看:133
本文介绍了Android的:如何从微调获得价值,并基于价值重定向到另一个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的机器人。我坚持了微调。有两个微调项FROM和TO。
我想code as..if我想要搜索PLACE1,要Place2。然后单击按钮后,如果条件匹配,它应该重定向到另一页。
在运行的应用程序,我没有得到任何错误,也没有反应出来点击按钮。

请参阅下面的code。我已经3路(*注释中),他们没有给出所需的输出试过这件事情。

 包com.aricent.aricentgroupapps;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.View;
进口android.widget.Button;
进口android.widget.Spinner;
公共类SelectLocation延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_select_location);
        按钮路线=(按钮)findViewById(R.id.RouteButton);
        最后的微调SPIN1 =(微调)findViewById(R.id.fromdropdrown);
        最后的微调spin2 =(微调)findViewById(R.id.todropdrown);        route.setOnClickListener(新View.OnClickListener(){            @覆盖
            公共无效的onClick(查看为arg0){
                // TODO自动生成方法存根
            。字符串STR1 = spin1.getSelectedItem()的toString();
            。字符串STR2 = spin2.getSelectedItem()的toString();
            字符串plot31 =情节31;
            字符串plot314 =314地块;
        / *如果(将String.valueOf(spin1.getSelectedItem())==情节17&放大器;&安培;将String.valueOf(spin2.getSelectedItem())==情节16)
            {
                意图I =新意图(SelectLocation.this,Route1.class);
                startActivity(ⅰ);
            }
            如果(spin1.equals(不积17)及和放大器; spin2.equals(不积16))
                {
                    意图I =新意图(SelectLocation.this,Route1.class);
                    startActivity(ⅰ);
                }
                * /            如果(STR1 == plot31&放大器;&安培; STR2 == plot314)
            {
                意图I =新意图(SelectLocation.this,Route1.class);
                startActivity(ⅰ);            }
            }
        });    }    保护字符串的valueOf(对象将selectedItem){
        // TODO自动生成方法存根
        返回的getString(0);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.select_location,菜单);
        返回true;
    }}


解决方案

使用这个

  spinner.setOnItemSelectedListener(新Spinner.OnItemSelectedListener(){
@覆盖
    公共无效onItemSelected(适配器视图<>主,查看视图,INT位置,
            长标识){
            如果(位置== 0){
            //你的code
             }
             如果(位置== 1){
            //你的code
             }
    }
}

I am new to android. I am stuck with the spinner. There are two spinner item "FROM" and "TO". I want to code as..if I want to search FROM Place1, To Place2. Then after clicking button, if condition matches, it should redirect to another page. While running app, I am not getting any error, and no response comes out of clicking button.

Please see the below code. I have tried this thing by 3 way (*in comments), none of them gives desired output.

package com.aricent.aricentgroupapps;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;


public class SelectLocation extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_location);


        Button route = (Button) findViewById(R.id.RouteButton);
        final Spinner spin1 = (Spinner) findViewById(R.id.fromdropdrown);
        final Spinner spin2 = (Spinner) findViewById(R.id.todropdrown);





        route.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            String str1 = spin1.getSelectedItem().toString();
            String str2 = spin2.getSelectedItem().toString();
            String plot31= "Plot 31";
            String plot314= "Plot 314";
        /*  if(String.valueOf(spin1.getSelectedItem())== "Plot 17" && String.valueOf(spin2.getSelectedItem())== "Plot 16")
            {
                Intent i = new Intent(SelectLocation.this, Route1.class);
                startActivity(i);
            }


            if (spin1.equals("Plot 17") && spin2.equals("Plot 16"))
                {
                    Intent i = new Intent(SelectLocation.this, Route1.class);
                    startActivity(i);
                }
                */

            if (str1== plot31 && str2==plot314)
            {
                Intent i = new Intent(SelectLocation.this, Route1.class);
                startActivity(i);

            }
            }
        }); 



    }

    protected String valueOf(Object selectedItem) {
        // TODO Auto-generated method stub
        return getString(0);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.select_location, menu);
        return true;
    }

}

解决方案

use this one

spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
@Override
    public void onItemSelected(AdapterView<?> main, View view, int position,
            long Id) {
            if(position==0){
            //your code
             }
             if(position==1){
            //your code
             }
    }
}

这篇关于Android的:如何从微调获得价值,并基于价值重定向到另一个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆