我得到的結果作爲動態按鈕,但我想顯示在gridview.here是我的代碼如何顯示的結果,從JSON獲取到的GridView
JSONArray jsonMainNode1 = jsonResponse.getJSONArray(「菜單」) ; // JSONObject e = jsonMainNode1.getJSONObject(position);
int lengthJsonArr = jsonMainNode1.length();
for(int i=0; i <lengthJsonArr; i++)
{
JSONObject jsonChildNode = jsonMainNode1.getJSONObject(i);
String Pid = jsonChildNode.optString("pid".toString());
String Name = jsonChildNode.optString("name").toString();
String Refid=jsonChildNode.optString("refid".toString());
String resName = jsonChildNode.getString("image_url");
OutputData = Name;
str_id = Pid;
// OutputData += "pid : "+ Pid +" "+ "content : "+ Name+" ";
LinearLayout buttonContainer=(LinearLayout)findViewById(R.id.btn_container);
Button button = new Button(buttonContainer.getContext());
button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ViewGroup.LayoutParams params = button.getLayoutParams();
button.setLayoutParams(params);
//Button new width
params.width = 64;
params.height = 64;
button.setText(OutputData);
int resId = getResources().getIdentifier(resName, "drawable", getPackageName());
button.setBackgroundResource(resId);
button.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
button.setTextSize(13);
}
button.setTag(Refid);
button.setTextColor(Color.parseColor("#FF0000"));
buttonContainer.addView(button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout buttonContainer = (LinearLayout)findViewById(R.id.btn_container);
buttonContainer.removeAllViews();