如何从对象更改 TextView?

How to change the TextView from an object?

我如何使用它来使用单选按钮更改海龟的配置文件?因此,当用户选择单选按钮时,TextView 中应显示不同的文本。如何设置这些对象?当前单击单选按钮时没有输出显示。所以我想从对象中获取海龟的详细信息并设置它。

     //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 + '\'' +
                    '}';
        }*/
    }


    
 
  • 改变String t1 = text_description.getText().toString();

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

或打印所有数据

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

在您的 changeTurtle() 方法中