2012/03/26

[Android]Display view first, flash content later

Let view comes out first, then update the content later.
Do not put everything into onCreate(), try to use AsyncTask class to access content in background.

1. Implement a class to inherent AsynTask class
class RunningAsyncTask extends AsyncTask{
   protected void onPreExecute( ) { 
     // system will call this first
     // do whatever view content modification
     // just after thread started.
      bEndThread = false;
   }
   protected void onPostExecute( String value ) { 
     // system will call this when thread finished.
     // do whatever view content modification
     // before thread finished.
      SetAddressText ( value );
      bEndThread = true;
   }
   protected void onProgressUpdate(Integer... values) {  
     // system will call this when publishProgress( )
     // is called in doInBackground( )
     // do whatever view content modification 
     // when the background process.
   } 
   protected String doInBackground(Void... params) {  
     // CAN NOT DO any modification of view content
     // just do any content fetch for longer process time
     // Call publishProgress((int) ((i / (float) count) * 100))
     // giving a chance to update the view during the process time
      String addStr = getCurrentGPSAddress( );
      return addStr;
   }
}
2. In onResume or onCreate to init the thread (never reentry)
  protected void onResume(){
    super.onResume( );
    if( bEndThread == true);
      new RunningAsyncTask().execute();
  }
3. Never Never execute a new AsyncTask when the last one is not finished.

2012/03/25

[Android] Gridview display in reverse order

If your Gridview got a reverse order display bug after refresh the screen
try to change the code of Google sample to below code
 public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     if(convertView==null){ 
         LayoutInflater li = getLayoutInflater(); 
         v = li.inflate(R.layout.iconview, parent, false); 
     } 
     TextView tv = (TextView)v.findViewById(R.id.icon_text); 
     tv.setText(fun_strs[position]); 
     ImageView iv = (ImageView)v.findViewById(R.id.icon_image); 
     iv.setImageResource(fun_imgs[position]); 
     return v; 
 }