[android]Button setText() 不会不会实际上 setText

发布时间: 2016/10/10 21:26:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试着向 setText()

Button btnFloor, btnTable;

这不工作工作自动取款机,实际上 iam 试图将数据从发送

FloorsActivity-TablesActivity-NewOrdersActivity >

所以如何传递数据从 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 是在哪里 iam 试图设置文本

这里是我提交在 Github 上这充满变化在这里是链接到该项目

如果任何人就会拉-请求给与必要的代码更改,会有很大的帮助。

解决方法 1:

你叫 initViews() 你已经重载之前 intent 额外的。

固定的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();
}
赞助商