简体   繁体   中英

How to Pass an Int from non-activity class to Activity class

Android is similar to Java in terms of convention but why does my setter and getter methods won't work.

I have a spinner(which is a non-activity class) with code like this:

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;
    }

and in onCreate(), here is my MainActivity(which extends 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();
            }
        });

What I need to do is that if I select the item at position 1 in my spinner and clicked gPath button, a toast saying "It Works!" should be displayed. However, it does not work. I put a toast(displaying the position that was taken from calling getPosition()) on the button. When I selected the item at position 1 and click the button, it returned position:0 . So that's why it won't enter the if-condition that I implemented and I'm wondering why. I do this in Java but why am I having troubles in Android . What is wrong/missing in my code?

Any help is appreciated

Try this. You are creating a new instance of the listener on each onClick, and that listener would obviously not know which item was selected in the spinner, since it isn't effectively listening over anything.

//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();
        }
    });

You Can try this:

Under 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();
                }
            });

Under CustomOnItemSelectedListener2.java( You can create this class like below)

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;
    }
}

Hope this is what you want...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM