2011-08-18 27 views
0
////// SeatLayout.java /////////// 

public class SeatLayoutActivity extends Activity 
{ 
    private TableLayout mineField; // table layout to add mines to 

    private Block blocks[][]; // blocks for mine field 
    private int blockDimension = 24; // width of each block 
    private int blockPadding = 2; // padding between blocks 

    private int numberOfRowsInMineField = 9; 
    private int numberOfColumnsInMineField = 9; 

    public ProgressDialog progressDialog; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 


     progressDialog = ProgressDialog.show(SeatLayoutActivity.this, "Getting data", "Loading..."); 
     new GetDataTask(SeatLayoutActivity.this).execute(); 

      setContentView(R.layout.seatlayout); 
    } 


    private Boolean isOnline() { 
      ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo ni = cm.getActiveNetworkInfo(); 
      if(ni != null && ni.isConnected()) 
       return true; 

      return false; 
     } 

    private class GetDataTask extends AsyncTask<Void, Void, Integer> { 
     private Context ctx; 
     public GetDataTask(Context context) { 
      ctx = context; 
     } 

     @Override 
     protected Integer doInBackground(Void... params) { 

      if(isOnline()){ 



       mineField = (TableLayout)findViewById(R.id.MineField);  
       blocks = new Block[numberOfRowsInMineField + 2][numberOfColumnsInMineField + 2]; 


       //in feature below table(number of rows and columns) will based on RESET service response 

       for (int row = 0; row < numberOfRowsInMineField + 2; row++) 
       { 
        for (int column = 0; column < numberOfColumnsInMineField + 2; column++) 
        { 
         blocks[row][column] = new Block(ctx); 
         blocks[row][column].setDefaults();      

        } 
       } 

       for (int row = 1; row < numberOfRowsInMineField + 1; row++) 
       { 
        TableRow tableRow = new TableRow(ctx); 
        tableRow.setLayoutParams(new LayoutParams((blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding)); 

        for (int column = 1; column < numberOfColumnsInMineField + 1; column++) 
        { 
         blocks[row][column].setLayoutParams(new LayoutParams( 
           blockDimension + 2 * blockPadding, 
           blockDimension + 2 * blockPadding)); 
         blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding); 
         tableRow.addView(blocks[row][column]); 
        } 
        mineField.addView(tableRow,new TableLayout.LayoutParams( 
          (blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding)); 
       } 


      }else{ 
       Toast.makeText(SeatLayoutActivity.this, "No connection..", Toast.LENGTH_LONG).show();   
      } 

      return 1; 
     } 

     @Override 
     protected void onPostExecute(Integer result) { 


     progressDialog.dismiss(); 
      super.onPostExecute(result); 
     } 
    } 
} 

///Block.java 


package com.pxr.tutorial.xmltest; 

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.widget.Button; 

public class Block extends Button 
{ 
    private boolean isCovered; // is block covered yet 
    private boolean isMined; // does the block has a mine underneath 
    private boolean isFlagged; // is block flagged as a potential mine 
    private boolean isQuestionMarked; // is block question marked 
    private boolean isClickable; // can block accept click events 
    private int numberOfMinesInSurrounding; // number of mines in nearby blocks 

    public Block(Context context) 
    { 
     super(context); 
    } 

    public Block(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public Block(Context context, AttributeSet attrs, int defStyle) 
    { 
     super(context, attrs, defStyle); 
    } 

    // set default properties for the block 
    public void setDefaults() 
    { 
     isCovered = true; 
     isMined = false; 
     isFlagged = false; 
     isQuestionMarked = false; 
     isClickable = true; 
     numberOfMinesInSurrounding = 0; 

     this.setBackgroundResource(R.drawable.square_blue); 
     setBoldFont(); 
    } 

    // mark the block as disabled/opened 
    // update the number of nearby mines 
    public void setNumberOfSurroundingMines(int number) 
    { 
     this.setBackgroundResource(R.drawable.square_grey); 

     updateNumber(number); 
    } 

    // set mine icon for block 
    // set block as disabled/opened if false is passed 
    public void setMineIcon(boolean enabled) 
    { 
     this.setText("M"); 

     if (!enabled) 
     { 
      this.setBackgroundResource(R.drawable.square_grey); 
      this.setTextColor(Color.RED); 
     } 
     else 
     { 
      this.setTextColor(Color.BLACK); 
     } 
    } 

    // set mine as flagged 
    // set block as disabled/opened if false is passed 
    public void setFlagIcon(boolean enabled) 
    { 
     this.setText("F"); 

     if (!enabled) 
     { 
      this.setBackgroundResource(R.drawable.square_grey); 
      this.setTextColor(Color.RED); 
     } 
     else 
     { 
      this.setTextColor(Color.BLACK); 
     } 
    } 

    // set mine as question mark 
    // set block as disabled/opened if false is passed 
    public void setQuestionMarkIcon(boolean enabled) 
    { 
     this.setText("?"); 

     if (!enabled) 
     { 
      this.setBackgroundResource(R.drawable.square_grey); 
      this.setTextColor(Color.RED); 
     } 
     else 
     { 
      this.setTextColor(Color.BLACK); 
     } 
    } 

    // set block as disabled/opened if false is passed 
    // else enable/close it 
    public void setBlockAsDisabled(boolean enabled) 
    { 
     if (!enabled) 
     { 
      this.setBackgroundResource(R.drawable.square_grey); 
     } 
     else 
     { 
      this.setBackgroundResource(R.drawable.square_blue); 
     } 
    } 

    // clear all icons/text 
    public void clearAllIcons() 
    { 
     this.setText(""); 
    } 

    // set font as bold 
    private void setBoldFont() 
    { 
     this.setTypeface(null, Typeface.BOLD); 
    } 

    // uncover this block 
    public void OpenBlock() 
    { 
     // cannot uncover a mine which is not covered 
     if (!isCovered) 
      return; 

     setBlockAsDisabled(false); 
     isCovered = false; 

     // check if it has mine 
     if (hasMine()) 
     { 
      setMineIcon(false); 
     } 
     // update with the nearby mine count 
     else 
     { 
      setNumberOfSurroundingMines(numberOfMinesInSurrounding); 
     } 
    } 

    // set text as nearby mine count 
    public void updateNumber(int text) 
    { 
     if (text != 0) 
     { 
      this.setText(Integer.toString(text)); 

      // select different color for each number 
      // we have already skipped 0 mine count 
      switch (text) 
      { 
       case 1: 
        this.setTextColor(Color.BLUE); 
        break; 
       case 2: 
        this.setTextColor(Color.rgb(0, 100, 0)); 
        break; 
       case 3: 
        this.setTextColor(Color.RED); 
        break; 
       case 4: 
        this.setTextColor(Color.rgb(85, 26, 139)); 
        break; 
       case 5: 
        this.setTextColor(Color.rgb(139, 28, 98)); 
        break; 
       case 6: 
        this.setTextColor(Color.rgb(238, 173, 14)); 
        break; 
       case 7: 
        this.setTextColor(Color.rgb(47, 79, 79)); 
        break; 
       case 8: 
        this.setTextColor(Color.rgb(71, 71, 71)); 
        break; 
       case 9: 
        this.setTextColor(Color.rgb(205, 205, 0)); 
        break; 
      } 
     } 
    } 

    // set block as a mine underneath 
    public void plantMine() 
    { 
     isMined = true; 
    } 

    // mine was opened 
    // change the block icon and color 
    public void triggerMine() 
    { 
     setMineIcon(true); 
     this.setTextColor(Color.RED); 
    } 

    // is block still covered 
    public boolean isCovered() 
    { 
     return isCovered; 
    } 

    // does the block have any mine underneath 
    public boolean hasMine() 
    { 
     return isMined; 
    } 

    // set number of nearby mines 
    public void setNumberOfMinesInSurrounding(int number) 
    { 
     numberOfMinesInSurrounding = number; 
    } 

    // get number of nearby mines 
    public int getNumberOfMinesInSorrounding() 
    { 
     return numberOfMinesInSurrounding; 
    } 

    // is block marked as flagged 
    public boolean isFlagged() 
    { 
     return isFlagged; 
    } 

    // mark block as flagged 
    public void setFlagged(boolean flagged) 
    { 
     isFlagged = flagged; 
    } 

    // is block marked as a question mark 
    public boolean isQuestionMarked() 
    { 
     return isQuestionMarked; 
    } 

    // set question mark for the block 
    public void setQuestionMarked(boolean questionMarked) 
    { 
     isQuestionMarked = questionMarked; 
    } 

    // can block receive click event 
    public boolean isClickable() 
    { 
     return isClickable; 
    } 

    // disable block for receive click events 
    public void setClickable(boolean clickable) 
    { 
     isClickable = clickable; 
    } 
} 

回答

1

您不得修改甚至從非UI線程訪問UI。如果您想修改UI從AsyncTask.doInBackground()您應該使用Activity.runOnUiThread()或定期致電AsyncTask.publishProgress()並覆蓋AsyncTask.onProgressUpdate()方法,因爲它在主線程上執行。

+0

Thx..its working now.Now我發送REST URL響應publishProgress(xml)並在AsyncTask.onProgressUpdate(String ... values)中創建tablerow。 –

+0

太棒了!現在,如果你認爲這是正確的,你可以接受這個答案。 – Michael

+0

@Michael或在「onPostExecute」,正確的? –

0

的第一件事是,這個代碼將不會編譯..因爲你試圖做UI相關的東西在其中沒有任何與UI ..加圖和toast應做在UIthread,我的意思是後臺線程在postExecute()

0

寫入onPostExecute()表中創建代碼 '

if(isOnline()){

  mineField = (TableLayout)findViewById(R.id.MineField);  
      blocks = new Block[numberOfRowsInMineField + 2][numberOfColumnsInMineField + 2]; 


      //in feature below table(number of rows and columns) will based on RESET service response 

      for (int row = 0; row < numberOfRowsInMineField + 2; row++) 
      { 
       for (int column = 0; column < numberOfColumnsInMineField + 2; column++) 
       { 
        blocks[row][column] = new Block(ctx); 
        blocks[row][column].setDefaults();      

       } 
      } 

      for (int row = 1; row < numberOfRowsInMineField + 1; row++) 
      { 
       TableRow tableRow = new TableRow(ctx); 
       tableRow.setLayoutParams(new LayoutParams((blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding)); 

       for (int column = 1; column < numberOfColumnsInMineField + 1; column++) 
       { 
        blocks[row][column].setLayoutParams(new LayoutParams( 
          blockDimension + 2 * blockPadding, 
          blockDimension + 2 * blockPadding)); 
        blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding); 
        tableRow.addView(blocks[row][column]); 
       } 
       mineField.addView(tableRow,new TableLayout.LayoutParams( 
         (blockDimension + 2 * blockPadding) * numberOfColumnsInMineField, blockDimension + 2 * blockPadding)); 
      } 


     }else{ 
      Toast.makeText(SeatLayoutActivity.this, "No connection..", Toast.LENGTH_LONG).show();   
     } 

`