在我的應用程序中,這是我的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幫我
顯示你的NodeListViewCustomAdapter代碼 –
ya updated.Please檢查我的問題 – AndroidCrazy