我想提出一个观点。这里有多个选项,一个选项有多个按钮。数据将从Web服务中检索。整个过程必须动态完成。
我的代码在这里,我就是这样做的,但我必须像上面的图片那样做。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option_selection);
tableLayout = (TableLayout) findViewById(R.id.linear);
int itemId = getIntent().getIntExtra("itemId", 0);
getGroupName(itemId);
}
private void getGroupName( final int itemId) {
StringRequest stringrequest = new StringRequest(Method.GET,
getString(R.string.base_url)+"ItemsOptions/GetGroupByItemID?itemID="
+ itemId, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
groupId = jsonArray.getJSONObject(i)
.getString("GroupName");
showOption(itemId,groupId);
}
//showOption(itemId,groupId);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
}
});
AppController.getInstance().addToRequestQueue(stringrequest);
}
protected void showOption(int itemId,String groupName) {
StringRequest stringrequest = new StringRequest(Method.GET,
getString(R.string.base_url)+"ItemsOptions/GetAllOptionByItemGroupID?itemID="+itemId+"&groupID="+groupName+"",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
String option = jsonArray.getJSONObject(i)
.getString("OptionsName");
if (i % 3 == 0) {
tableRow = new TableRow(
getApplicationContext());
tableLayout.addView(tableRow);
}
orderBtn = new OrderButton(
getApplicationContext());
orderBtn.setId(i);
orderBtn.setText(option);
orderBtn.setTextColor(Color
.parseColor("#FFFFFF"));
orderBtn.setBackgroundResource((R.drawable.selector));
tableRow.addView(orderBtn);
orderBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.setSelected(true);
enabledButton = orderBtn.getId();
deselectButtons();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
}
});
AppController.getInstance().addToRequestQueue(stringrequest);
答案 0 :(得分:0)
whateverbuttonclicked.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
LinearLayout layoutyouwantbuttonin = (LinearLayout)findViewById(R.id.layoutyouwantbuttonin);
Button brandnewbutton = new Button(NameofActivity.this);
String bc = whateverbuttonclicked.getText().toString();
brandnewbutton.setText(bc);
layoutyouwantbuttonin.addView(brandnewbutton);
}});