在我的应用程序中,这是我的活动。在计时器中我正在更新listview.My listview即使在notifyDataSetChanged()方法之后也没有更新。
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中逐一添加它然后更新。请帮助我
答案 0 :(得分: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();
}
答案 1 :(得分:0)
您正在构造函数中传递数据
mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus);
如果您使用mNodeList.notifyDataSetChanged();
,那么您的数据将在构造函数&amp;中使用。数据不会改变,这就是为什么会出现旧数据。
刷新listview有两种方法。
1)再次使用SetAdaper而不是notidyDataSetChanged()
mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus);
mListView.setAdapter(mNodeList);
<强> 2)notidyDataSetChanged()强>
- 不要在构造函数
中传递数据 - 宣布所有全球数据&amp;使用它在NodeListViewCustomAdapter
- 更新全球数据&amp;使用notidyDataSetChanged()
答案 2 :(得分:0)
使用此:
mNodeList = new NodeListViewCustomAdapter(getParent(), mNodes, mSentStatus, mRecvStatus);
mListView.setAdapter(mNodeList);
而不是:
mNodeList.notifyDataSetChanged();
感谢。