Q:
Android how to get the id value when click button?
I have this class called AddEditProduct:
public class AddEditProduct extends AppCompatActivity {
private EditText categoryName;
private EditText productName;
private EditText unitOfMeasure;
private EditText price;
private EditText stock;
private ImageButton button;
private long id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_product);
setTitle("Add Product");
categoryName = (EditText) findViewById(R.id.categoryName);
productName = (EditText) findViewById(R.id.productName);
unitOfMeasure = (EditText) findViewById(R.id.unitOfMeasure);
price = (EditText) findViewById(R.id.price);
stock = (EditText) findViewById(R.id.stock);
button = (ImageButton) findViewById(R.id.button);
id = 0;
Button b = (Button) findViewById(R.id.button2);
Intent intent = new Intent(AddEditProduct.this, AddEditProduct2.class);
Bundle b = new Bundle();
b.putInt("catid", id);
b.putString("name", categoryName.getText().toString());
b.putInt("num", id);
b.putInt("numofam", numOfProductName);
intent.putExtras(b);
intent.setClass(AddEditProduct.this, AddEditProduct2.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();
id = getIntent().getIntExtra("catid", 0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b.putInt("catid", id);
b.putString("name", categoryName.getText().toString());
b.putInt("num", id);
b.putInt("numofam", numOfProductName);
id = getIntent().getIntExtra("catid", 0);
Intent intent2 = new Intent(AddEditProduct.this, AddEditProduct3.class);
startActivity(intent2);
}
});
}
In this class I try to get the ID value when I click button2 but I don't know how can I do that. I already tried with Intent.getIntExtra.
AddEditProduct.xml:
AddEditProduct2.xml:
AddEditProduct3.xml:
And this is my MainActivity.java:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
new GetDataTask().execute();