Home > Software engineering >  How to change the TextView from an object?
How to change the TextView from an object?

Time:02-27

How can I use this to change the profile of a Turtle using Radio buttons? So when user selects a radio button different text should be displayed in the TextView. How can I set these objects? Currently there is no output displaying when the radio button is clicked. So I would like to get the details of the turtle from the object and set it.

     //Main Activity
     public class MainActivity extends AppCompatActivity {
    
        private RadioGroup group_turtle;
        private ImageView image_turtle;
        private TextView text_description;
    
        //create an array to store the turtle's profile
        ArrayList<Turtle> turtles = new ArrayList<>(4);
        
        //OnCreate method
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Load GUI components
            loadGUI();
            processRadioButtons();
            loadTurtles();
    
        }
    
        private void loadTurtles() {
    
            Turtle t1 = new Turtle("Leo","Leo is the heart of the team.",
                    "Loyal, brave, and responsible, Leonardo is the team leader, but not by choice.",
                    5);
    
            Turtle t2 =  new Turtle("Mike","Mikey is the jokester.",
                    "Slice of pizza in hand and shouts of \"COWABUNGA!\"",
                    4);
    
            Turtle t3 = new Turtle("Don","Donny is the thinker.",
                    "Every team needs a brain, and for the Turtles, that's Donny. He's smart and philosophical.",
                    5);
    
            Turtle t4 =  new Turtle("Raphael","Raphael is fearless.",
                    "Raphael has the confidence and charisma of a \"D\" personality",
                    4);
    
            //add the t1-t4 to the turtle list
            turtles.add(t1);
            turtles.add(t2);
            turtles.add(t3);
            turtles.add(t4);
    
            /*try to display the turtle details in the logcat
            for (Turtle t:turtles) {
                Log.d("TURTLE", t.toString());
            }*/
        }
    
        private void changeTurtle(int index) {
    
            int images[] = {
                    R.drawable.tmntleo,
                    R.drawable.tmntmike,
                    R.drawable.tmntdon,
                    R.drawable.tmntraph};
    
            //change the turtle image using setImageresource
            image_turtle.setImageResource(images[index]);
    
            //change the turtle profile
            //I'm trying to change the Turtle's text description in here which is the textview.

            String t1 = text_description.getText().toString();
            text_description.setText(t1);
    
    
        }
    
        private void processRadioButtons() {
            //set the listener
            group_turtle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup radioGroup, int id) {
    
    
                    switch (id) {
                        case R.id.radio_leo:
                            MainActivity.this.changeTurtle(0);
    
                            break;
    
                        case R.id.radio_mike:
                            MainActivity.this.changeTurtle(1);
                            break;
    
                        case R.id.radio_don:
                            MainActivity.this.changeTurtle(2);
                            break;
    
                        case R.id.radio_raph:
                            MainActivity.this.changeTurtle(3);
                            break;
                    }
                }
            });
        }
    
        private void loadGUI() {
            group_turtle = findViewById(R.id.radio_group);
            image_turtle = findViewById(R.id.imageView);
            text_description = findViewById(R.id.text_description);
        }
    }
    
    package com.example.ninjaturtle;
    
// new Turtle class
    public class Turtle {
        private String name;
        private String features;
        private String description;
        private int rating;
    
        //constructor
    
        /*public Turtle(String name, String features, String description, int rating) {
            this.name = name;
            this.features = features;
            this.description = description;
            this.rating = rating;
        }*/
    
        //setter & getter
    
        /*public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getFeatures() {
            return features;
        }
    
        public void setFeatures(String features) {
            this.features = features;
        }
    
        public String getDescription() {
            return description;
        }
    
        public void setDescription(String description) {
            this.description = description;
        }
    
        public int getRating() {
            return rating;
        }
    
        public void setRating(int rating) {
            this.rating = rating;
        }
    
        //toString method
        @Override
        public String toString() {
            return "Turtle{"  
                    "name='"   name   '\''  
                    ", description='"   description   '\''  
                    '}';
        }*/
    }


    
 

CodePudding user response:

  • Change String t1 = text_description.getText().toString();

to

     String desc= turtles.get(index).getDescription();

or to print all data

   String desc= turtles.get(index).toString();

in your changeTurtle() method

  • Related