好吧我有兩個textview「名稱」和「ID」,我想從recyclerview和JSON填充他們。 我可以在Android Monitor中獲取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"}]
有什麼問題?我該如何解決它?請幫忙!
打印'listItems.size()'在第一 –
你試過: http://stackoverflow.com/questions/43303817/recyclerview-not-showing-anything – CyberClaw
在這裏把你的JSON響應,我認爲JSON解析是錯的 –