2015-03-25 74 views
0

我建立一個Android應用程序,其中有一個主屏幕中的onCreate我初始化異步任務。在那個異步任務中,我從服務器獲取數據並使用baseAdapter顯示。如何在Android中使JSONarray null?

現在的問題是同時刷新我再次觸發的異步任務,並且因此重複我的數據,我需要的是使舊數據刪除和使用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 
     } 


    } 

回答

0

我認爲這個問題是在「currentQuestionArray」。你必須創建新的每次。就像你doInBackground method.The問題currentQuestionArray=new HashMap<String,String>()是每次新currentQuestionObject插入地圖,但舊的還剩餘。希望它可以幫助你。

+0

感謝回答所提但這不會幫我,因爲它會在分頁成爲問題。所以,我必須讓這個陣列空將刷新 – 2015-03-25 07:21:18

+0

你可以提出一個方法中,我清除舊JSONArray和重新初始化而清爽。然後激發我的異步任務 – 2015-03-25 07:36:07

+0

我所知道的只是 - 創建一個新的JSONArray。 JSONArray otherJsonArray =新JSONArray(); 或者遍歷數組並刪除(int index)索引。 http://www.json.org/javadoc/org/json/JSONArray.html#remove(int) – Soham 2015-03-25 07:39:02

0

刷新過程中創建新的適配器,也有新的陣列上方

+0

感謝它的劑量不是一個好主意,使新baseadapter和新的數組,你可以建議其他方法,使老數據刪除和存儲加載新。 – 2015-03-25 07:27:32