[英]how to get which is selected when Checkbox and Radio button added programmatically in android
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);
}
}
This is my code in which i have add radio button and checkbox according to json data.So when i click on a button "btnAddToCart" then i want to know all the selected checkbox and radio button.beacuse i have to store all the selected data in my session. 这是我的代码,我根据json数据添加单选按钮和复选框。所以当我点击按钮“btnAddToCart”然后我想知道所有选中的复选框和单选按钮。我必须存储所有选定的数据在我的会议中。
Declare a global variable like: 声明一个全局变量,如:
int rlProductOptionQuantityTxt =0;// for default case int rlProductOptionQuantityTxt = 0; //默认情况下
Use Switch case, 使用Switch case,
@Override public void onClick(View view) { switch (view.getId()) @Override public void onClick(查看视图){switch(view.getId())
{ {
case R.id.yourRadioButton: 案例R.id.yourRadioButton:
rlProductOptionQuantityTxt =1;
} }
} }
and then you can directly use the value of your global variable. 然后你可以直接使用全局变量的值。
SetGet.setProductOptionQuantityTxt(rlProductOptionQuantityTxt ); SetGet.setProductOptionQuantityTxt(rlProductOptionQuantityTxt);
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.