我正在尝试将setText()设置为
Button btnFloor, btnTable;
哪个无法使用ATM,实际上我正在尝试从
FloorsActivity-> TablesActivity-> NewOrdersActivity
那么我如何将数据从一个活动传递到另一个活动?
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();
}