1
0
mirror of https://github.com/bspeice/itcs4180 synced 2024-12-04 13:18:16 -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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:id="@+id/layout_async"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@ -11,15 +11,15 @@
tools:context=".PhotoActivity" >
<GridView
android:id="@+id/GridView1"
android:id="@+id/grid_async"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="@integer/column_count"
android:columnWidth="90dp" />
android:layout_weight="1"
android:columnWidth="90dp"
android:numColumns="@integer/column_count" />
<Button
android:id="@+id/ExitButton"
android:id="@+id/btn_exit_async"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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"
android:id="@+id/layout_thread"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
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);
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)
{

View File

@ -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<Bitmap> bitmapList = new ArrayList<Bitmap>();
ArrayList<String> bitmapNames = new ArrayList<String>();
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;
}
}