简体   繁体   中英

Getter and setter variable not working

I'm more of an iOS developer, so passing java variables seems to be a challenge. I've looked up passing by value and reference, none have worked for me. I can't seem to understand why. Since my application is a bit complicated to explain, I broke it down into a more simple, tangible problem. My app has an android spinner, with three categories. What I want to have happen is to simply change the text on the textview based on what the user selects. In my fragmented view, I start with the basics by calling the button and textview from the xml file.

public class AskQuestions extends Fragment {

private Button selectAnswer; 
private TextView displayAnswer;
private Spinner spinner;
public String result;

public String getResult() {
    return result;
}
public void setResult(String result) {
    this.result = result;
}

    public void onCreate(Bundle savedInstabceState){
        super.onCreate(savedInstabceState);

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        //Fetching layout
        View rootView = inflater.inflate(R.layout.fragment_questions, container, false);

        selectAnswer  = (Button) rootView.findViewById(R.id.button1);

        displayAnswer = (TextView) rootView.findViewById(R.id.textView1);

        spinner = (Spinner) rootView.findViewById(R.id.planets_spinner);




        //Setting spinner to custom class I created
        spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());



        //Button clicks when the user selects an item of the spinner, and changes the text
        //based on what item is selected.
        selectAnswer.setOnClickListener(new View.OnClickListener() {

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

                        //This is where I am confused.
                        result = getResult();

                        displayAnswer.setText(result);              

            }
        });


        return rootView;
    }



}

OK. This is primarily where my troubles are. How do I change the textview based on what the user picks from this class?

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

    //Calling object in to display in the view

    AskQuestions question = new AskQuestions();

    //When a user selects an item off of the spinner, the position changes 
    //accordingly (spinner works just fine, changing the text on the textview from this class
    //seems to be most of my trouble.
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {

    switch (pos) {
    case 0:     
        //This does not work when a user selects an item, and clicks a button to review the result
        question.setResult("You selected index zero");
        break;
    case 1:
        //Does not work
        question.setResult("You selected index one");
        break; 
    case 2: 
        //Does not work
        question.setResult("You selected index 2");
        break;
    }
    }

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

I'm not an Android developer, but you are doing:

AskQuestions question = new AskQuestions();

that creates a new AskQuestions object that is not present in your user interface, so you are changing an object that is not showing up.

You need to referenced the AskQuestions object of your interface from CustomOnItemSelectedListener in order to modify that UI object. The answer of @DerGolem shows you a way to do it ;-)

Regards,

The easiest option would be to turn the Listener into an anonymous class:

public class AskQuestions extends Fragment {

    private Button selectAnswer; 
    private TextView displayAnswer;
    private Spinner spinner;
    public String result;

    public String getResult() {
        return result;
    }
    public void setResult(String result) {
        this.result = result;
    }

    public void onCreate(Bundle savedInstabceState){
        super.onCreate(savedInstabceState);

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        //Fetching layout
        View rootView = inflater.inflate(R.layout.fragment_questions, container, false);

        selectAnswer  = (Button) rootView.findViewById(R.id.button1);

        displayAnswer = (TextView) rootView.findViewById(R.id.textView1);

        spinner = (Spinner) rootView.findViewById(R.id.planets_spinner);




        //Setting spinner to custom class I created
        spinner.setOnItemSelectedListener(new OnItemSelectedListener (){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos,
             long id) {
                switch (pos) {
                case 0:     
                    displayAnswer.setText("You selected index 0");
                    break;
                case 1:
                    displayAnswer.setText("You selected index one");
                    break; 
                case 2: 
                    displayAnswer.setText("You selected index 2");
                    break;
                }
            }

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



        //Button clicks when the user selects an item of the spinner, and changes the text
        //based on what item is selected.
        selectAnswer.setOnClickListener(new View.OnClickListener() {

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

                        //This is where I am confused.
                        result = getResult();

                        displayAnswer.setText(result);              

            }
        });


        return rootView;
    }



}

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