Question

Je récupére l'image et le texte pour GridView à partir d'un service Web, il faut donc un certain temps pour afficher le GridView. Je veux montrer un progressdialog jusqu'à ce que la grille soit complètement chargée. Ce que j'ai fait jusqu'à présent est comme ci-dessous:

public class PCGridMain extends Activity
{
    WebService web = new WebService();
    private GridView gridView;
    ProgressDialog dialog;
    Bitmap icon;


    int i, total;
    URL url= null;
    List<GridItem> list;

    @Override
    public void onCreate(Bundle grid)
    {
        super.onCreate(grid);
        dialog = ProgressDialog.show(PCGridMain.this, "Loading...", "Loading App, Please wait.", true);
        DialogWork dWork = new DialogWork();
        dWork.execute();

        setContentView(R.layout.main);
        gridView = (GridView)findViewById(R.id.gridView1);

        web.WebService1();       
        total = web.totalService;        



        list = new ArrayList<GridItem>();

        for(i=0; i<total; i++)
            {
                    Log.v("Try Block", "See what we get:-");
                    try 
                    {
                        Log.v("GridMain", "try url"  + Integer.toString(i));
                        url = new URL(web.arr[i][2]);
                    }
                    catch (MalformedURLException e)
                    {   
                        Log.v("GridMain", "catch MalformedURLException" + Integer.toString(i));
                        e.printStackTrace();
                    }


                    try
                    {
                        Log.v("GridMain", "try BitmapFactory"  + Integer.toString(i));
                        icon = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                    } 
                    catch (IOException e)
                    {   
                        Log.v("GridMain", "catch IOException"  + Integer.toString(i));
                        e.printStackTrace();
                    }

                    list.add(new GridItem(icon, web.arr[i][1]));                    // Adding Icon & LAbel              

            }

        gridView.setAdapter(new GridAdapter(this, list));
        gridView.setOnItemClickListener(Itemlistener);
    }



    private OnItemClickListener Itemlistener = new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> 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<URL, Integer, Long>
    {       
        protected Long doInBackground(URL... params) 
        {
            try
            {

            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            return null;
        }

        protected void onProgressUpdate(Integer... progress)
        {

        }

        protected void onPostExecute(Long result)
        {
            try
            {
                //setContentView(R.layout.main);
                //gridView.setAdapter(new GridAdapter(PCGridMain.this, list));              
                dialog.dismiss();                   
            }
            catch (Exception e)
            {
                e.printStackTrace();
                dialog.dismiss();
            }
        }

    }

Veuillez me dire que quel code doit être placé à quel emplacement exact, chaque fois que je fais des modifications, cela ne montre aucun effet ou l'application se ferme en raison d'un problème.

Merci,

Haps.

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top