2016-11-24 96 views
0

如果我清除列表視圖,listview將會消失。列表視圖將從for循環中獲取數據。如何清除列表視圖並添加新的列表視圖

解決方法:

  • 把setdaylist.clear()的任何環路內將清除所有列表視圖,並僅顯示一個列表項。

  • 將其設置在方法的頂部會清除所有的listview,任何解決方案?

代碼:

public void onDataGotOnline(JSONObject response) { 

    DateTime today = new DateTime().withTimeAtStartOfDay(); 
    DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"); 
    try { 
     JSONArray currentData = response.getJSONArray("list"); 
       // setdaylist.clear(); 
     for (int i = 1; i < 5; i++) { 
      DateTime tomorow = today.plusDays(i).withTimeAtStartOfDay(); 
      String dtStr = fmt.print(tomorow); 
      int info = 0; 


      for(; info<currentData.length();info++){ 
       JSONObject managedata = currentData.getJSONObject(info); 
       String time= managedata.getString("dt_txt"); 

       if(time.equals(dtStr)){ 

        int getResult=info+8; 

        double[] arrayAverageTemp = new double[8]; 
        double[] arrayMaxTemp = new double[8]; 
        double[] arrayMinTemp = new double[8]; 
        int[] arrayWheaterId = new int[8]; 

        for (int a = info; a < getResult; a++) { 
         JSONObject datalist = currentData.getJSONObject(i); 
         JSONArray weatherdata = datalist.getJSONArray("weather"); 
         JSONObject weatherdata2 = weatherdata.getJSONObject(weatherdata.length() - 1); 
         int id = weatherdata2.getInt("id"); 

         JSONObject weather = datalist.getJSONObject("main"); 
         double avg_temp = weather.getDouble("temp"); 
         double max_temp = weather.getDouble("temp_max"); 
         double min_temp = weather.getDouble("temp_min"); 


         arrayWheaterId[a-info]=id; 
         arrayAverageTemp[a-info]=avg_temp; 
         arrayMaxTemp[a-info]=max_temp; 
         arrayMinTemp[a-info]=min_temp; 
        } 


        ManageData manageData = new ManageData(); 
        double max = manageData.findMax(arrayMaxTemp); 
        double min = manageData.findMin(arrayMinTemp); 
        WheatherData day = new WheatherData(); 
        day.setDay("sunday"); 
        // int image = convert.covertImage(icon); 
        day.setImage(R.drawable.ic_image_01d); 
        day.setDescrption("asdasd"); 
        day.setAvgTemp("14 °c"); 
        day.setMaxTemp(max+ "°c"); 
        day.setMinTemp(min+ "°c"); 

        setdaylist.add(day); 


        adapter.notifyDataSetChanged(); 

        // findMax(arrayMaxTemp); 
        //findMin(arrayMinTemp); 
        //findID(arrayWheaterId); 
        //findaverage(arrayAverageTemp); 





       } 
      } 
     } 
    } 
    catch (JSONException e){ 

    } 

} 
+0

什麼時候你想清除一個列表視圖?你什麼時候想創建一個新的列表視圖? – 09Q71AO534

+0

使用'setdaylist.clear();'和'adapter.notifyDataSetChanged();'現在將數據添加到列表並調用通知 – SaravInfern

+0

@ 09Q71AO534上面的方法onDatagotOnline是inteface方法,當該方法被調用時應該是舊的listview不應該添加新的listitem – kenji

回答

0

從上面的討論。

  • 問題是由於Android studio中的緩存問題。
  • 清理項目和構建它將刪除現有的緩存。

以下所示for循環之前的setdaylist按預期工作。

public void onDataGotOnline(JSONObject response) { 
     ... 
     try { 
      ... 
      setdaylist.clear(); 
      for (int i = 1; i < 5; i++) { 
       ... 
       for(; info<currentData.length();info++){ 
        ... 
        if(time.equals(dtStr)){ 
         ... 
          setdaylist.add(day); 
         ... 
        } 
       } 
      } 
     } 
     catch (JSONException e){} 
    }