Check out the AsyncTask object:

http://developer.android.com/reference/android/os/AsyncTask.html

Simple sample would be like:

    private class AsyncInit extends AsyncTask<Void, Void, Void>
    {
        public static boolean bThreadRunning = false;
        public Context context;
        private ProgressDialog pdDialog;

        @Override
        protected void onPreExecute()
        {
            bThreadRunning = true;
            pdDialog = ProgressDialog.show( context, "", "Doing something, 
please wait...", true );
        }

        @Override
        protected void onPostExecute(Void result)
        {
            bThreadRunning = false;
            pdDialog.dismiss();
        }

        @Override
        protected Void doInBackground(Void... params)
        {
            // Do something here in the background
            return null;
        }
    }

Then you can just activate that by this:

    public void openBackground( Context context )
    {
        if( !AsyncInit.bThreadRunning )
        {
            AsyncInit atInit = new AsyncInit();
            atInit.context = context;
            atInit.execute( null, null, null );
                }
    }

There are other options available also, check out Threads, Handlers and 
Runnables also.

Steven
Studio LFP
http://www.studio-lfp.com


On Sunday, October 9, 2011 8:12:51 AM UTC-5, Muchamad Jeffri wrote:
>
> hello, i want to ask how to implement progress bar spinner in 
> background and then stop when parsing JSON data finish to list view. 
> this my source code. 
>
> package com.cob.json; 
>
> import java.lang.reflect.Type; 
> import java.util.ArrayList; 
> import java.util.HashMap; 
> import java.util.List; 
> import java.util.Map; 
>
> import android.app.Activity; 
> import android.os.Bundle; 
> import android.util.Log; 
> import android.view.View; 
> import android.widget.ListView; 
> import android.widget.Toast; 
>
> import com.google.gson.Gson; 
> import com.google.gson.reflect.TypeToken; 
>
> public class JSONListViewActivity extends Activity { 
>   /** Called when the activity is first created. */ 
>     //ListView that will hold our items references back to main.xml 
>     ListView lstTest; 
>     //Array Adapter that will hold our ArrayList and display the items 
> on the ListView 
>     ProjectsAdapter arrayAdapter; 
>
>     //List that will  host our items and allow us to modify that array 
> adapter 
>     ArrayList<Projects> prjcts=null; 
>     @Override 
>     public void onCreate(Bundle savedInstanceState) { 
>         super.onCreate(savedInstanceState); 
>         setContentView(R.layout.main); 
>         //Initialize ListView 
>         lstTest= (ListView)findViewById(R.id.lstText); 
>
>          //Initialize our ArrayList 
>         prjcts = new ArrayList<Projects>(); 
>         //Initialize our array adapter notice how it references the 
> listitems.xml layout 
>         arrayAdapter = new ProjectsAdapter(JSONListViewActivity.this, 
> R.layout.list_item,prjcts); 
>
>         //Set the above adapter as the adapter of choice for our list 
>         lstTest.setAdapter(arrayAdapter); 
>
>
>         //Instantiate the Web Service Class with he URL of the web 
> service not that you must pass 
>         WebService webService = new WebService("http:// 
> frontine.hostoi.com/jsontest.json"); 
>
>         //Pass the parameters if needed , if not then pass dummy one 
> as follows 
>         Map<String, String> params = new HashMap<String, String>(); 
>         params.put("var", ""); 
>
>         //Get JSON response from server the "" are where the method 
> name would normally go if needed example 
>         // webService.webGet("getMoreAllerts", params); 
>         String response = webService.webGet("", params); 
>
>         try 
>         { 
>             //Parse Response into our object 
>             Type collectionType = new TypeToken<ArrayList<Projects>>() 
> {}.getType(); 
>
>             //JSON expects an list so can't use our ArrayList from the 
> lstart 
>             List<Projects> lst= new Gson().fromJson(response, 
> collectionType); 
>
>             //Now that we have that list lets add it to the ArrayList 
> which will hold our items. 
>             for(Projects l : lst) 
>             { 
>                 prjcts.add(l); 
>             } 
>
>             //Since we've modified the arrayList we now need to notify 
> the adapter that 
>             //its data has changed so that it updates the UI 
>             arrayAdapter.notifyDataSetChanged(); 
>         } 
>         catch(Exception e) 
>         { 
>             Log.d("Error: ", e.getMessage()); 
>         } 
>
>
>
>     } 
>     protected void onListItemClick(ListView lstTest, View v, int 
> position, long id) { 
> // TODO Auto-generated method stub 
> //super.onListItemClick(l, v, position, id); 
> String selection = lstTest.getItemAtPosition(position).toString(); 
> Toast.makeText(this, selection, Toast.LENGTH_LONG).show(); 
> } 
>
>
> }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to