RecyclerView没有显示任何内容

时间:2017-04-27 10:48:48

标签: java android json android-studio android-volley

好的我有两个textview" name"和" id"我想从recyclerview和json填充它们。 我可以在Android Monitor中获取json,但它在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"}]

问题是什么?我该怎么办呢?请帮忙!

2 个答案:

答案 0 :(得分:2)

你的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);

        }

答案 1 :(得分:0)

你的json字符串无效.Json字符串总是以JsonObject开头而不是JsonArray ex。{&#34; ss&#34;:[]}

请检查您的json是否有效here

有效的json如下

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

创建你的json Pojo类here

请检查:

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

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