To use ProgressDialog till GridView gets loaded from webservice

后端 未结 4 1133
被撕碎了的回忆
被撕碎了的回忆 2021-01-17 05:51

I am fetching Image and Text for GridView from a webservice, so its takes some time to display the GridView. I want to show a ProgressDialog till Grid gets fully loaded. Wha

4条回答
  •  旧巷少年郎
    2021-01-17 06:22

    Here I am giving the complete answer to my question, So that it may help others to make it done easily...

    public class PCGridMain extends Activity
    {
        WebService web = new WebService();
        private GridView gridView;
        ProgressDialog dialog;
        Bitmap icon;
    
    
    int i, total;
    URL url= null;
    List list;
    
    @Override
    public void onCreate(Bundle grid)
    {
        super.onCreate(grid);
    
        Log.v("GridMain", "setContent");     
        setContentView(R.layout.main);
        gridView = (GridView)findViewById(R.id.gridView1);
    
        DialogWork dWork = new DialogWork();
        dWork.execute();
    
    }
    
    
    
    private void ForLoop()
    {
    
        for(i=0; i arg0, View view, int position, long id)
        {
            ViewHolder holder = (ViewHolder)view.getTag();
    
            if(holder == null)
            {
                return;
            }
            Toast.makeText(PCGridMain.this, holder.label.getText(), Toast.LENGTH_SHORT).show();
                                                                                Log.v("GridMain", "Intent Creation");
            Intent intent = new Intent(view.getContext(), ShowService.class);   Log.v("GridMain", "Intent Created");
            intent.putExtra("ServiceId", web.arr[position][0]);                 Log.v("GridMain", "ValueAdded Sid");
            intent.putExtra("SName", holder.label.getText());                   Log.v("GridMain", "ValueAdded SName");
    
            startActivity(intent);          
    
        }
    };
    
    
    class DialogWork extends AsyncTask
    {   
        protected void onPreExecute()
        {
            Log.v("GridMain", "PreExecute()");
            dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", false, true);
    
        }
        protected Long doInBackground(URL... params) 
        {
                        String response = "";
            try
            {
                Log.v("GridMain", "doInBackground");
                response = web.WebService1();
                total = web.totalService; 
    
            }
            catch (InterruptedException e)
            {
                Log.v("GridMain", "InterruptedException");
                e.printStackTrace();
            }           
            return response;
        }       
    
        protected void onPostExecute(String result)
        {
            try
            {
                                // Response is in RESULT_VAR
                Log.v("GridMain", "onPostExecute");
                list = new ArrayList();
                ForLoop();
                gridView.setAdapter(new GridAdapter(PCGridMain.this, list));
                gridView.setOnItemClickListener(Itemlistener);
                dialog.dismiss();
            }
            catch (Exception e)
            {
                Log.v("GridMain", "Exception e");
                e.printStackTrace();
                dialog.dismiss();
            }
        }
    
    }   
    }
    

    I have used it as according to my needs, its just for the help, the complete code might give problem to you. So just take it as a reference.

    Thanks & Regards,

    Haps.

提交回复
热议问题