diff --git a/InClass3/res/layout/activity_photo.xml b/InClass3/res/layout/activity_photo.xml
index 3be93a5..12bd166 100644
--- a/InClass3/res/layout/activity_photo.xml
+++ b/InClass3/res/layout/activity_photo.xml
@@ -1,6 +1,6 @@
+ android:layout_weight="1"
+ android:columnWidth="90dp"
+ android:numColumns="@integer/column_count" />
\ No newline at end of file
diff --git a/InClass3/src/edu/uncc/itcs4180/hw4/PhotoActivity.java b/InClass3/src/edu/uncc/itcs4180/hw4/PhotoActivity.java
index bddd650..bb517c6 100644
--- a/InClass3/src/edu/uncc/itcs4180/hw4/PhotoActivity.java
+++ b/InClass3/src/edu/uncc/itcs4180/hw4/PhotoActivity.java
@@ -46,8 +46,8 @@ public class PhotoActivity extends Activity {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
- root = (LinearLayout)findViewById(R.id.LinearLayout1);
- photoGrid = (GridView)findViewById(R.id.GridView1);
+ root = (LinearLayout)findViewById(R.id.layout_async);
+ photoGrid = (GridView)findViewById(R.id.grid_async);
downloadProgress = 0;
//set the progress dialog
@@ -62,7 +62,7 @@ public class PhotoActivity extends Activity {
}
//create exit button
- Button exit = (Button)findViewById(R.id.ExitButton);
+ Button exit = (Button)findViewById(R.id.btn_exit_async);
exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
diff --git a/InClass3/src/edu/uncc/itcs4180/hw4/PhotoThread.java b/InClass3/src/edu/uncc/itcs4180/hw4/PhotoThread.java
index 8be8df4..61d2164 100644
--- a/InClass3/src/edu/uncc/itcs4180/hw4/PhotoThread.java
+++ b/InClass3/src/edu/uncc/itcs4180/hw4/PhotoThread.java
@@ -13,12 +13,16 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import org.apache.http.HttpConnection;
+import edu.uncc.itcs4180.hw4.PhotoActivity.Holder;
+import edu.uncc.itcs4180.hw4.PhotoActivity.ImageAdapter;
+
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
@@ -31,7 +35,10 @@ import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.GridView;
import android.widget.ImageView;
+import android.widget.TextView;
public class PhotoThread extends Activity {
@@ -39,13 +46,23 @@ public class PhotoThread extends Activity {
public int[] imageUrlIds = {R.string.uncc_main_image, R.string.football_main_image,
R.string.ifest_main_image, R.string.commencement_main_image
};
+ int[] imageNames = {R.string.uncc, R.string.sports, R.string.ifest, R.string.commencement};
+ GridView photoGrid;
+ ArrayList bitmapList = new ArrayList();
+ ArrayList bitmapNames = new ArrayList();
private final int MSG_WHAT_ERROR = -1;
+
+ public void onClickExit(View v) {
+ finish();
+ }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_thread);
+ photoGrid = (GridView)findViewById(R.id.grid_thread);
+
// Set the initial progress dialog
dialog = new ProgressDialog(this);
dialog.setMessage("Downloading images...");
@@ -70,19 +87,21 @@ public class PhotoThread extends Activity {
// Start up the thread pool, and get downloading!
ExecutorService pool = Executors.newFixedThreadPool(2);
- pool.execute(new DownloadThread(getString(R.string.uncc_main_image), handler));
- pool.execute(new DownloadThread(getString(R.string.football_main_image), handler));
- pool.execute(new DownloadThread(getString(R.string.ifest_main_image), handler));
- pool.execute(new DownloadThread(getString(R.string.commencement_main_image), handler));
+ pool.execute(new DownloadThread(getString(R.string.uncc_main_image), handler, R.string.uncc));
+ pool.execute(new DownloadThread(getString(R.string.football_main_image), handler, R.string.sports));
+ pool.execute(new DownloadThread(getString(R.string.ifest_main_image), handler, R.string.ifest));
+ pool.execute(new DownloadThread(getString(R.string.commencement_main_image), handler, R.string.commencement));
}
private class DownloadThread implements Runnable{
String url;
Handler handler;
+ int name;
- public DownloadThread(String url, Handler handler) {
+ public DownloadThread(String url, Handler handler, int name) {
this.url = url;
this.handler = handler;
+ this.name = name;
}
@Override
@@ -100,6 +119,7 @@ public class PhotoThread extends Activity {
Bitmap image = BitmapFactory.decodeStream(con.getInputStream());
Message msg = Message.obtain(handler);
msg.obj = image;
+ msg.what = name;
handler.sendMessage(msg);
} else {
handler.sendEmptyMessage(MSG_WHAT_ERROR);
@@ -137,14 +157,15 @@ public class PhotoThread extends Activity {
} else {
// The thread worked just fine, so let's get the Bitmap out and create a view for it
Bitmap image = (Bitmap) msg.obj;
- ImageView newImage = new ImageView(this.ctx);
- newImage.setImageBitmap(image);
- ((ViewGroup)findViewById(android.R.id.content)).addView(newImage);
+ bitmapList.add(image);
+ bitmapNames.add(ctx.getString(msg.what));
}
// If (max - increment == current progress) we're done
- if (dialog.getMax() - progressIncrement == dialog.getProgress())
+ if (dialog.getMax() - progressIncrement == dialog.getProgress()) {
+ photoGrid.setAdapter(new ImageAdapter(photoGrid.getContext()));
dialog.dismiss();
+ }
else
dialog.setProgress(dialog.getProgress() + progressIncrement);
@@ -152,5 +173,73 @@ public class PhotoThread extends Activity {
}
}
+
+ // Code very similar to PhotoActivity, but tweaked slightly for different ID's, etc.
+
+ public class ImageAdapter extends BaseAdapter
+ {
+ private Context context;
+
+ public ImageAdapter(Context context)
+ {
+ this.context = context;
+ }
+
+ @Override
+ public int getCount()
+ {
+ return imageUrlIds.length;
+ }
+
+ @Override
+ public Object getItem(int position)//no purpose. only to fill the requirement of needing the method.
+ {
+ return position;
+ }
+
+ @Override
+ public long getItemId(int position)//no purpose. only to fill the requirement of needing the method.
+ {
+ return position;
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent)
+ {
+ Holder holder = new Holder();
+ View vi = convertView;
+
+ if(vi == null)
+ {
+ //create layout of what we want one grid section to look like
+ vi = getLayoutInflater().inflate(R.layout.grid_schema, null);
+
+ holder.textView = (TextView)vi.findViewById(R.id.textView1);
+ holder.imageView = (ImageView)vi.findViewById(R.id.imageView1);
+
+ vi.setTag(holder);//associate the views in the holder to the grid
+ }
+ else
+ {
+ holder = (Holder)(vi.getTag());
+ }
+ //set the views in the grid to what was loaded
+ holder.textView.setText(getString(R.string.download_error));
+ if(bitmapList.get(position)!=null)
+ {
+ holder.imageView.setImageBitmap(bitmapList.get(position));
+ holder.textView.setText(bitmapNames.get(position));
+ }
+
+ return vi;
+ }
+ }
+
+ //views included in one grid section
+ static class Holder
+ {
+ TextView textView;
+ ImageView imageView;
+ }
}