2017-04-27 3 views
1

저는 textview "name"과 "id"두 개를 가지고 있으며 recyclerview와 json에서 채우고 싶습니다. Android 모니터에서 json을 얻을 수 있지만 recyclerview에는 아무 것도 표시되지 않습니다! 여기 RecyclerView는 아무 것도 표시하지 않습니다

내 MainActivity.java입니다 :

txt = (TextView) findViewById(R.id.name); 
    idd = (TextView) findViewById(R.id.id); 
    recyclerView = (RecyclerView) findViewById(R.id.recyclerview); 
    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new 
    LinearLayoutManager(getApplicationContext())); 
    listItems = new ArrayList<>(); 
    loadRecyclerViewData(); 
    } 
    public void loadRecyclerViewData() 
{ 
    final String URL_DATA = "my url"; 
    final StringRequest stringRequest = new StringRequest(Request.Method.GET, URL_DATA, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String s) { 
      try { 
       JSONObject jsonObject = new JSONObject(s); 
       JSONArray array = jsonObject.getJSONArray(""); 

       for(int i=0;i<array.length();i++) 
       { 
        JSONObject o = array.getJSONObject(i); 
        ListItem_Ostan item = new ListItem_Ostan(o.getString("name"),o.getString("id")); 
        listItems.add(item); 
       } 
       adapter = new MyAdapter_Ostan(listItems,getApplicationContext()); 
       recyclerView.setAdapter(adapter); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 


       } 
      }) 
     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); 
    requestQueue.add(stringRequest); 
} 

여기 내 ListItem_ostan.java입니다 :

public class ListItem_Ostan { 
private String id,name; 

    public ListItem_Ostan(String id, String name) 
    { 
     this.id = id; 
     this.name = name; 
    } 

    public String getId() { 
     return id; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

그리고 여기 내 MyAdapter_ostan.java입니다 : 여기

public class MyAdapter_Ostan extends RecyclerView.Adapter<MyAdapter_Ostan.ViewHolder> { 
private List<ListItem_Ostan> listItems; 
Context context; 

public MyAdapter_Ostan(List<ListItem_Ostan> listItems, Context context) { 
    this.listItems = listItems; 
    this.context = context; 
} 


@Override 
public MyAdapter_Ostan.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.layouts_ostan, parent, false); 
    return new ViewHolder(v); 
} 


@Override 
public void onBindViewHolder(final MyAdapter_Ostan.ViewHolder holder, int position) { 
    final ListItem_Ostan listItem = listItems.get(position); 
    LinearLayout linearLayout = holder.linearLayout; 
    holder.id.setText(listItem.getId()); 
    holder.name.setText(listItem.getName()); 
    final CardView cardView = holder.cardView; 

} 
@Override 
public int getItemCount() { 
    return listItems.size(); 
} 

public class ViewHolder extends RecyclerView.ViewHolder { 
    TextView name,id; 
    LinearLayout linearLayout; 
    ArrayList<ListItem_Ostan> listItem = new ArrayList<ListItem_Ostan>(); 
    CardView cardView; 
    Context context; 
    public TextView getName() { 
     return name; 
    } 



    public ViewHolder(View itemView) 
    { 
     super(itemView); 
     cardView = (CardView)itemView.findViewById(R.id.card_view); 
     name = (TextView)itemView.findViewById(R.id.name); 
     id = (TextView)itemView.findViewById(R.id.id); 
    } 

및 내 json 응답 :

Value [{"id":"1","name":"Sydney"},{"id":"2","name":"Shiraz"},{"id":"3","name":"Tokyo"},{"id":"4","name":"Tehran"}] 

무엇이 문제입니까? 내가 어떻게 고칠 수 있니? 도와주세요!

+0

인쇄'listItems.size()'첫 –

+0

에서 당신이 시도 유무 : http://stackoverflow.com/questions/43303817/recyclerview-not-showing-anything – CyberClaw

+0

여기 JSON 응답을 넣고 난 JSON 파싱을 생각한다 잘못되었습니다 –

답변

1

귀하의 JSON

[{ "ID": "1", "이름": "시드니"}, { "ID": "2", "이름": "쉬라즈"}, { "ID": "3", "이름": "도쿄"}, { "ID": "4", "이름": "테헤란"}]이 방법

  JSONArray jArray = new JSONArray(s); 

      for(i=0; i < jArray.length(); i++) 
      { 

      JSONObject jObject = jArray.getJSONObject(i); 
      String name = jObject.getString("name"); 
      String id= jObject.getString("id"); 
      System.out.println("id: " + i + " IntelliJ Amiya " + jObject.getString("id")); 
      ListItem_Ostan item = new ListItem_Ostan(name,id); 
      listItems.add(item); 

     } 
을 시도 할 수 있습니다

0

json 문자열이 유효하지 않습니다 .Json 문자열은 항상 JsonArray가 아닌 JsonObject로 시작합니다. { "ss": []}

here

확인하시기 바랍니다 6,

당신의 JSON이 유효한지 확인하십시오 또는

{ 
    "array_name": [{ 
     "id": "1", 
     "name": "Sydney" 
    }, { 
     "id": "2", 
     "name": "Shiraz" 
    }, { 
     "id": "3", 
     "name": "Tokyo" 
    }, { 
     "id": "4", 
     "name": "Tehran" 
    }] 
} 

이하로하는 것은 당신의 JSON은 POJO 클래스 생성과 같은하지 here

유효한 JSON :

JSONObject jsonObject = new JSONObject(s); 
       JSONArray array = jsonObject.getJSONArray(""); 

JSONObject jsonObject = new JSONObject(s); 
       JSONArray array = jsonObject.getJSONArray("array_name"); 
+0

위의 업데이트 된 답변을 확인하십시오. –

+0

sorry.getJSONArray' 배열 이름이 없으며 Json이 유효합니다 .http : //jsonviewer.stack.hu/ –

관련 문제