I am developing an app, In this i want to save radiogroup selected values to server with some temp defined variable i.e i want to pass values instead of selected text. example: if i selected value "NO" then it pass 0 as parameter to url if i selected value "Yes" then it pass 1 as parameter to url
Following is my code please suggest me how do i do this on button click all radio group selected values should send to url?
http://www.test.com/Android/api/savemedicalDetails?problemid=1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28&problemstatus=1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4&userID=129 In above format my url should send data to server.In following code my problem is radiogroup selected some values passes null values.
//activity
public class MyMedicalHistoryActivity2 extends AppCompatActivity {
Toolbar toolbar;
RadioGroup rg1, rg2, rg3, rg4, rg5, rg6, rg7, rg8, rg9, rg10,
rg11, rg12, rg13, rg14, rg15, rg16, rg17, rg18, rg19, rg20,
rg21, rg22, rg23, rg24, rg25, rg26, rg27, rg28;
String [] que;
String que1, que2, que3, que4, que5, que6, que7, que8, que9, que10, que11, que12, que13, que14, que15, que16, que17, que18, que19, que20, que21,
que22, que23, que24, que25, que26, que27, que28;
TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10,
tv11, tv12, tv13, tv14, tv15, tv16, tv17, tv18, tv19, tv20,
tv21, tv22, tv23, tv24, tv25, tv26, tv27, tv28;
TextView user_id;
String [] ans;
String srb1, srb2, srb3, srb4, srb5, srb6, srb7, srb8, srb9, srb10,
srb11, srb12, srb13, srb14, srb15, srb16, srb17, srb18, srb19, srb20, srb21, srb22, srb23, srb24, srb25, srb26, srb27, srb28;
Button btnSave;
private static String url = null;
private boolean isLoggedIn = false;
SessionManagement session;
String sessionid;
String email,userID;
JSONArray user = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_medical_history1);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
TextView toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbarTitle.setText("My Medical History");
toolbar.setTitleMarginBottom(50);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
btnSave= (Button)findViewById(R.id.btn_save);
user_id = (TextView)findViewById(R.id.user_id);
SharedPreferences sharedPreferences = getSharedPreferences(SessionManagement.PREF_NAME, Context.MODE_PRIVATE);
isLoggedIn = sharedPreferences.getBoolean(SessionManagement.IS_LOGIN, false);
String userID1 = sharedPreferences.getString(SessionManagement.KEY_SESSIONID, "Not Available");
user_id.setText(userID1);
rg1=(RadioGroup)findViewById(R.id.radioGroup1);
rg2=(RadioGroup)findViewById(R.id.radioGroup2);
rg3=(RadioGroup)findViewById(R.id.radioGroup3);
rg4=(RadioGroup)findViewById(R.id.radioGroup4);
rg5=(RadioGroup)findViewById(R.id.radioGroup5);
rg6=(RadioGroup)findViewById(R.id.radioGroup6);
rg7=(RadioGroup)findViewById(R.id.radioGroup7);
rg8=(RadioGroup)findViewById(R.id.radioGroup8);
rg9=(RadioGroup)findViewById(R.id.radioGroup9);
rg10=(RadioGroup)findViewById(R.id.radioGroup10);
rg11=(RadioGroup)findViewById(R.id.radioGroup11);
rg12=(RadioGroup)findViewById(R.id.radioGroup12);
rg13=(RadioGroup)findViewById(R.id.radioGroup13);
rg14=(RadioGroup)findViewById(R.id.radioGroup14);
rg15=(RadioGroup)findViewById(R.id.radioGroup15);
rg16=(RadioGroup)findViewById(R.id.radioGroup16);
rg17=(RadioGroup)findViewById(R.id.radioGroup17);
rg18=(RadioGroup)findViewById(R.id.radioGroup18);
rg19=(RadioGroup)findViewById(R.id.radioGroup19);
rg20=(RadioGroup)findViewById(R.id.radioGroup20);
rg21=(RadioGroup)findViewById(R.id.radioGroup21);
rg22=(RadioGroup)findViewById(R.id.radioGroup22);
rg23=(RadioGroup)findViewById(R.id.radioGroup23);
rg24=(RadioGroup)findViewById(R.id.radioGroup24);
rg25=(RadioGroup)findViewById(R.id.radioGroup25);
rg26=(RadioGroup)findViewById(R.id.radioGroup26);
rg27=(RadioGroup)findViewById(R.id.radioGroup27);
rg28=(RadioGroup)findViewById(R.id.radioGroup28);
tv1= (TextView)findViewById(R.id.id1);
tv2= (TextView)findViewById(R.id.id2);
tv3= (TextView)findViewById(R.id.id3);
tv4= (TextView)findViewById(R.id.id4);
tv5= (TextView)findViewById(R.id.id5);
tv6= (TextView)findViewById(R.id.id6);
tv7= (TextView)findViewById(R.id.id7);
tv8= (TextView)findViewById(R.id.id8);
tv9= (TextView)findViewById(R.id.id9);
tv10= (TextView)findViewById(R.id.id10);
tv11= (TextView)findViewById(R.id.id11);
tv12= (TextView)findViewById(R.id.id12);
tv13= (TextView)findViewById(R.id.id13);
tv14= (TextView)findViewById(R.id.id14);
tv15= (TextView)findViewById(R.id.id15);
tv16= (TextView)findViewById(R.id.id16);
tv17= (TextView)findViewById(R.id.id17);
tv18= (TextView)findViewById(R.id.id18);
tv19= (TextView)findViewById(R.id.id19);
tv20= (TextView)findViewById(R.id.id20);
tv21= (TextView)findViewById(R.id.id21);
tv22= (TextView)findViewById(R.id.id22);
tv23= (TextView)findViewById(R.id.id23);
tv24= (TextView)findViewById(R.id.id24);
tv25= (TextView)findViewById(R.id.id25);
tv26= (TextView)findViewById(R.id.id26);
tv27= (TextView)findViewById(R.id.id27);
tv28= (TextView)findViewById(R.id.id28);
que1= tv1.getText().toString();
que2= tv2.getText().toString();
que3= tv3.getText().toString();
que4= tv4.getText().toString();
que5= tv5.getText().toString();
que6= tv6.getText().toString();
que7= tv7.getText().toString();
que8= tv8.getText().toString();
que9= tv9.getText().toString();
que10= tv10.getText().toString();
que11= tv11.getText().toString();
que12= tv12.getText().toString();
que13= tv13.getText().toString();
que14= tv14.getText().toString();
que15= tv15.getText().toString();
que16= tv16.getText().toString();
que17= tv17.getText().toString();
que18= tv18.getText().toString();
que19= tv19.getText().toString();
que20= tv20.getText().toString();
que21= tv21.getText().toString();
que22= tv22.getText().toString();
que23= tv23.getText().toString();
que24= tv24.getText().toString();
que25= tv25.getText().toString();
que26= tv26.getText().toString();
que27= tv27.getText().toString();
que28= tv28.getText().toString();
que= new String[] { que1, que2, que3, que4, que5, que6, que7, que8, que9, que10, que11,
que12, que13, que14, que15, que16, que17, que18, que19, que20, que21,
que22, que23, que24, que25, que26, que27, que28 };
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selectedIdGender1 = rg1.getCheckedRadioButtonId();
RadioButton rb1 = (RadioButton) findViewById(selectedIdGender1);
String msrb1 = rb1.getText().toString();
String srb1= null;
if (msrb1.equals("No")) {
srb1 = String.valueOf(1);
} else if (msrb1.equals("Sometimes")) {
srb1 = String.valueOf(2);
} else if (msrb1.equals("Often")) {
srb1 = String.valueOf(3);
} else if (msrb1.equals("Always")) {
srb1 = String.valueOf(4);
}
int selectedIdGender2 = rg2.getCheckedRadioButtonId();
RadioButton rb2 = (RadioButton) findViewById(selectedIdGender2);
String msrb2 = rb2.getText().toString();
String srb2=null;
if(msrb2.equals("No")){
srb2= String.valueOf(1);
}
else if(msrb2.equals("Within 1 Years")){
srb2= String.valueOf(2);
}
else if(msrb2.equals("Often")){
srb2= String.valueOf(3);
}
else if(msrb2.equals("Always")){
srb2= String.valueOf(4);
}
int selectedIdGender3 = rg3.getCheckedRadioButtonId();
RadioButton rb3 = (RadioButton) findViewById(selectedIdGender3);
String msrb3 = rb3.getText().toString();
String srb3=null;
if(msrb3.equals("No")){
srb3= String.valueOf(1);
}else if(msrb3.equals("Within 1 Years")){
srb3= String.valueOf(2);
}
else if(msrb3.equals("Within 2 Years")){
srb3= String.valueOf(3);
}
else if(msrb3.equals("More than 1 Years")){
srb2= String.valueOf(4);
}
int selectedIdGender4 = rg4.getCheckedRadioButtonId();
RadioButton rb4 = (RadioButton) findViewById(selectedIdGender4);
String msrb4 = rb4.getText().toString();
String srb4=null;
if(msrb4.equals("No")){
srb4= String.valueOf(1);
}else if(msrb4.equals("Within 1 Years")){
srb4= String.valueOf(2);
}
else if(msrb4.equals("Within 2 Years")){
srb4= String.valueOf(3);
}
else if(msrb4.equals("More than 1 Years")){
srb4= String.valueOf(4);
}
int selectedIdGender5 = rg5.getCheckedRadioButtonId();
RadioButton rb5 = (RadioButton) findViewById(selectedIdGender5);
String msrb5 = rb5.getText().toString();
String srb5=null;
if(msrb5.equals("No")){
srb5= String.valueOf(1);
}else if(msrb5.equals("Mild")){
srb5= String.valueOf(2);
}
else if(msrb5.equals("Moderate")){
srb5= String.valueOf(3);
}
else if(msrb5.equals("severe")){
srb5= String.valueOf(4);
}
int selectedIdGender6 = rg6.getCheckedRadioButtonId();
RadioButton rb6 = (RadioButton) findViewById(selectedIdGender6);
String msrb6 = rb6.getText().toString();
String srb6=null;
if(msrb6.equals("No")){
srb6= String.valueOf(1);
}else if(msrb6.equals("Mild")){
srb6= String.valueOf(2);
}
else if(msrb6.equals("Moderate")){
srb6= String.valueOf(3);
}
else if(msrb6.equals("severe")){
srb6= String.valueOf(4);
}
int selectedIdGender7 = rg7.getCheckedRadioButtonId();
RadioButton rb7 = (RadioButton) findViewById(selectedIdGender7);
String msrb7 = rb7.getText().toString();
String srb7=null;
if(msrb7.equals("No")){
srb7= String.valueOf(1);
}else if(msrb7.equals("Mild")){
srb7= String.valueOf(2);
}
else if(msrb7.equals("Moderate")){
srb7= String.valueOf(3);
}
else if(msrb7.equals("severe")){
srb7= String.valueOf(4);
}
int selectedIdGender8 = rg8.getCheckedRadioButtonId();
RadioButton rb8 = (RadioButton) findViewById(selectedIdGender8);
String msrb8 = rb8.getText().toString();
String srb8=null;
if(msrb8.equals("No")){
srb8= String.valueOf(1);
}else if(msrb8.equals("Mild")){
srb8= String.valueOf(2);
}
else if(msrb8.equals("Moderate")){
srb8= String.valueOf(3);
}
else if(msrb8.equals("severe")){
srb8= String.valueOf(4);
}
int selectedIdGender9 = rg9.getCheckedRadioButtonId();
RadioButton rb9 = (RadioButton) findViewById(selectedIdGender9);
String msrb9 = rb9.getText().toString();
String srb9=null;
if(msrb9.equals("No")){
srb9= String.valueOf(1);
}else if(msrb9.equals("Mild")){
srb9= String.valueOf(2);
}
else if(msrb9.equals("Moderate")){
srb9= String.valueOf(3);
}
else if(msrb9.equals("severe")){
srb9= String.valueOf(4);
}
int selectedIdGender10 = rg10.getCheckedRadioButtonId();
RadioButton rb10 = (RadioButton) findViewById(selectedIdGender10);
String msrb10 = rb10.getText().toString();
String srb10=null;
if(msrb10.equals("No")){
srb10= String.valueOf(1);
}else if(msrb10.equals("Mild")){
srb10= String.valueOf(2);
}
else if(msrb10.equals("Moderate")){
srb10= String.valueOf(3);
}
else if(msrb10.equals("severe")){
srb10= String.valueOf(4);
}
//upto 28 selected
int selectedIdGender28 = rg28.getCheckedRadioButtonId();
RadioButton rb28 = (RadioButton) findViewById(selectedIdGender28);
String msrb28 = rb28.getText().toString();
String srb28=null;
if(msrb28.equals("No")){
srb28= String.valueOf(1);
}else if(msrb28.equals("Mild")){
srb28= String.valueOf(2);
}
else if(msrb28.equals("Moderate")){
srb28= String.valueOf(3);
}
else if(msrb28.equals("severe")){
srb28= String.valueOf(4);
}
ans = new String[]{ srb1, srb2, srb3, srb4, srb5, srb6, srb7, srb8, srb9, srb10,
srb11, srb12, srb13, srb14, srb15, srb16, srb17, srb18, srb19, srb20,
srb21, srb22, srb23, srb24, srb25, srb26, srb27, srb28 };
// String userID = Integer.parseInt(user_id.getText().toString());
int userID = Integer.parseInt(user_id.getText().toString());
// userID = String.valueOf(19161);
String que1 = convertArrayToString(que);
String ans1 = convertArrayToString(ans);
// String ans1 = URLEncoder.encode(convertArrayToString(ans), "utf-8");
if(isLoggedIn) {
connectWithHttpGet(userID, que1, ans1);
Intent intent = new Intent(MyMedicalHistoryActivity2.this, AsanasActivity.class);
startActivity(intent);
}
}
});
}
//insert string array to db
public static String strSeparator = ",";
public static String convertArrayToString(String[] array){
String str = "";
for (int i = 0;i<array.length; i++) {
str = str+array[i];
// Do not append comma at the end of last element
if(i<array.length-1){
str = str+strSeparator;
}
}
return str;
}
private void connectWithHttpGet(int userID, String que, String ans) {
class HttpGetAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
int paramUid = Integer.parseInt(params[0]);
String paramQue = params[1];
String paramAns = params[2];
System.out.println("Que ID:" + paramQue + " Ans is :" + paramAns );
// Create an intermediate to connect with the Internet
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.test.com/Android/api/savemedicalDetails?userID="+paramUid+
"&problemid="+paramQue+ "&problemstatus="+paramAns);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
System.out.println("httpResponse// getEntity() ; obtains the message entity of this response");
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
System.out.println("Returninge of doInBackground :" + stringBuilder.toString());
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println("Exceptionrates caz of httpResponse :" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Secondption generates caz of httpResponse :" + ioe);
ioe.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("httpResponse :" + result);
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray login = jsonObject.getJSONArray("savemedicaldetails");
JSONObject jsonObject1 = login.getJSONObject(0);
String responsetypes = jsonObject1.optString("responsetype");
// String responsetypes = jsonObject1.getString("responsetype");
String message = jsonObject1.getString("message");
if (TextUtils.equals(responsetypes, "success")) {
Toast.makeText(getApplicationContext(), message , Toast.LENGTH_LONG).show();
} else if (TextUtils.equals(responsetypes, "failure")) {
Toast.makeText(getApplicationContext(), message , Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
// Initialize the AsyncTask class
HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();
httpGetAsyncTask.execute(String.valueOf(userID), que, ans);
}
}
I'd do the following:
For every radio button I would add a Tag. So for the radio button which says "No", I'd set the tag to 1. For the "sometimes", I'd set the tag to 2. And so on.
Then, if every RadioGroup is under the same parent, I'd iterate through the parent's children and get the radio button's tags. Something like this:
You can set tags to your views.
In xml:
Or in code: