2017-03-02 76 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,这可以防止你的应用程序崩溃。

我认为它会解决你的问题。