1
0
mirror of https://github.com/bspeice/itcs4180 synced 2024-11-13 19:08:20 -05:00

Fix up the threaded section of the layout

Also, minor tweaks to the asynctask portion.
This commit is contained in:
DjBushido 2014-03-10 18:24:50 -04:00
parent 467d7eb9a7
commit d23208c55f
4 changed files with 127 additions and 20 deletions

View File

@ -1,6 +1,6 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1" android:id="@+id/layout_async"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
@ -11,15 +11,15 @@
tools:context=".PhotoActivity" > tools:context=".PhotoActivity" >
<GridView <GridView
android:id="@+id/GridView1" android:id="@+id/grid_async"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_weight="1" android:layout_weight="1"
android:numColumns="@integer/column_count" android:columnWidth="90dp"
android:columnWidth="90dp" /> android:numColumns="@integer/column_count" />
<Button <Button
android:id="@+id/ExitButton" android:id="@+id/btn_exit_async"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom" android:layout_gravity="bottom"

View File

@ -1,11 +1,29 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_thread"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".PhotoThread" > tools:context=".PhotoThread" >
</RelativeLayout> <GridView
android:id="@+id/grid_thread"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:columnWidth="90dp"
android:numColumns="@integer/column_count" >
</GridView>
<Button
android:id="@+id/btn_exit_async"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickExit"
android:text="@string/exit_button_text" />
</LinearLayout>

View File

@ -46,8 +46,8 @@ public class PhotoActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo); setContentView(R.layout.activity_photo);
root = (LinearLayout)findViewById(R.id.LinearLayout1); root = (LinearLayout)findViewById(R.id.layout_async);
photoGrid = (GridView)findViewById(R.id.GridView1); photoGrid = (GridView)findViewById(R.id.grid_async);
downloadProgress = 0; downloadProgress = 0;
//set the progress dialog //set the progress dialog
@ -62,7 +62,7 @@ public class PhotoActivity extends Activity {
} }
//create exit button //create exit button
Button exit = (Button)findViewById(R.id.ExitButton); Button exit = (Button)findViewById(R.id.btn_exit_async);
exit.setOnClickListener(new View.OnClickListener() { exit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) public void onClick(View v)
{ {

View File

@ -13,12 +13,16 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import org.apache.http.HttpConnection; 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.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
@ -31,7 +35,10 @@ import android.util.Log;
import android.view.Menu; import android.view.Menu;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView;
public class PhotoThread extends Activity { 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, public int[] imageUrlIds = {R.string.uncc_main_image, R.string.football_main_image,
R.string.ifest_main_image, R.string.commencement_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<Bitmap> bitmapList = new ArrayList<Bitmap>();
ArrayList<String> bitmapNames = new ArrayList<String>();
private final int MSG_WHAT_ERROR = -1; private final int MSG_WHAT_ERROR = -1;
public void onClickExit(View v) {
finish();
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo_thread); setContentView(R.layout.activity_photo_thread);
photoGrid = (GridView)findViewById(R.id.grid_thread);
// Set the initial progress dialog // Set the initial progress dialog
dialog = new ProgressDialog(this); dialog = new ProgressDialog(this);
dialog.setMessage("Downloading images..."); dialog.setMessage("Downloading images...");
@ -70,19 +87,21 @@ public class PhotoThread extends Activity {
// Start up the thread pool, and get downloading! // Start up the thread pool, and get downloading!
ExecutorService pool = Executors.newFixedThreadPool(2); ExecutorService pool = Executors.newFixedThreadPool(2);
pool.execute(new DownloadThread(getString(R.string.uncc_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)); 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)); 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)); pool.execute(new DownloadThread(getString(R.string.commencement_main_image), handler, R.string.commencement));
} }
private class DownloadThread implements Runnable{ private class DownloadThread implements Runnable{
String url; String url;
Handler handler; Handler handler;
int name;
public DownloadThread(String url, Handler handler) { public DownloadThread(String url, Handler handler, int name) {
this.url = url; this.url = url;
this.handler = handler; this.handler = handler;
this.name = name;
} }
@Override @Override
@ -100,6 +119,7 @@ public class PhotoThread extends Activity {
Bitmap image = BitmapFactory.decodeStream(con.getInputStream()); Bitmap image = BitmapFactory.decodeStream(con.getInputStream());
Message msg = Message.obtain(handler); Message msg = Message.obtain(handler);
msg.obj = image; msg.obj = image;
msg.what = name;
handler.sendMessage(msg); handler.sendMessage(msg);
} else { } else {
handler.sendEmptyMessage(MSG_WHAT_ERROR); handler.sendEmptyMessage(MSG_WHAT_ERROR);
@ -137,14 +157,15 @@ public class PhotoThread extends Activity {
} else { } else {
// The thread worked just fine, so let's get the Bitmap out and create a view for it // The thread worked just fine, so let's get the Bitmap out and create a view for it
Bitmap image = (Bitmap) msg.obj; Bitmap image = (Bitmap) msg.obj;
ImageView newImage = new ImageView(this.ctx); bitmapList.add(image);
newImage.setImageBitmap(image); bitmapNames.add(ctx.getString(msg.what));
((ViewGroup)findViewById(android.R.id.content)).addView(newImage);
} }
// If (max - increment == current progress) we're done // 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(); dialog.dismiss();
}
else else
dialog.setProgress(dialog.getProgress() + progressIncrement); dialog.setProgress(dialog.getProgress() + progressIncrement);
@ -153,4 +174,72 @@ 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;
}
} }