1
I have a layout where I can only have one button and need to use the same layout for various events onClick in a cardView.
How do I get this?
public void onClick(View arg0) {
switch (arg0.getContext()) {
case getClass().getMethod(onActivityResult(2,2,null),MainActivity.class): {
String value = editText.getText().toString();
Intent intent = new Intent();
intent.putExtra("VALUE", value);
setResult(2, intent);
finish();
}
case onActivityResult(3, 3, null): {
String value = editText.getText().toString();
Intent intent = new Intent();
intent.putExtra("VALUE", value);
setResult(3, intent);
finish();
}
case onActivityResult(4, 4, null) {
String value = editText.getText().toString();
Intent intent = new Intent();
intent.putExtra("VALUE", value);
setResult(4, intent);
finish();
}
}
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==2) {
String value = data.getStringExtra("VALUE");
SharedPreferences sharedpreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("str_textview", value);
editor.apply();
textView1.setText(value);
} else if(requestCode==3) {
String value = data.getStringExtra("VALUE");
SharedPreferences sharedpreferences = getSharedPreferences("pref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("str_textview", value);
editor.apply();
textView2.setText(value);
}
...
public void onClickSaldo(View arg0) {
Intent intent=new Intent(this,StarterActivity.class);
startActivityForResult(intent, 2);
}
public void onClickDizimo(View arg0) {
Intent intent=new Intent(this,MainActivity.CashActivity.class);
startActivityForResult(intent, 3);
}
public void onClickMercado(View arg0) {
Intent intent=new Intent(this,MainActivity.CashActivity.class);
startActivityForResult(intent, 4);
}
...