gpt4 book ai didi

java - 按钮 setText() 实际上不会 setText

转载 作者:行者123 更新时间:2023-12-01 09:28:50 28 4
gpt4 key购买 nike

我正在尝试将 setText() 设置为

Button btnFloor, btnTable;

这不工作 ATM,实际上我正在尝试从

发送数据

FloorsActivity -> TablesActivity -> NewOrdersActivity

那么我如何将数据从一个 Activity 传递到另一个 Activity ?

FloorsActivity.java

@Override
public void onFloorItemClicked(int id) {


Intent intent = new Intent(this, TablesActivity.class);
intent.putExtra("FloorId", id);
startActivity(intent);

Toast.makeText(this, "Floor id : " + String.valueOf(id), Toast.LENGTH_SHORT).show();

}

TablesActivity.java

int floorId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_floors_tables);
initViews();

Intent intent = getIntent();
floorId = intent.getIntExtra("FloorId", 1);
}

@Override
public void onTableItemClicked(String name) {

String floorName = "F" + floorId;

Intent intent = new Intent(this, NewOrderActivity.class);
intent.putExtra("FloorId", floorId);
intent.putExtra("TableName", name);
intent.putExtra("FloorName", floorName);
startActivity(intent);

Toast.makeText(this, "Table Name : " + String.valueOf(name), Toast.LENGTH_SHORT).show();

}

NewOrderActivity.java

String floorName;
String tableName;
int floorId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_order);
initViews();

Intent intent = getIntent();
floorName = intent.getStringExtra("FloorName");
tableName = intent.getStringExtra("TableName");
floorId = intent.getIntExtra("FloorId", 1);
}

public void initViews() {

// Fetch view
btnFloor = (Button) findViewById(R.id.btn_floor);
btnTable = (Button) findViewById(R.id.btn_table);

//Set Views

btnFloor.setText(floorName);
btnTable.setText(tableName);

btnFloor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NewOrderActivity.this, FloorsActivity.class);
startActivity(intent);
}
});

btnTable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent = new Intent(NewOrderActivity.this, TablesActivity.class);
intent.putExtra("FloorId", floorId);
startActivity(intent);

}
});

NewOrdersActivity 是我尝试设置文本的地方

这是我在 Github 上的提交 full change这是此链接 project

最佳答案

在重载 Intent extra 之前调用 initViews()

修复了NewOrderActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_order);

Intent intent = getIntent();
floorName = intent.getStringExtra("FloorName");
tableName = intent.getStringExtra("TableName");
floorId = intent.getIntExtra("FloorId", 1);
initViews();
}

关于java - 按钮 setText() 实际上不会 setText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39623813/

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