recyclerview没有显示片段中的任何内容

时间:2017-07-11 07:47:54

标签: android json android-recyclerview android-volley android-glide

大家好,我有一个卡片视图,从json填充它的数据。我用排球解析json并且它是成功的。 但在卡片视图中没有任何显示

这是我的json

{"photo":[{"id":"4","profile_id":"mahdi","caption":"hello","image_url":"https:\/\/hemispheric-suggest.000webhostapp.com\/img\/img\/birjand.jpg","profile_img_url":"https:\/\/hemispheric-suggest.000webhostapp.com\/prof\/prof\/prof_1.jpg","insert_time":"2017-07-09 11:21:44"}

和我的片段适配器:

private RecyclerView recyclerView;
private RequestQueue mRequestQueue;
ArrayList<Card> list = new ArrayList<>();

String url = "My url";
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frag,container,false);
    recyclerView = (RecyclerView) view.findViewById(R.id.recycler);

    mRequestQueue = Volley.newRequestQueue(getActivity());

    list();

    LinearLayoutManager MyLayoutManager = new LinearLayoutManager(getActivity());
    MyLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setAdapter(new Adapter(list));
    return view;
}


public class Adapter extends RecyclerView.Adapter<Adapter.MyviewHolder>{

   // private ArrayList<Card> list;

    public Adapter(ArrayList<Card> data) {
        list = data;
    }

    public class MyviewHolder extends RecyclerView.ViewHolder{

        public TextView textView;
        public ImageView imageView;
        public MyviewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.textView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
        }
    }

    @Override
    public MyviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_row,parent,false);
        return new MyviewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyviewHolder holder, int position) {

        Card card = list.get(position);
        holder.textView.setText(card.getName());
        Glide.with(getActivity()).load(card.getImg()).into(holder.imageView);

    }



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

public void list() {

    JsonObjectRequest object = new JsonObjectRequest(Request.Method.GET, url,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try{

                        JSONArray array = response.getJSONArray("photo");
                        for (int i = 0; i <array.length() ; i++) {
                            JSONObject temp = array.getJSONObject(i);
                            Card card = new Card();
                            card.setName(temp.getString("profile_id"));
                            card.setImg(temp.getString("image_url"));
                            list.add(card);
                        }


                    }catch (Exception ex){
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }
    );
    mRequestQueue.add(object);
}
}

1 个答案:

答案 0 :(得分:0)

Volley是异步的,所以当你打电话时:

recyclerView.setAdapter(new Adapter(list));

在您排队请求后几毫秒,列表仍然是空的,因为Volley还没有收到任何回复。一旦在onResponse回调中获得响应,就应该设置适配器。