JSONObject和for循环在RecycleView中不起作用

时间:2018-08-02 11:06:41

标签: java android for-loop

我正在尝试使用JSON和for循环从数据库中提取Geofence名称列表,但它并未在RecycleView上添加任何内容。

但是,如果我手动添加示例注释mTextNames.add("HARRY POTTER")之类的字符串,它显示就很好。

private void initGeofenceNames(){

    final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, HttpUrl, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {

            for(int i = 0; i < response.length(); i++){

                try {
                    JSONObject jsonObject = response.getJSONObject(i);
                    Log.d(TAG, "GeofenceManageListActivity: jsonObject called");

                    mGeofenceNames.add(jsonObject.getString("geof_name"));

                    for (String s : mGeofenceNames){
                        mTextNames.add(s.toString());
                    }

                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "GeofenceManageListActivity: " + error.toString());
        }
    });
    requestQueue.add(jsonArrayRequest);

    //mTextNames.add("HARRY POTTER");

    initRecyclerView();
}

编辑:

private void initRecyclerView(){
    RecyclerView recyclerView = findViewById(R.id.geofenceManagementListRecyclerView);
    GeofenceManagementListRecyclerViewAdapter adapter = new GeofenceManagementListRecyclerViewAdapter(mTextNames, this );
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
}

3 个答案:

答案 0 :(得分:2)

用作方法

 private void initGeofenceNames(){

        final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, HttpUrl, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                for(int i = 0; i < response.length(); i++){

                    try {
                        JSONObject jsonObject = response.getJSONObject(i);
                        Log.d(TAG, "GeofenceManageListActivity: jsonObject called");

                        mGeofenceNames.add(jsonObject.getString("geof_name"));

                        for (String s : mGeofenceNames){
                            mTextNames.add(s.toString());
                        }
    initRecyclerView();

                    } catch (JSONException e) {
                        e.printStackTrace();

                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "GeofenceManageListActivity: " + error.toString());
            }
        });
        requestQueue.add(jsonArrayRequest);

        //mTextNames.add("HARRY POTTER");


    }

第二个选项 创建可验证的类级别适配器

private GeofenceManagementListRecyclerViewAdapter adapter;
private void initRecyclerView(){
    RecyclerView recyclerView = findViewById(R.id.geofenceManagementListRecyclerView);
    adapter = new GeofenceManagementListRecyclerViewAdapter(mTextNames, this );
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

private void initGeofenceNames(){

    final JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, HttpUrl, null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {

            for(int i = 0; i < response.length(); i++){

                try {
                    JSONObject jsonObject = response.getJSONObject(i);
                    Log.d(TAG, "GeofenceManageListActivity: jsonObject called");

                    mGeofenceNames.add(jsonObject.getString("geof_name"));

                    for (String s : mGeofenceNames){
                        mTextNames.add(s.toString());
                    }
adapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "GeofenceManageListActivity: " + error.toString());
        }
    });
    requestQueue.add(jsonArrayRequest);

    //mTextNames.add("HARRY POTTER");

    initRecyclerView();
}

答案 1 :(得分:0)

尝试将“ initRecyclerView()”放在onResponse内

              try {
                JSONObject jsonObject = response.getJSONObject(i);
                Log.d(TAG, "GeofenceManageListActivity: jsonObject called");

              mGeofenceNames.add(jsonObject.getString("geof_name"));

                for (String s : mGeofenceNames){
                    mTextNames.add(s.toString());
                }

                 initRecyclerView();

            } catch (JSONException e) {
                e.printStackTrace();

答案 2 :(得分:0)

像下面一样添加adapter.notifyDataSetChanged();

 @Override
    public void onResponse(JSONArray response) {

        for(int i = 0; i < response.length(); i++){

            try {
                JSONObject jsonObject = response.getJSONObject(i);
                Log.d(TAG, "GeofenceManageListActivity: jsonObject called");

                mGeofenceNames.add(jsonObject.getString("geof_name"));

                for (String s : mGeofenceNames){
                    mTextNames.add(s.toString());
                }

            } catch (JSONException e) {
                e.printStackTrace();

            }
        }
        adapter.notifyDataSetChanged(); //change here

    }
},