【问题标题】:RecyclerView not showing data from JSON Array after parsing解析后 RecyclerView 不显示来自 JSON 数组的数据
【发布时间】:2016-12-17 14:37:42
【问题描述】:

我正在尝试使用 Android Volley 获取 JSON 数组,但我的 RecyclerView 没有显示数据。 recyclerView 是空的。我检查了我的网址没有问题。

BackgroundTask.java

    public class BackgroundTask {

    Context context;
    String jsonURL = "my url";
    ArrayList<Contact> arrayList = new ArrayList<>();

    public BackgroundTask(Context ctx){

        this.context = ctx;
    }

    public ArrayList<Contact> getList(){

        final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, jsonURL, (String) null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        int count = 0;
                        while (count > response.length()){
                            try {
                                JSONObject jsonObject = response.getJSONObject(count);
                                Contact contact = new Contact(jsonObject.getString("Name"),jsonObject.getString("Email"));
                                arrayList.add(contact);
                                count++;

                            } catch (JSONException e) {

                                Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
                                e.printStackTrace();
                            }
                        }

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

            }
        }
        );
        Singletone.getSingletone(context).addToRequest(jsonArrayRequest);
        return arrayList;
    }
}

MySingletone.java

    public class Singletone {

    private static Singletone singletone;
    private RequestQueue requestQueue;
    private static Context context;

    private Singletone(Context ctx){
        context = ctx.getApplicationContext();
        requestQueue = getRequestQueue();
    }

    public RequestQueue getRequestQueue(){

        if(requestQueue == null){
            requestQueue = Volley.newRequestQueue(context);
        }
        return requestQueue;
    }

    public static synchronized Singletone getSingletone(Context ctx){

        if(singletone == null){
            singletone = new Singletone(ctx.getApplicationContext());
        }

        return singletone;
    }

    public<T> void addToRequest(Request<T> request){

        requestQueue.add(request);
    }

}

RecyclerAdapter.java

    public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {

    ArrayList<Contact> arrayList = new ArrayList<>();

    public RecycleAdapter(ArrayList<Contact> array){

        this.arrayList = array;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return  myViewHolder;
    }

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

        holder.Name.setText(arrayList.get(position).getName().toString());
        holder.Email.setText((arrayList.get(position).getEmail().toString()));

    }

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


    public static class MyViewHolder extends RecyclerView.ViewHolder{

        TextView Name, Email;
        public MyViewHolder(View itemView) {
            super(itemView);

            Name = (TextView) itemView.findViewById(R.id.txt_name);
            Email = (TextView) itemView.findViewById(R.id.txt_email);
        }
    }
}

【问题讨论】:

    标签: android android-recyclerview android-volley


    【解决方案1】:

    请求是异步的。您不能只从 getList 方法返回 ArrayList。您必须通过调用adapter.notifyDataSetChanged() 在回调方法onResponse 中填充RecyclerView

    你的while 循环也永远不会运行,因为count 永远不会大于response.length()

    @Override
    public void onResponse(JSONArray response) {
        if(response != null && response.length() > 0){
            for (int count = 0; count < response.length(); count++ ) {
                try {
                    JSONObject jsonObject = response.getJSONObject(count);
                    Contact contact = new Contact(jsonObject.getString("Name"), jsonObject.getString("Email"));
                    arrayList.add(contact);
                } catch (JSONException e) {
                    Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
            yourAdapter.notifyDataSetChanged();
        }
    }
    

    【讨论】:

    • 如何在onResponse()中调用adpter.notifyDataSetChanged?
    • 如何在 onResponse() 中调用 adpter.notifyDataSetChanged ?我的适配器对象在另一个活动中,我正在从该活动中调用 BackgroundTask。谢谢。
    • 使适配器对象成为全局对象。
    【解决方案2】:

    尝试改变这个:

    int count = 0;
    while (count > response.length()){
        try {
            JSONObject jsonObject = response.getJSONObject(count);
            Contact contact = new Contact(jsonObject.getString("Name"),jsonObject.getString("Email"));
            arrayList.add(contact);
            count++;
    
        } catch (JSONException e) {
    
            Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    

     for(int count=0;count<response.length();count++){
        try {
            JSONObject jsonObject = response.getJSONObject(count);
            Contact contact = new Contact(jsonObject.getString("Name"),jsonObject.getString("Email"));
            arrayList.add(contact);
    
        } catch (JSONException e) {
    
            Toast.makeText(context,"Error",Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }
    

    【讨论】:

    • @SabirHossain 不客气...不要忘记投票或接受答案
    【解决方案3】:

    更新arrayList后需要拨打notifyDataSetChanged();

    【讨论】:

      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 2013-03-21
      • 2021-08-28
      相关资源
      最近更新 更多