gpt4 book ai didi

java - 我将如何使用公共(public)类将数据从一项 Activity 传输到另一项 Activity ?

转载 作者:行者123 更新时间:2023-12-02 02:42:16 26 4
gpt4 key购买 nike

我尝试创建一个公共(public)类来尝试将数据从一个 Activity 传输到另一个 Activity 。但是,当我尝试完全设置此类的信息时,我设法获取 Int 变量,但没有获取 String,当我尝试获取此数据时,它是空白的。

这是我的主要 Activity

public void toyota_yaris(View view) {
CurrentCar currentcar = new CurrentCar();
currentcar.setInfo("Toyota Yaris",130,8,1160,7);


Intent switchScreen = new Intent(MainActivity.this,CarActivity.class);
MainActivity.this.startActivity(switchScreen);
}

这是我的汽车 Activity

CurrentCar currentcar = new CurrentCar();

TextView name = (TextView) findViewById(R.id.name);
name.setText(currentcar.getName());

TextView speed = (TextView) findViewById(R.id.speed);
speed.setText(String.valueOf(currentcar.getSpeed()));

这是我的 CurrentCar 类(getter 和 setter 类)

public class CurrentCar {
private String mName;
private int mSpeed;
private int mAge;
private int mMileage;
private int mSeats;

public void setInfo(String Name,int Speed,int Age,int Mileage,int Seats ) {
mName = Name;
mSpeed = Speed;
mAge = Age;
mMileage = Mileage;
mSeats = Seats;
}
public String getName() {
return mName;
}
public int getSpeed() {
return mSpeed;
}
public int getAge() {
return mAge;
}
public int getMileage() {
return mMileage;
}
public int getSeats() {
return mSeats;
}
}

最佳答案

如果您想将数据从一个 Activity 传递到另一个 Activity ,请将其附加到 Intent 。示例-

在主 Activity 中-

Bundle bundle= new Bundle();
bundle.putString("name", "A");
bundle.putString("speed", "100");

Intent intent= new Intent(MainActivity.this,CarActivity.class);
intent.putExtras(bundle);
startActivity(intent);

车内 Activity -

Bundle bundle=getIntent().getExtras();
String name=bundle.getString("name");
String speed=bundle.getString("speed");

然后在 TextView 中设置这些值。

关于java - 我将如何使用公共(public)类将数据从一项 Activity 传输到另一项 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45252275/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com