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唯一の価値は、uみんなからの助けが必要です。
アドオン:

Viewpagerindicatorライブラリを使用しています。タイトルを表示するためのデフォルトのコードはtatのようなものです。

private static String [] titles = new String [] {"ページ1"、 "ページ2"、 "ページ3"、 "ページ4"、"ページ5"};

そこにjsonデータを入力しようとしています。ありがとう。

4

2 に答える 2

1

titles配列をインクリメントしていません

これを変える

 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 [titles.length]; }

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