2017-05-30 47 views
0
private void parseProductOptionData(JSONObject response){ 
    try { 
     boolean success = response.getBoolean("success"); 
     if(success){ 
      rlProductOption.setVisibility(View.GONE); 
      //Get All the Categories 
      JSONArray data = response.getJSONArray("data"); 
      if(data.length()!=0){ 
       rlProductOptionQuantityTxt.setVisibility(View.VISIBLE); 
       for(int i= 0;i<data.length();i++){ 
        //First Get Type 
        JSONObject optionQuantity = data.getJSONObject(i); 
        Log.d(TAG, "onResponse: option quantity"+optionQuantity); 


        //Create a Layout 
        llMainLayout = new LinearLayout(getActivity()); 
        llMainLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
        llMainLayout.setBackgroundResource(R.drawable.border_details); 
        llMainLayout.setOrientation(LinearLayout.VERTICAL); 
        llMainLayout.setPadding(10,0,10,5); 
        llProductOptionsSheet.addView(llMainLayout); 



        tvOptionTitleText= new TextView(getActivity()); 
        tvOptionTitleText.setId(Integer.valueOf("1"+i)); 
        tvOptionTitleText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
        tvOptionTitleText.setTypeface(Typeface.SERIF); 
        llMainLayout.addView(tvOptionTitleText); 
        tvOptionTitleText.setText(optionQuantity.getString("title")); 

        if(optionQuantity.getString("min_selection").equals("1") && optionQuantity.getString("max_selection").equals("1")){ 
         RadioGroup maxAndMin = new RadioGroup(getActivity()); 
         maxAndMin.setOrientation(LinearLayout.VERTICAL); 
         maxAndMin.clearCheck(); 

         //maxAndMin.setId(Integer.valueOf("1")); 
         maxAndMin.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
         Log.d(TAG, "Option length1 "+optionQuantity.getJSONArray("options").length()); 
         for (int btnCount =0;btnCount<optionQuantity.getJSONArray("options").length();btnCount++){ 
          RadioButton rdbtn = new RadioButton(getActivity()); 
          //rdbtn.setId(i); 
          //if (btnCount==0) 
          rdbtn.setText(optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name")+" ("+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("price")+")"); 
          maxAndMin.addView(rdbtn); 
         } 
         llMainLayout.addView(maxAndMin); 
        }else if(optionQuantity.getString("min_selection").equals("0") && optionQuantity.getString("max_selection").equals("1")){ 
         RadioGroup maxAndMin = new RadioGroup(getActivity()); 
         maxAndMin.setOrientation(LinearLayout.VERTICAL); 
         maxAndMin.clearCheck(); 
         //maxAndMin.setId(Integer.valueOf("1")); 
         maxAndMin.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
         Log.d(TAG, "Option length1 "+optionQuantity.getJSONArray("options").length()); 
         for (int btnCount =0;btnCount<optionQuantity.getJSONArray("options").length();btnCount++){ 
          RadioButton rdbtn = new RadioButton(getActivity()); 
          //rdbtn.setId(i); 
          rdbtn.setText(optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name")+" ("+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("price")+")"); 
          maxAndMin.addView(rdbtn); 
         } 
         llMainLayout.addView(maxAndMin); 
        }else{ 
         Log.d(TAG, "Option length2 "+optionQuantity.getJSONArray("options").length()); 
         for (int btnCount =0;btnCount<optionQuantity.getJSONArray("options").length();btnCount++){ 
          Log.d(TAG, "Option data fetch "+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name")); 
          CheckBox cbMaxOption = new CheckBox(getActivity()); 
          cbMaxOption.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
          cbMaxOption.setText(optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("name")+" ("+optionQuantity.getJSONArray("options").getJSONObject(btnCount).getString("price")+")"); 
          llMainLayout.addView(cbMaxOption); 
         } 


        } 
       } 

      }else{ 
       Log.d(TAG, "No product option "); 
       rlProductOptionQuantityTxt.setVisibility(View.VISIBLE); 
      } 

      btnAddToCart = new Button(getActivity()); 
      btnAddToCart.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT)); 
      btnAddToCart.setBackgroundColor(getResources().getColor(R.color.color_btn_login)); 
      btnAddToCart.setText(getActivity().getResources().getString(R.string.add_to_cart)); 
      btnAddToCart.setTypeface(Typeface.SERIF); 
      btnAddToCart.setTextColor(Color.WHITE); 
      llProductOptionsSheet.addView(btnAddToCart); 
      btnAddToCart.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        tvOptionTitleText.setError("please fill all the text"); 
       } 
      }); 

     }else{ 
      rlProductOption.setVisibility(View.GONE); 
      Toast.makeText(getActivity(),response.getString("message"),Toast.LENGTH_SHORT).show(); 
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     Toast.makeText(getActivity(),"Sorry try Later!!!",Toast.LENGTH_SHORT).show(); 
     rlProductOption.setVisibility(View.GONE); 
    } 
} 

編程方式添加這是我的代碼在我所添加根據JSON data.So單選按鈕,複選框,當我在一個按鈕「btnAddToCart」單擊然後選擇我想知道所有選中的複選框和單選按鈕。因爲我必須將所有選定的數據存儲在我的會話中。如何獲得時選和單選按鈕的Android

回答

0

聲明像一個全局變量:

INT rlProductOptionQuantityTxt = 0; //爲默認情況下

使用開關的情況下,

@Override 公共無效的onClick(查看視圖) { 開關(view.getId())

{

ca SE R.id.yourRadioButton:

  rlProductOptionQuantityTxt =1; 

}

}

,然後就可以直接使用全局變量的值。

SetGet.setProductOptionQuantityTxt(rlProductOptionQuantityTxt);

0
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId(); 
View radioButton = radioButtonGroup.findViewById(radioButtonID); 
int idx = radioButtonGroup.indexOfChild(radioButton);