Created
April 10, 2013 04:17
-
-
Save jpotts18/5351749 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.parachuteapp.android.helpers; | |
import java.util.List; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import com.parachuteapp.android.R; | |
import com.parachuteapp.android.models.Dropzone; | |
public class LazyDropzoneAdapter extends BaseAdapter { | |
private String TAG = LazyDropzoneAdapter.class.getSimpleName(); | |
private Activity activity; | |
private List<Dropzone> dropzoneArray; | |
private static LayoutInflater mInflater = null; | |
public LazyDropzoneAdapter(Activity a, List<Dropzone> dropzoneList) { | |
activity = a; | |
dropzoneArray = dropzoneList; | |
mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
} | |
@Override | |
public int getCount() { | |
return dropzoneArray.size(); | |
} | |
@Override | |
public Object getItem(int position) { | |
return position; | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
static class ViewHolder { | |
TextView dropzoneHashTag; | |
TextView dropzoneName; | |
ImageView dropzoneImage; | |
} | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder; | |
// Lookup current game based on selected row | |
Dropzone currentDropzone = new Dropzone(); | |
currentDropzone = dropzoneArray.get(position); | |
if (convertView == null) { | |
convertView = mInflater.inflate(R.layout.dropzone_list_row, parent, false); | |
holder = new ViewHolder(); | |
holder.dropzoneImage = (ImageView) convertView.findViewById(R.id.iv_dropzone_img); | |
holder.dropzoneHashTag = (TextView) convertView.findViewById(R.id.tv_drop_hashtag); | |
holder.dropzoneName = (TextView) convertView.findViewById(R.id.tv_dropzone_name); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
int randomImage = getRandomImage(); | |
Bitmap bm = BitmapFactory.decodeResource(activity.getResources(), randomImage); | |
holder.dropzoneHashTag.setText(currentDropzone.getHashTag()); | |
holder.dropzoneName.setText(currentDropzone.getName()); | |
holder.dropzoneImage.setImageBitmap(bm); | |
return convertView; | |
} | |
private int getRandomImage() { | |
int minimum = 1; | |
int maximum = 9; | |
int randomNum = minimum + (int) (Math.random() * maximum); | |
Log.i(TAG, "random Num = " + randomNum); | |
if (randomNum == 1) { | |
return R.drawable.building_1; | |
} else if (randomNum == 2) { | |
return R.drawable.building_2; | |
} else if (randomNum == 3) { | |
return R.drawable.building_3; | |
} else if (randomNum == 4) { | |
return R.drawable.building_4; | |
} else if (randomNum == 5) { | |
return R.drawable.building_5; | |
} else if (randomNum == 6) { | |
return R.drawable.building_6; | |
} else if (randomNum == 7) { | |
return R.drawable.building_7; | |
} else if (randomNum == 8) { | |
return R.drawable.building_8; | |
} else if (randomNum == 9) { | |
return R.drawable.building_9; | |
} else { | |
return R.drawable.building_1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment