在我的应用程序中,我使用listview显示来自CustomAdapter的数据。在我的arraylist中我有两个项目。我的listview显示最后添加的数据两次而不是第一项。 这是我的自定义适配器:
public class NodeListViewCustomAdapter extends BaseAdapter {
LayoutInflater inflater;
Activity mActivity;
ArrayList<Node> mNodeList;
double mRecvProgress;
double mSentProgress;
String repeatSentprogress;
String repeatRecvprogress;
ViewHolder holder;
Node node;
public NodeListViewCustomAdapter(Activity aContext,Node node,ArrayList<Node> nodeList){
super();
mActivity = aContext;
this.node = node;
this.inflater = (LayoutInflater)aContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.mNodeList = nodeList;
}
@Override
public int getCount() {
return mNodeList.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;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public static class ViewHolder
{
TextView txtViewnodeTitle;
TextView sentStatustxtView;
TextView recvStatustxtView;
TextView msgsentvalue;
TextView msgrecvvalue;
TextView sentindicator;
TextView recvindicator;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.nodelistitem_row, null);
} else{
holder=(ViewHolder)convertView.getTag();
}
if(node.advertise)
convertView.setBackgroundColor(Color.YELLOW);
node.advertise = false;
convertView.setBackgroundColor(Color.BLACK);
holder.txtViewnodeTitle = (TextView) convertView.findViewById(R.id.txtViewnodeTitle);
holder.sentStatustxtView = (TextView) convertView.findViewById(R.id.sentstatustxtView);
holder.recvStatustxtView = (TextView) convertView.findViewById(R.id.recvstatustextView);
holder.msgsentvalue = (TextView) convertView.findViewById(R.id.msgsenttitle);
holder.msgrecvvalue = (TextView) convertView.findViewById(R.id.msgrecvtitle);
convertView.setTag(holder);
holder.sentStatustxtView.setGravity(Gravity.LEFT);
holder.recvStatustxtView.setGravity(Gravity.LEFT);
computeProgress();
holder.txtViewnodeTitle.setText(String.valueOf(node.nodeID));
holder.msgsentvalue.setText("< "+String.valueOf(node.ms)+"/"+node.MS);
holder.sentStatustxtView.setText(repeatSentprogress);
holder.msgrecvvalue.setText("> "+String.valueOf(node.mr)+"/"+node.MR);
holder.recvStatustxtView.setText(repeatRecvprogress);
return convertView;
}
private void computeProgress() {
mSentProgress = ((double)node.ms/(double)node.MS)*20;
mRecvProgress = ((double)node.mr/(double)node.MR)*20;
repeatSentprogress = new String(new char[(int)mSentProgress]).replace('\0', (char) 9608);
repeatRecvprogress = new String(new char[(int)mRecvProgress]).replace('\0', (char) 9608);
if(node.ms == node.MS)holder.sentStatustxtView.setTextColor(Color.GREEN);
else holder.sentStatustxtView.setTextColor(Color.BLUE);
if(node.mr == node.MR)holder.recvStatustxtView.setTextColor(Color.GREEN);
else holder.recvStatustxtView.setTextColor(Color.BLUE);
}
}
这是我的活动:
public class Nodes extends Activity implements OnItemClickListener {
ListView mListView;
NodeListViewCustomAdapter mNodeListAdapter;
public ArrayList <Node> mNodesList ;
TextView nodeIdView;
Timer mTimer;
Timer mInnerTimer;
int delay = 500;
int period = 500;
boolean found = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.nodes);
mListView = (ListView)findViewById(R.id.node_list);
mNodesList = new ArrayList<Node>();
doTimerTask();
mListView.setOnItemClickListener(this);
}
private void doTimerTask() {
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
while(true){
try{
String PeardOutput = null;
synchronized(AndroidPeardServer.nodestatsqe){
PeardOutput = AndroidPeardServer.nodestatsqe.poll();
if (PeardOutput == null) return;
}
String msg = PeardOutput.substring(3);
process_nodeStats(msg);
mNodeListAdapter.notifyDataSetChanged();
}
catch(Exception e){
e.printStackTrace();
}
}
}
});
}
}, delay, period);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
private void process_nodeStats(String msg) {
String[] nodeStats = msg.split(":");
String type = nodeStats[0];
int nodeID = Integer.parseInt(nodeStats[1]);
int value = Integer.parseInt(nodeStats[2]);
int cycle = Integer.parseInt(nodeStats[3]);
// Iterate through the nodelist
found = false;
for(Node node: mNodesList){
if (node.nodeID != nodeID) {
continue;
}
else found = true;
populateNode(node,type, nodeID, value, cycle);
mNodeListAdapter = new NodeListViewCustomAdapter(getParent(),node,mNodesList);
mListView.setAdapter(mNodeListAdapter);
break;
}
if (!found){
Node node = new Node();
mNodesList.add(node);
populateNode(node,type,nodeID,value,cycle);
mNodeListAdapter = new NodeListViewCustomAdapter(getParent(),node,mNodesList);
mListView.setAdapter(mNodeListAdapter);
}
}
private void populateNode(Node node,String type,int nodeId, int value, int cycle) {
node.nodeID = nodeId;
if(type.equals("MS")){
node.MS = value;
node.CS = cycle;
node.ms = 0;
}else if(type.equals("MR")){
node.MR = value;
node.CR = cycle;
node.mr = 0;
}else if(type.equals("ms")){
if(cycle==node.CS) node.ms = + value;
else node.ms = value;
}else if(type.equals("mr")){
if(cycle==node.CR) node.mr = + value;
else node.mr = value;
}else if (type.equals("NL")){
mNodesList.remove(node);
}else if(type.equals("NA")){
node.advertise = true;
}
}
//========= Node class to store in NodeList =================
public class Node {
int nodeID;
int MS; //Total Messages to Send to nodeID
int ms; //number of Messages sent so far
int MR ; //Total Messages expected from nodeID
int mr; //number of Messages received so far
int CS; //Investigation Cycle for Sending
int CR; //Investigation Cycle for Receiving
boolean advertise = false; // To know the advertisements received
}
}
答案 0 :(得分:0)
我终于把山雀弄好了。我没有从arraylist那里取出节点。所以这条线为我完成了。
node = mNodeList.get(position);
谢谢大家。