gpt4 book ai didi

android - 动态添加单选按钮单选

转载 作者:行者123 更新时间:2023-11-29 01:12:50 24 4
gpt4 key购买 nike

我无法获得动态添加到 radio group 中的 radio button 的单一选择。请帮助选择它的单选。以下是我的代码。

我的 fragment 类。

 public class fragment4 extends Fragment {
View v;
LinearLayout my_layout;
//CheckBox checkBox;
RadioButton checkBox;
JSONArray jsonArray;
Context ctx;
RadioGroup rg; /* MediaPlayer mp;*/

public fragment4(){
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// 1.

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.farg4, container, false);
my_layout = (LinearLayout)v.findViewById(R.id.my_layout);

getcategory();
return my_layout;

}

public void getcategory() {
StringRequest request = new StringRequest(Request.Method.POST,
StaticDataUtility.Server_URL + "" + StaticDataUtility.getsubcategory, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject joResp = new JSONObject(response);
ArrayList<productinfo> sliderList = new ArrayList<>();
int success = joResp.getInt("Success");
if (success == 1) {
// industryListBeanArrayList.clear();
jsonArray = joResp.getJSONArray("result");
// JSONObject joResult = joResp.getJSONObject("result");
// JSONArray jaDetail = joResult.getJSONArray("industry_detail");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject joDetail = jsonArray.getJSONObject(i);
// productinfo slider = new productinfo();
if (joDetail.has("subcat_name")) {
rg= new RadioGroup(getContext());
rg.setOrientation(RadioGroup.VERTICAL);
// TableRow row =new TableRow(getActivity());
// row.setId(i);
// row.setLayoutParams(new TableRow.LayoutParams(Toolbar.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
checkBox = new RadioButton(getActivity());
/* checkBox.setOnCheckedChangeListener(v.getContext());*/
checkBox.setId(i);
checkBox.setText(joDetail.getString("subcat_name"));
//row.addView(checkBox);
rg.addView(checkBox);
// my_layout.addView(rg);

my_layout.addView(rg);

// ((ViewGroup)v.findViewById(R.id.radgrpcat)).addView(my_layout);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
for (int p=0;p<=jsonArray.length();p++){
if(b){
String valchk=checkBox.getText().toString().trim();
FilteredBy.valchk2= new String[]{valchk};
}
}
Log.e("checkbox", String.valueOf(FilteredBy.valchk2.length));
}
});
}



}

} else {
Toast.makeText(getActivity(), "Sorry Data Are Not Available ", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},


new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() {
HashMap<String, String> params = new HashMap<>();
params.put("cat_id", "363");
Log.e("params", params.toString());
return params;
}
};

request.setRetryPolicy(new
DefaultRetryPolicy(60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adding request to request queue
Volley.newRequestQueue(getActivity()).add(request);

}

}

here showing all button is selected in image

最佳答案

正如 Mike 在评论中所说,您每次都在创建 RadioGroup。只需将以下代码放在 for 循环之前

rg= new RadioGroup(getContext());
rg.setOrientation(RadioGroup.VERTICAL);
my_layout.addView(rg);

代码应该是这样的(如果我理解正确的话):

rg= new RadioGroup(getContext());
rg.setOrientation(RadioGroup.VERTICAL);
my_layout.addView(rg);
for (int i = 0; i < jsonArray.length(); i++) {

关于android - 动态添加单选按钮单选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41735144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com