Hi all guys,

I tried to get the pics from internet and display all the results in
the Gridview with a dynamic updated. However it doesn't work properly.
I did some research on the internet and using AsyncTask class to avoid
the block of the UI Thread. But I just can't get the Gridview
Dynamiclly updated well.

Here is the code snippet:

public class GridViewMultiThread extends Activity {
        Button button = null;
        GridView gridView = null;
        Context context = null;
        ImageAdapter imageAdapter;
        private View mSearchPanel;
        DownloadPhotosTask downloadPhotosTask;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);

                gridView = (GridView) findViewById(R.id.showPhotoGridView);

                imageAdapter = new ImageAdapter(this);
                gridView.setAdapter(imageAdapter);
                downloadPhotosTask = (DownloadPhotosTask) new DownloadPhotosTask
().execute();
//              button.setOnClickListener(new OnClickListener() {
//                      @Override
//                      public void onClick(View v) {
//                              button.setEnabled(false);
//                              downloadPhotosTask = (DownloadPhotosTask) new 
DownloadPhotosTask
()
//                                              .execute();
//                      }
//              });
        }

        class DownloadPhotosTask extends AsyncTask<Void, Photo, Void>
implements
                        PhotoDownloadListener {

//              @Override
//              protected void onPreExecute() {
//                      if (mSearchPanel == null) {
//                              mSearchPanel = ((ViewStub) 
findViewById(R.id.stub_search))
//                                              .inflate();
//
//                              ProgressBar progress = (ProgressBar) 
mSearchPanel
//                                              .findViewById(R.id.progress);
//                              progress.setIndeterminate(true);
//
//                              ((TextView) 
mSearchPanel.findViewById(R.id.label_loading))
//                                              .setText("loading");
//
//                              final View cancelButton = mSearchPanel
//                                              
.findViewById(R.id.button_cancel);
//                              cancelButton.setOnClickListener(new 
View.OnClickListener() {
//                                      public void onClick(View v) {
//                                              onCancelSearch();
//                                      }
//                              });
//                      }
//              }

//              private void onCancelSearch() {
//                      if (downloadPhotosTask != null
//                                      && downloadPhotosTask.getStatus() == 
AsyncTask.Status.RUNNING)
{
//                              downloadPhotosTask.cancel(true);
//                              downloadPhotosTask = null;
//                      }
//              }

                @Override
                protected Void doInBackground(Void... params) {
                        DownLoader.getInstance().downLoadImage(this);
                        return null;
                }

                @Override
                public void onPhotoDownloadListener(Photo photo) {
                        if (photo != null && !isCancelled()) {
                                publishProgress(photo);
                        }
                }

                @Override
                public void onProgressUpdate(Photo... photos) {
                        for (Photo photo : photos) {
                                imageAdapter.addPhoto(photo);
                                gridView.invalidateViews();
//                              imageAdapter.registerDataSetObserver(observer)
                        }
                }

        }

}

class ImageAdapter extends BaseAdapter {

        private Context mContext;
        private List<Photo> photos = new ArrayList<Photo>();

        public ImageAdapter(Context context) {
                this.mContext = context;
        }

        public void addPhoto(Photo photo) {
                photos.add(photo);
        }

        @Override
        public int getCount() {
                return photos.size();
        }

        @Override
        public Object getItem(int position) {
                return photos.get(position);
        }

        @Override
        public long getItemId(int position) {
                return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
{
                final ImageView imageView;
                if (convertView == null) {
                        imageView = new ImageView(mContext);
                } else {
                        imageView = (ImageView) convertView;
                }
                imageView.setImageBitmap(photos.get(position).getBm());
                return imageView;
        }

}

class DownLoader {
        private static DownLoader downloader = new DownLoader();
        private static String[] myImageURL = null;
        private List<Photo> photos = new ArrayList<Photo>();

        public static DownLoader getInstance() {
                initImageURL();
                return downloader;
        }

        static void initImageURL() {
                int no = 0;
                myImageURL = new String[100];
                for (int i = 0; i < myImageURL.length; i++) {
                        myImageURL[i] = "http://cp.a8.com/image/128X128GIF/8"; + 
no +
".gif";
                        no++;
                        if (no % 10 == 0) {
                                no = 0;
                        }
                }
        }

        public List<Photo> downLoadImage(PhotoDownloadListener listener) {
                List<String> urls = Arrays.asList(myImageURL);
                List<Photo> photos = new ArrayList<Photo>();
                URL aryURI = null;
                URLConnection conn = null;
                InputStream is = null;
                Bitmap bm = null;
                Photo photo = null;
                for (String url : urls) {
                        // Log.e("URL:", url);
                        try {
                                aryURI = new URL(url);
                                conn = aryURI.openConnection();
                                is = conn.getInputStream();
                                bm = BitmapFactory.decodeStream(is);
                                photo = new Photo(bm);
                                listener.onPhotoDownloadListener(photo);
                                photos.add(photo);
                        } catch (Exception e) {
                                throw new RuntimeException(e);
                        } finally {
                                try {
                                        if (is != null)
                                                is.close();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                        }
                }
                return photos;
        }
}

class Photo {
        private Bitmap bm;

        public Photo(Bitmap bm) {
                this.bm = bm;
        }

        public Bitmap getBm() {
                return bm;
        }

        public void setBm(Bitmap bm) {
                this.bm = bm;
        }

        interface PhotoDownloadListener {
                public void onPhotoDownloadListener(Photo photo);
        }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content" 
android:text="@string/hello" />

        <GridView android:id="@+id/showPhotoGridView"
                android:layout_width="wrap_content"
android:layout_height="wrap_content"
                android:numColumns="3" android:verticalSpacing="10dp"
                android:horizontalSpacing="10dp" />
        <ViewStub android:id="@+id/stub_search" android:inflatedId="@+id/
panel_search"
                android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
                android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>

--~--~---------~--~----~------------~-------~--~----~
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