2014-02-06 2 views
0

Am получать результаты как в динамических кнопок, но я хочу показать, что в gridview.here мой кодкак отображать результаты, которые получают из JSON в GridView

JSONArray jsonMainNode1 = jsonResponse.getJSONArray («меню») ; // JSONObject e = jsonMainNode1.getJSONObject (позиция);

   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(); 

ответ

0

Каков объем данных в json?
Анализировать JSON и получить ваши данные в классе модели с использованием Gson см:
Unable to Loop through dynamic json string recursively in android

И пройти ArrayList объектов класса для пользовательского адаптера для GridView, см:
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/

Смежные вопросы