2014-11-06 26 views
1

我使用json解析服務器的數據。然後將它們存儲在Arraylist中並將它們加載到android中的微調器中。但是,對於所有微調數據,我都獲得了相同的價值。比如我在歌廳JSON數據如下:無法加載ArrayList數據到微調在android

[{"pricing":"500,600,700,800,900,1000"}]

現在我逗號分隔條件他們,他們加入ArrayList中是這樣的:

List<String> items = Arrays.asList(Pricing.split(",")); 
for(int j=0;j<items.size();j++) 
{ 
    r.add(items.get(i)); 
} 

然後加載此ArrayList的微調。 這裏是我的全碼:

try 
       { 
        json = new JSONArray(data); 
        for (int i = 0; i < json.length(); i++) 
        { 
         JSONObject obj = json.getJSONObject(i); 
         String Pricing = obj.getString("pricing"); 

         List<String> items = Arrays.asList(Pricing.split(",")); 
         List<String> r = new ArrayList<String>(); 
         for(int j=0;j<items.size();j++) 
         { 
          r.add(items.get(i)); 
         } 
        } 

        @SuppressWarnings({ "rawtypes", "unchecked" }) 
        ArrayAdapter cd = new ArrayAdapter(this,android.R.layout.simple_spinner_item,r); 
        cd.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
        spin2.setAdapter(cd); 
} 

但微調器500 500 500 500 500 500而不是500 600 700 800 900 1000。 有什麼問題我在做這個代碼.. ??我是Android新手。請建議我解決方案。

+1

用j代替我喜歡:r.add(items.get(j)); – 2014-11-06 05:13:04

回答

2

試試這個辦法,希望這將幫助你解決你的問題。

您正在使用錯誤的索引變量(i),它用於外部循環但使用內部循環訪問數據,因此只需使用(j)索引變量替換(i)索引變量。

r.add(items.get(i)); 

更換

r.add(items.get(j)); 
+1

thanxx ...這是我的一個小小的愚蠢錯誤。 :) D – Ruchir 2014-11-06 05:18:25

+0

@ user3519241,祝你好運,但請注意下次出現此類錯誤。 – 2014-11-06 05:19:27

+0

@ user3519241,請接受任何用戶答案,它會給你第一個提示以解決你的問題。 – 2014-11-06 05:20:25

2

看來你正試圖在列表中添加相同的值,在整個列表中添加0th值,因爲索引i用於json數組,您的正確索引將是j

因此改變

r.add(items.get(i)); 

r.add(items.get(j)); 
1

嘗試改變下面的代碼:

List<String> items = Arrays.asList(Pricing.split(",")); 
         List<String> r = new ArrayList<String>(); 
         for(int j=0;j<items.size();j++) 
         { 
          r.add(items.get(i)); 
         } 

通過

List<String> items = Arrays.asList(Pricing.split(",")); 
         List<String> r = new ArrayList<String>(); 
         for(int j=0;j<items.size();j++) 
         { 
          r.add(items.get(j)); 
         } 

因爲u使用我,而不是j的。

+0

希望這可以幫助你! – 2014-11-06 05:15:44