按钮setText()实际上不会设置文本

时间:2016-09-21 18:18:20

标签: java android android-studio

我想将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

的链接

1 个答案:

答案 0 :(得分:1)

在您超出意图额外费用之前,请致电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();
}