【问题标题】:How to make JSONarray null in android?如何在android中使JSONarray为空?
【发布时间】:2015-03-25 07:09:00
【问题描述】:

我正在构建一个 android 应用程序,其中有一个主屏幕,其中 onCreate 我正在初始化异步任务。在那个异步任务中,我从服务器获取数据并使用 baseAdapter 显示。

现在的问题是刷新时我再次触发异步任务并且它复制了我的数据所以,我需要使用 BaseAdapter 删除旧数据并在列表中插入新数据。

这里是做异步任务 -

 /**
 * Async task class to get json by making HTTP call
 */
private class questionfeed_async extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

    }


    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        ServiceHandler http = new ServiceHandler();
        jsonString = http.makeServiceCall(url, ServiceHandler.GET,
                null);
        if (jsonString != null) {
            try {
                SCROLL_TO_POSITION = currentQuestionArray.length();
                questions = new JSONArray(jsonString);
                //result_text = new String[questions.length()];

                for (int i = 0; i < questions.length(); i++) {
                    temp_obj = questions.getJSONObject(i);


                    JSONObject currentQuestionObject = new JSONObject();

                    currentQuestionObject.put("question_text", temp_obj.getString(Tag_questionText)
                            .toString());

                    currentQuestionArray.put(currentQuestionObject);

                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        } else {
            Log.d("TAG", "Empty");
        }
        return null;
    }


    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


            adapter = new BaseAdapterFeed(getActivity(), currentQuestionArray);

            questionfeed_customelistview.setAdapter(adapter);               

        } catch (NullPointerException e) {
            // probably don't bother doing clean up
        } finally {
            // carry on as if nothing went wrong
        }


    }

【问题讨论】:

    标签: android asynchronous pull-to-refresh


    【解决方案1】:

    我认为问题出在“currentQuestionArray”中。你必须每次都创建新的。就像你的 doInBackground 方法中的currentQuestionArray=new HashMap&lt;String,String&gt;() 一样。问题是每次在地图中插入一个新的 currentQuestionObject 但旧的仍然存在。希望它可以帮助你。

    【讨论】:

    • 感谢您的回复,但这对我没有帮助,因为它会成为分页问题。所以我让这个数组为空会刷新
    • 你能建议一种方法来清除旧的 JSONArray 并在刷新时再次初始化。然后触发我的异步任务
    • 我所知道的是-----只需创建一个新的 JSONArray。 JSONArray otherJsonArray = new JSONArray();或者遍历数组并删除(int index)索引。 json.org/javadoc/org/json/JSONArray.html#remove(int)
    【解决方案2】:

    在刷新期间创建新的适配器,以及上面提到的新数组

    【讨论】:

    • 谢谢,制作新的baseadapter和新的数组不是一个好主意,你能建议其他方法来删除旧数据并重新存储加载吗?
    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2013-06-12
    • 2012-07-14
    • 2011-05-27
    相关资源
    最近更新 更多