tags:

views:

14

answers:

0

Hello all i am new to android. Udp data is coming like the following format. Id:data 120:10 130:23 120:12 122:13 134:12 Data will come frequently when the data is coming I have to show it on the screen. Once we receive the id we should display id with data if the same id comes again we should update the data for the particular id. My code is below

 private HashMap hashmap = new HashMap(); 
 private Set set;
 private Iterator itr;
 ::::::::
 ::::::::::

      try {
       ds = new DatagramSocket(SERVERPORT);
       //Log.d("MY UDP ","After socket create");
       while (!shutdownRequested) {
        DatagramPacket p = new DatagramPacket(buffer, buffer.length);
        ds.receive(p);        
        recdata=new String(p.getData(),0,p.getLength());
        id=recdata.substring(0, snf_detail.indexOf(":"));
        data=recdata.substring(snf_detail.indexOf(":")+1);
        hashmap.put(id, data);                           
                         set = hashmap.entrySet(); 
                         // Get an iterator 
                         itr = set.iterator();   
                         //new Thread(new hashprinter()).start(); 
         }  
       }

             new Thread(new hashprinter()).start();
      }
      catch(Exception e){} 
      ::::::::
      ::::::::::::

       public class hashprinter implements Runnable{

         public void run() {
         handler.post(new Runnable() {
          @Override
          public void run() {
           textview_tagid.setText("");
           textview_roomid.setText(""); 
           while(itr.hasNext()) { 
            Map.Entry me = (Map.Entry)itr.next();           
            textview_id.append("\n"+me.getKey()+"\n");
            textview_data.append("\n"+me.getValue()+"\n");
            }
           }});  
         }

When I call the thread within the loop I got the expected output. But some times it gives the blank textview because of I set texview.setText(“”)

If I call the thread outside the loop it append data repeatedly like the following

120:10

120:10 130:23

120:10 130:23 120:12 ........ .............

How to solve this?