在解析之后,RecyclerView不显示来自JSON Array的数据

时间:2016-12-17 14:37:43

标签: android android-recyclerview android-volley

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

BackgroundTask.java

sudo docker build -t my-image .

MySingletone.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;
    }
}

RecyclerAdapter.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);
    }

}

3 个答案:

答案 0 :(得分:1)

尝试更改此内容:

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();
    }
}

答案 1 :(得分:1)

请求是异步的。您无法从func downloadImageForPhoto(_ photo: Photo, completionHandler: @escaping (_ success: Bool, _ errorString: String?) -> Void) { taskForGETMethod(photo.photoURL, parameters: nil, parseJSON: false) { (result, error) in if error != nil { photo.imagePath = "unavailable" completionHandler(false, "Unable to download Photo") } else { if let result = result { DispatchQueue.main.async(execute: { let fileName = (photo.photoURL as NSString).lastPathComponent let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let pathArray = [path, fileName] let fileURL = URL(fileURLWithPath: "\(pathArray)") FileManager.default.createFile(atPath: fileURL.path, contents: result as? Data, attributes: nil) photo.imagePath = fileURL.path completionHandler(true, nil) }) } else { completionHandler(false, "Unable to download Photo") } } } } 方法返回ArrayList。您必须通过调用getList填充回调方法RecyclerView中的onResponse

此外,adapter.notifyDataSetChanged()循环永远不会运行,因为while永远不会超过count;

response.length()

答案 2 :(得分:0)

您需要在更新notifyDataSetChanged();

后致电arrayList