0

Am getting the results as in dynamic buttons, but i want to display that in gridview.here is my code

JSONArray jsonMainNode1 = jsonResponse.getJSONArray("menu"); // 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();
ammu
  • 117
  • 2
  • 14

1 Answers1

0

What is the amount of data in json?
Parse json and Get your data into a model class using Gson, refer:
Unable to Loop through dynamic json string recursively in android

And pass the ArrayList of the class objects to a custom adapter for the gridview, refer:
http://www.learn-android-easily.com/2013/09/android-custom-gridview-example.html
http://www.mkyong.com/android/android-gridview-example/
http://www.technotalkative.com/android-gridview-example/

Community
  • 1
  • 1
Pararth
  • 8,114
  • 4
  • 34
  • 51