2017-03-02 77 views
-4

我想把一個json數組放入Recyclerview。當我只有一個論據(store_textposition)時,一切正常,但當我添加另一個論證(store_name)時,它給了我"Wrong 1st argument type. Found 'String' , required Integer"錯誤的第一個參數類型。發現'字符串',需要整數

這是我的JSON數組:

{"action":"true","error":"","data":[{"_id":"58ad8d8ca49d0e11e21c4504","store_name":"firstStore","store_view":0,"store_textposition":null}]} 

並沒有在那裏我得到的錯誤:

private boolean parse() 
{ 
    try 
    { 
     JSONObject obj = new JSONObject(jsonData); 
     JSONArray ja = obj.getJSONArray("data"); 
     JSONObject jo; 
     shops.clear(); 
     for(int i=0;i<ja.length();i++) 
     { 
      jo=ja.getJSONObject(i); 
      String store_name = jo.getString("store_name"); 
      String store_textposition = jo.getString("store_textposition"); 
      shops.add(store_textposition,store_name); 
     } 
     return true; 
    } 
    catch (JSONException e) 
    { 
     e.printStackTrace(); 
     return false; 
    } 
} 
+1

我不明白。這裏不清楚什麼?該錯誤明確指出,第一個參數需要是一個整數,但你的是一個字符串。 – Tom

+0

什麼類型有「商店」對象? – Ircover

+1

我會假設'商店'是一個'列表'。我也會假設你正在試圖在列表中添加兩個字符串。做到這一點的正確方法是每次調用add()兩次,每次String。編譯器認爲你正在嘗試使用方法add(index,item),它將String項添加到指定的索引。 –

回答

0

如@AlinPandichi在評論中提到,你的問題是,當你添加對象去購物清單。

我不知道你想要做什麼。但是,如果store_textposition是int,則最好使用

int store_textposition = jo.optInt(「store_textposition」);

這樣,即使服務器傳遞喲空你有默認值0,這可以防止你的應用程序崩潰。

我認爲它會解決你的問題。