Ryuji89_pb

can't redraw views/button

Jun 7th, 2011
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.26 KB | None | 0 0
  1. package elf.app;
  2.  
  3. import elf.app.comm.CommClient;
  4. import elf.app.comm.CommListener;
  5. import android.app.Activity;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13.     /**
  14.      * @author Ryuji89
  15.      * Displays details about the selected room/entry.
  16.      */
  17. public class RoomInfoActivity extends Activity implements OnClickListener, CommListener {
  18.    
  19.         // Views used as GUI-components
  20.     TextView rumInfo;
  21.     Button buttonCleaned;
  22.     TextView rumStatus;
  23.    
  24.         // comm - CommClient
  25.         // cleaned - indicates whether the room is 'cleaned' or 'uncleaned'
  26.         // entry - the name of the room
  27.         // entryID - the ID of the room/entry
  28.         // dcCounter - counter, when 10 is reached it disconnects comm, see dcCounterIncr()
  29.     CommClient comm;
  30.     boolean cleaned;
  31.     String entry = "";
  32.     int entryID = 0;
  33.     int dcCounter = 0;
  34.    
  35.  
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.             // calls the onCreate method of the superclass
  38.             // and sets the layout
  39.         Log.v("***DEV***", "RoomInfoActivity: onCreate()");
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.room_info);
  42.        
  43.             // creates the CommClient
  44.             // fetches the ip and port stored in the Extras of the Intent
  45.             // sets the CommClient as a listener for this activity
  46.             // starts it in a separate thread
  47.         comm = new CommClient(  getIntent().getExtras().getString("ip"),
  48.                                 getIntent().getExtras().getInt("port") );
  49.         comm.setListener(this);
  50.         new Thread(comm).start();
  51.        
  52.             // Explicit casting of the layout components into reference variables
  53.         rumInfo = (TextView)findViewById(R.id.textInfo);
  54.         buttonCleaned = (Button)findViewById(R.id.buttonCleaned);
  55.         rumStatus = (TextView)findViewById(R.id.textStatus);
  56.        
  57.        
  58.             // fetch the entry info
  59.         entry = getIntent().getExtras().getString("entry");
  60.  
  61.             // set the text of the rumInfo view
  62.         rumInfo.setText(entry);
  63.        
  64.             // fetch the entry ID
  65.         entryID = getIntent().getExtras().getInt("entryID");
  66.         Log.v("***DEV***", "RoomInfoActivity2: "+"entryID: "+entryID);
  67.  
  68.  
  69.  
  70.             // sets an clicklistener for the button
  71.         buttonCleaned.setOnClickListener(new OnClickListener() {
  72.            
  73.             public void onClick(View v) {
  74.                 if (v.equals(buttonCleaned)) {
  75.                     if (cleaned == true) {
  76.                         comm.send("setCleaned0"+entryID);
  77.                         try {
  78.                             Thread.sleep(500);
  79.                         } catch (InterruptedException e) {
  80.                             e.printStackTrace();
  81.                         }
  82.                     } else {
  83.                         comm.send("setCleaned1"+entryID);
  84.                         try {
  85.                             Thread.sleep(500);
  86.                         } catch (InterruptedException e) {
  87.                             e.printStackTrace();
  88.                         }
  89.                     }
  90.                 }
  91.             }
  92.         });
  93.     }
  94.  
  95.    
  96.         // handles incomming messages
  97.     public void receiveMessage(String IP, String message, int id) {
  98.             // displays the incomming message in the LogCat
  99.         Log.v("***DEV***", "RoomInfoActivity: receivedMessage: "+message);
  100.        
  101.             // when comm is created, this message is received
  102.         if (message.equals("Connection established")) {
  103.             Log.v("***DEV***", "RoomInfoActivity: connection established");
  104.            
  105.                 // send a query of whether the room is cleaned or not
  106.             comm.send("checkCleaned"+entryID);
  107.             Log.v("***DEV***", "RoomInfoActivity: comm.send(checkCleaned"+entryID+");");
  108.         }
  109.  
  110.             // when I receive cleaned1 from the server
  111.         if(message.equals("cleaned1")) {
  112.             Log.v("***DEV***", "RoomInfoActivity: cleaned1");
  113.             cleaned = true;
  114.             rumStatus.setText("Status: Städat");
  115.             buttonCleaned.setText("Färdig med städningen");
  116.             redraw();
  117.         }
  118.             // when I receive cleaned0 from the server
  119.         if(message.equals("cleaned0")) {
  120.             Log.v("***DEV***", "RoomInfoActivity: cleaned0");
  121.             cleaned = false;
  122.             rumStatus.setText("Status: Ej städat");
  123.             buttonCleaned.setText("Ej färdig med städningen");
  124.             redraw();
  125.         }
  126.        
  127.             // FIXME disconnects comm if it receives 10 null
  128.         if(message.equals(null)) {
  129.             dcCounterIncr();
  130.         }
  131.     }
  132.  
  133.     public void onClick(View v) {
  134.         // has to be included, otherwise Eclipse glitches..
  135.     }
  136.    
  137.         // have to use this one to re-draw/render the views?
  138.     public void redraw() {
  139.         rumInfo.invalidate();
  140.         buttonCleaned.invalidate();
  141.         rumStatus.invalidate();
  142.     }
  143.    
  144.     public void dcCounterIncr() {
  145.         dcCounter++;
  146.         if(dcCounter>9)
  147.             comm.disconnect();
  148.     }
  149.  
  150.  
  151. }
Advertisement
Add Comment
Please, Sign In to add comment