2012-12-27 68 views
1

在我的應用程序中,這是我的activity.Inside計時器我正在更新listview.My listview甚至沒有更新notifyDataSetChanged()方法後。ListView沒有更新onNotifyDataSetChanged()

protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nodes); 
     mListView = (ListView)findViewById(R.id.node_list); 
     mNodes = new ArrayList<String>(); 
     mSentStatus = new ArrayList<String>(); 
     mRecvStatus = new ArrayList<String>(); 
     doTimerTask(); 
     mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus); 
     mListView.setAdapter(mNodeList); 
     mListView.setOnItemClickListener(this); 
    } 
    private void doTimerTask() { 
     mTimer = new Timer(); 
     mTimer.scheduleAtFixedRate(new TimerTask() { 

      @Override 
      public void run() { 

       runOnUiThread(new Runnable() { 
        public void run() { 
         mNodes = getNodeListfromPeard(); 
         mSentStatus = getSentStatusFromPeard(); 
         mRecvStatus = getRecvStatusFromPeard(); 
         mNodeList.notifyDataSetChanged(); 
        } 
       }); 
      } 
     }, delay, period); 

    } 
    protected ArrayList<String> getRecvStatusFromPeard() { 

     ArrayList<String> recvStatusList = new ArrayList<String>(); 
     recvStatusList.add("|"); 
     recvStatusList.add("||"); 
     return recvStatusList; 
    } 
    protected ArrayList<String> getSentStatusFromPeard() { 
     ArrayList<String> sentStatusList = new ArrayList<String>(); 
     sentStatusList.add("|"); 
     sentStatusList.add("||"); 
     return sentStatusList; 
    } 
    protected ArrayList<String> getNodeListfromPeard() { 

    ArrayList<String> nodeList = new ArrayList<String>(); 
    nodeList.add("node1"); 
    nodeList.add("node2"); 
     return nodeList; 
    } 


public class NodeListViewCustomAdapter extends BaseAdapter { 
    LayoutInflater inflater; 
    private ArrayList<String> mNodeTitle; 
    Activity mActivity; 
    private ArrayList<String> mSentStatus; 
    private ArrayList<String> mRecvStatus; 

    public NodeListViewCustomAdapter(Activity aContext,ArrayList<String> aTitle,ArrayList<String> sentStatus,ArrayList<String> recvStatus){ 
     super(); 
     mActivity = aContext; 
     this.mNodeTitle = aTitle; 
     this.mSentStatus = sentStatus; 
     this.mRecvStatus = recvStatus; 
     this.inflater = (LayoutInflater)aContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 
    @Override 
    public int getCount() { 
     return mNodeTitle.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 
    public static class ViewHolder 
    { 
     TextView txtViewnodeTitle; 
     TextView sentStatustxtView; 
     TextView recvStatustxtView; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder; 
     if(convertView==null) 
     { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.nodelistitem_row, null); 

      holder.txtViewnodeTitle = (TextView) convertView.findViewById(R.id.txtViewnodeTitle); 
      holder.sentStatustxtView = (TextView) convertView.findViewById(R.id.sentstatustxtView); 
      holder.recvStatustxtView = (TextView) convertView.findViewById(R.id.recvstatustextView); 
      convertView.setTag(holder); 
     } 
     else{ 
      holder=(ViewHolder)convertView.getTag(); 
     } 

     holder.txtViewnodeTitle.setText(mNodeTitle.get(position)); 
     holder.sentStatustxtView.setText(mSentStatus.get(position)); 
     holder.recvStatustxtView.setText(mRecvStatus.get(position)); 

     return convertView; 
    } 

} 

但是,當我在ArrayList中添加一個個那麼它updating.Please幫我

+0

顯示你的NodeListViewCustomAdapter代碼 –

+0

ya updated.Please檢查我的問題 – AndroidCrazy

回答

1

改變你的runOnUiThread爲波紋管

 runOnUiThread(new Runnable() { 
         public void run() { 
          mNodes = getNodeListfromPeard(); 
          mSentStatus = getSentStatusFromPeard(); 
          mRecvStatus = getRecvStatusFromPeard(); 
          mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus); 
mListView.setAdapter(mNodeList); 
         } 
        }); 

超越控制你的notifyDatasetChanged方法

public void notifyDatasetChanged(){ 

     this.mNodeTitle = ActivityName.this.mNodes; 
     this.mSentStatus = ActivityName.this.mSentStatus; 
     this.mRecvStatus = ActivityName.this.mRecvStatus; 
     super.notifyDatasetChanged(); 

    } 
+0

感謝您的旅遊答案。我想刷新我的看法,而不是再次重繪,因爲它是在裏面timer.Is有一種方法嗎? – AndroidCrazy

+0

更新我的答案檢查現在 –

+0

Ya感謝它像魅力 – AndroidCrazy

1

你是通過你的數據在構造

mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus); 

如果要使用mNodeList.notifyDataSetChanged();那麼你的數據將會使用你已經在構造函數&中傳遞的數據不會改變,那爲什麼會出現舊數據。

有兩種方法可以刷新listview。

1)SetAdaper的再次代替notidyDataSetChanged()

mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus); 
mListView.setAdapter(mNodeList); 

2)notidyDataSetChanged()在構造

--Declare所有

--Don't通數據全局數據&使用說明在列表視圖中顯示NodeListViewCustomAdapter

--update全局數據&適配器使用notidyDataSetChanged()

+0

我試過第一種方式,它工作正常,但這將是一個開銷,因爲每5秒將被調用。但在第二種方式,我將如何通過我沒有構造函數的CustomAdpater的值? – AndroidCrazy

+0

聲明全局像靜態 –

+0

根據要求,我應該避免使用靜態如果可能。是否有可能? – AndroidCrazy

0

使用此:

mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus); 
mListView.setAdapter(mNodeList); 

相反的:

mNodeList.notifyDataSetChanged(); 

感謝。

+0

感謝您的answer.it工作正常,但它會是一個開銷,因爲這將是在計時器中,每隔5秒會進行一次? – AndroidCrazy

+0

@AndroidCrazy不完全。你是這樣做的,這不會有任何開銷。 –

+0

@AndroidCrazy如果我的解決方案對你有幫助,那麼你也可以投票。 :) –