如何将Int从非活动类传递给Activity类

时间:2015-04-18 17:10:10

标签: java android

Android在惯例方面类似于Java,但为什么我的setter和getter方法不起作用。

我有一个微调器(这是一个非活动类),代码如下:

int finalposition;

public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
        for(int i=0; i<40; i++) {
            if (i==pos){
                 setPosition(pos);
            }
        }
    }

    public void setPosition(int position){
        finalposition= position;
    }

    public int getPosition(){
        return finalposition;
    }

,在onCreate()中,这是我的MainActivity(扩展Activity):

//spinner
spinner2 = (Spinner)findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(adapter2);
            spinner2.setOnItemSelectedListener(new CustomOnItemSelectedListener2());

//button
gPath = (Button) findViewById(R.id.getPath);
        gPath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomOnItemSelectedListener2 coisl2= new CustomOnItemSelectedListener2();
                if(coisl2.getPosition()==1)
                    Toast.makeText(getApplicationContext(), "It Works!", Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "position:"+coisl2.getPosition(), Toast.LENGTH_SHORT).show();
            }
        });

我需要做的是,如果我在我的微调器中选择位置1的项目并点击gPath按钮,就会说出“#34;它有效!”#34;应该显示。但是,它不起作用。我在按钮上放了一个toast(显示从调用getPosition()获取的位置)。当我在位置1选择项目并点击该按钮时,它返回position:0。这就是为什么它不能进入​​我实施的if条件,我想知道为什么。我在Java执行此操作,但为什么我在Android遇到麻烦。我的代码中有什么错误/缺失?

感谢任何帮助

2 个答案:

答案 0 :(得分:0)

试试这个。 您正在每个onClick上创建一个新的侦听器实例,该侦听器显然不知道在微调器中选择了哪个项目,因为它没有有效地侦听任何内容。

//declare this as a class object
CustomOnItemSelectedListener2 coisl2= new CustomOnItemSelectedListener2();

//This follows in your onCreate
//spinner
spinner2 = (Spinner)findViewById(R.id.spinner2);
    ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(coisl2);

//button
gPath = (Button) findViewById(R.id.getPath);
        gPath.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(coisl2.getPosition()==1)
                Toast.makeText(getApplicationContext(), "It Works!", Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "position:"+coisl2.getPosition(), Toast.LENGTH_SHORT).show();
        }
    });

答案 1 :(得分:0)

你可以试试这个:

在MainActivity.java下

    spinner2 = (Spinner)findViewById(R.id.spinner2);
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, paths);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(adapter2);
            coisl2= new CustomOnItemSelectedListener2(MainActivity.this,spinner2);

            //button
            gPath = (Button) findViewById(R.id.getPath);
            gPath.setOnClickListener(new View.OnClickListener() 
            {
                @Override
                public void onClick(View v) 
                {
                    if(coisl2.getPosition()==1)
                        Toast.makeText(getApplicationContext(), "It Works!", Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(), "position:"+coisl2.getPosition(), Toast.LENGTH_SHORT).show();
                }
            });

在CustomOnItemSelectedListener2.java下(您可以像下面这样创建此类)

public class CustomOnItemSelectedListener2 
{
    int finalposition;
    public CustomOnItemSelectedListener2(Context context,Spinner spinner)
    {
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int pos, long arg3) {
                // TODO Auto-generated method stub
                setPosition(pos);
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) 
            {
                // TODO Auto-generated method stub
            }
        });
    }

    public void setPosition(int position){
        finalposition= position;
    }

    public int getPosition(){
        return finalposition;
    }
}

希望这就是你想要的......