您好我无法获取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库,显示标题的默认代码就像是
private static String [] titles = new String [] {&#34; Page 1&#34;,&#34; Page 2&#34;, &#34; Page 3&#34;,&#34; Page 4&#34;,&#34; Page 5&#34;};
我正在尝试将json数据输入到那里。 感谢。
答案 0 :(得分:1)
你没有递增标题数组
更改此
titles = new String[] {title};//Because of this your last retrived value will be stored in titles
到
titles[i] = title;
答案 1 :(得分: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
////////////////////////////////////////////// < / p>
titles[i]=title;
////////////////////////////////////////////// scrollPosition = new int [titles.length]; }
} catch (JSONException e) {
e.printStackTrace();
}
}