wseng92
Android Enthusiast
Good day guys. I'm new to android and now using **startActivityForResult** in my program. In my app, I have two button and two textView. The two `button` used to open the dialog. How can I check which button was pressed onActivityResult so that the TextView can be setText accordingly to the button?
So my program should work like this:
If button1 was clicked....c.setText();
If button2 was clicked....d.setText();
But the program now is **nothing display on the TextView**. Did the error came from `if (resultCode == 1)` and `else if (resultCode == 2)` ?? Thanks a lot
Code:
int a1 = 1;
int a2 = 2;
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(a1);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialogRadio(a2);
}
});
public void AlertDialogRadio(final int k) { //parameter k is never used
final CharSequence[] ClaimsModel = {"Project", "Petrol", "Car Maintenance"
, "Medical", "Other"};
AlertDialog.Builder alt_bld = new AlertDialog.Builder(getActivity());
alt_bld.setTitle("Select a Claims");
alt_bld.setSingleChoiceItems(ClaimsModel, -1, new DialogInterface
.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
Intent intent = new Intent(getActivity().getApplicationContext(), Project1.class);
startActivityForResult(intent, 0);
} else if (item == 1) {
Intent intent = new Intent(getActivity().getApplicationContext(), Petrol.class);
startActivityForResult(intent, 1);
} else if (item == 2) {
Intent intent = new Intent(getActivity().getApplicationContext(), CarMainten.class);
startActivityForResult(intent, 2);
} else if (item == 3) {
Intent intent = new Intent(getActivity().getApplicationContext(), Medical.class);
startActivityForResult(intent, 3);
} else if (item == 4) {
Intent intent = new Intent(getActivity().getApplicationContext(), Other.class);
startActivityForResult(intent, 4);
}
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 1) { // if button1 was clicked
switch (requestCode) {
case 0:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
c.setText(" " + b + "------" + "RM " + result);
Toast.makeText(getActivity(), "Not completed ", Toast.LENGTH_LONG).show();
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
c.setText(" " + b1 + "------" + "RM " + result1);
break;
case 2:
String result2 = data.getStringExtra("text");
String b2 = data.getStringExtra("a");
c.setText(" " + b2 + "------" + "RM " + result2);
break;
case 3:
String result3 = data.getStringExtra("text");
String b3 = data.getStringExtra("a");
c.setText(" " + b3 + "------" + "RM " + result3);
break;
case 4:
String result4 = data.getStringExtra("text");
String b4 = data.getStringExtra("a");
c.setText(" " + b4 + "------" + "RM " + result4);
break;
}
}
else if (resultCode == 2) { // if button2 was clicked
switch (requestCode) {
case 0:
String result = data.getStringExtra("text");
String b = data.getStringExtra("a");
d.setText(" " + b + "------" + "RM " + result);
break;
case 1:
String result1 = data.getStringExtra("text");
String b1 = data.getStringExtra("a");
d.setText(" " + b1 + "------" + "RM " + result1);
break;
case 2:
String result2 = data.getStringExtra("text");
String b2 = data.getStringExtra("a");
d.setText(" " + b2 + "------" + "RM " + result2);
break;
case 3:
String result3 = data.getStringExtra("text");
String b3 = data.getStringExtra("a");
d.setText(" " + b3 + "------" + "RM " + result3);
break;
case 4:
String result4 = data.getStringExtra("text");
String b4 = data.getStringExtra("a");
d.setText(" " + b4 + "------" + "RM " + result4);
break;
}
}
}
So my program should work like this:
If button1 was clicked....c.setText();
If button2 was clicked....d.setText();
But the program now is **nothing display on the TextView**. Did the error came from `if (resultCode == 1)` and `else if (resultCode == 2)` ?? Thanks a lot
So my program should work like this:
If button1 was clicked....c.setText();
If button2 was clicked....d.setText();
But the program now is **nothing display on the TextView**. Did the error came from `if (resultCode == 1)` and `else if (resultCode == 2)` ?? Thanks a lot