2013-10-13 70 views
0

我必須每3秒刷新一次listview(我在ListFragment中),所以我開始一個新的Thread,啓動runOnUiThread來編輯UI。BaseAdapter notifyDataSetChanged()不更新對象引用

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    new Thread(new Runnable() { 
     public void run() { 
      do{ 
       ObjectMapper mapper = new ObjectMapper(); 
       VideoData element = null; 
       try { 
        element = mapper.readValue(new URL("http://192.168.4.111:3232/videodata.json").openStream(), VideoData.class); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       final VideoData newData = element; 
       getActivity().runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         hd = newData.getChildren().get(0).getChildren().get(id); 
         vba.notifyDataSetChanged(); 
        } 
       }); 
       try { 
        Thread.sleep(3*1000); 
       } catch (InterruptedException e) {} 
      }while(true); 
     } 
    }).start(); 
    setAdapter(); 
} 

我在網上解析數據,並更新引用。

private void setAdapter() 
{ 
    if(id == 0) 
     hd = hd.getChildren().get(0); 
    vba = new ValueBaseAdapter(getActivity(), 0, hd.getChildren()); 
    setListAdapter(vba); 
} 

我看到,當notifyDataSetChanged被調用時,getView被稱爲(和它的確定),但我已經老了的參考,當我打電話setAdapter第一次的一個。 另外如果我在run()中設置null hd,不會改變任何東西,listview不會改變。 錯誤在哪裏?謝謝。

回答

0

看來notifyDataSetChanged()僅在您使用List時才更新適配器的數據

相關問題