0

嗨,我无法获取 JSONObject 值标题并将其存储到数组中。

public class ViewPagerAdapter extends PagerAdapter 
{   
// JSON Node names
     private static final String TAG_CATEGORIESLIST = "categorylist";
     private static final String TAG_TITLE = "title";
     private static final String TAG_URL = "url";
     public static String[] titles;
     public final Context context;
     public int[] scrollPosition;
     JSONArray categories = null; 
     JSONObject json;
     {
         try {
                JSONFunction JSONFunction = new JSONFunction();
               json = JSONFunction.categorylist();
               // Getting Array of Categories
               categories = json.getJSONArray(TAG_CATEGORIESLIST);
                // looping through All Categories
               for(int i = 0; i < categories.length(); i++){
                JSONObject c = categories.getJSONObject(i);
                // Storing each json item in variable
                String title = c.getString(TAG_TITLE);
                String url = c.getString(TAG_URL); 
                titles = new String[] {title}; //only obtain one result
                scrollPosition = new int[titles.length];
            }

            } catch (JSONException e) {
                e.printStackTrace();
            }   
     }
...

titles只得到一个价值,需要你们的帮助。
添加在:

我正在使用 Viewpagerindicator 库,显示标题的默认代码就像 tat

私有静态字符串 [] 标题 = 新字符串 [] { "第 1 页"、"第 2 页"、"第 3 页"、"第 4 页"、"第 5 页"};

我正在尝试将 json 数据输入到那里。谢谢。

4

2 回答 2

1

你没有增加标题数组

改变这个

 titles = new String[] {title};//Because of this your last retrived  value will be stored in titles

 titles[i] = title;
于 2012-11-05T12:56:12.223 回答
0
public class ViewPagerAdapter extends PagerAdapter 
{   
// JSON Node names
     private static final String TAG_CATEGORIESLIST = "categorylist";
     private static final String TAG_TITLE = "title";
     private static final String TAG_URL = "url";
     public static String[] titles;
     public final Context context;
     public int[] scrollPosition;
     JSONArray categories = null; 
     JSONObject json;
     {
         try {
                JSONFunction JSONFunction = new JSONFunction();
               json = JSONFunction.categorylist();
               // Getting Array of Categories
               categories = json.getJSONArray(TAG_CATEGORIESLIST);
                // looping through All Categories
               for(int i = 0; i < categories.length(); i++){
                JSONObject c = categories.getJSONObject(i);
                // Storing each json item in variable
                String title = c.getString(TAG_TITLE);
                String url = c.getString(TAG_URL); 

change this line in your code 

//////////////////////////////////////

                titles[i]=title; 

///////////////////////////////////////scrollPosition = new int [标题.长度]; }

            } catch (JSONException e) {
                e.printStackTrace();
            }   
     }
于 2012-11-05T12:57:20.243 回答