Sooraj Sizon Sooraj Sizon - 2 months ago 11
Android Question

Button setText() wouldnt wouldnt actually setText

i am trying to setText() to

Button btnFloor, btnTable;


Which isn't working working ATM , actually iam trying to send data from

FloorsActivity -> TablesActivity -> NewOrdersActivity

So how i pass Data from a activity to another ?

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 is where iam trying to set text

Here is my commit on Github for this full change
Here is the link to this project

If anyone would pull-request to me with necessary code change that would be of a lot of help .

Answer

You call initViews() before you've overloaded the intent extra's.

Fixed 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();
}
Comments