package com.tailan.moms.adapter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.tailan.moms.activity.ApplicationActivity;
import com.tailan.moms.activity.HiddenDangeActivity;
import com.tailan.moms.activity.PictureViewActivity;
import com.tailan.moms.activity.R;
import com.tailan.moms.activity.workorder.WorkOrderReportActivity;
import com.tailan.moms.common.Communal;
import com.tailan.moms.model.PatrolListData;
import com.tailan.moms.model.PatrolReportBackData;
import com.tailan.moms.model.TaskTemplateDetail;
import com.tailan.moms.tools.LogFile;
import com.tailan.moms.tools.MyGridView;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class PatrolAdapter extends BaseAdapter {
private List<TaskTemplateDetail> list = null;
private LayoutInflater inflater;
private Context context = null;
List<PatrolReportBackData> patrolReportBackDatas = null;
public static final int NONE = 0;
public static final int PHOTOHRAPH = 1;// 拍照
public static final int PHOTOZOOM = 2; // 縮放
public static final int PHOTORESOULT = 3;// 結果
public static final String IMAGE_UNSPECIFIED = "image/*";
byte[] bitmapBytes = new byte[1024];
protected String bitPhoto = "";
ApplicationActivity activity;
protected ArrayList<HashMap<String, Object>> imageItem = null;
int index = -1;
protected MyGridView photograph = null;
List<ViewHolderData> holderDatas = null;
//PatrolReportBackData patrolReportBackData = null;
int viewIndex = 0;
boolean changed = true;
public PatrolAdapter(List<TaskTemplateDetail> list,Context context,List<PatrolReportBackData> patrolReportBackDatas,ApplicationActivity activity)
{
this.list = list;
this.context = context;
this.inflater = LayoutInflater.from(context);
this.patrolReportBackDatas = patrolReportBackDatas;
this.activity = activity;
imageItem = new ArrayList<HashMap<String, Object>>();
holderDatas = new ArrayList<ViewHolderData>();
for (int i = 0; i < patrolReportBackDatas.size(); i++) {
String isNormal = patrolReportBackDatas.get(i).getIsNormal();
if(isNormal != null)
{
bitPhoto = patrolReportBackDatas.get(i).getPhotos();
ViewHolderData viewHolderData = new ViewHolderData();
viewHolderData.setPosition(i);
if(isNormal.equals("0"))
viewHolderData.setSpinnerIndex(2);
else if(isNormal.equals("1"))
viewHolderData.setSpinnerIndex(1);
viewHolderData.setDescription(patrolReportBackDatas.get(i).getFinishDesc());
viewHolderData.setStrBitPhoto(bitPhoto);
if(bitPhoto != null)
{
imageItem = new ArrayList<HashMap<String, Object>>();
String[] strImage = bitPhoto.split(",");
for (String string : strImage) {
if(!string.equals(""))
{
Communal.getInstance().saveImage(context, string);
String imagepath = Environment.getExternalStorageDirectory() + "/MOMSPHOTO/" + Communal.getInstance().getImageName();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true ;
BitmapFactory.decodeFile(imagepath, opts);
opts.inSampleSize = computeSampleSize(opts, - 1 , 80 * 80 );
opts.inJustDecodeBounds = false ;
try {
Bitmap bmp = BitmapFactory.decodeFile(imagepath, opts);
HashMap<String, Object> hashmap = new HashMap<String, Object>();
hashmap.put("ItemImage", bmp);//添加圖像資源的ID
hashmap.put("ItemText", Communal.getInstance().getImageName());//按序號做ItemText
imageItem.add(hashmap);
} catch (OutOfMemoryError err) {
}
}
}
viewHolderData.setImageList(imageItem);
}
holderDatas.add(viewHolderData);
}
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//private List<ViewHolder> holder = new ArrayList<ViewHolder>();
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.i("索引值:", String.valueOf(position));
//final PatrolReportBackData patrolReportBackData = patrolReportBackDatas.get(position);
// viewIndex = position;
index = position;
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_list_patrol, null);
viewHolder = new ViewHolder();
viewHolder.id = (TextView)convertView.findViewById(R.id.item_patrol_id_textView);
viewHolder.name = (TextView)convertView.findViewById(R.id.item_patrol_name_textView);
viewHolder.spinner = (Spinner)convertView.findViewById(R.id.item_patrol_whetherAnomaly);
viewHolder.relativeLayout = (RelativeLayout)convertView.findViewById(R.id.item_relativeLayout_patrol_description);
viewHolder.disposeDescription = (EditText)convertView.findViewById(R.id.item_patrol_disposeDescription);
viewHolder.addPicture = (TextView)convertView.findViewById(R.id.item_patrol_addPicture);
viewHolder.photograph = (MyGridView)convertView.findViewById(R.id.item_patrol_photograph);
viewHolder.spinner.setTag(position);
viewHolder.disposeDescription.setTag(position);
viewHolder.addPicture.setTag(position);
viewHolder.photograph.setTag(position);
viewHolder.spinner.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
// 在TOUCH的UP事件中,要保存當前的行下標,因為彈出軟鍵盤後,整個畫面會被重畫
// 在getView方法的最後,要根據index和當前的行下標手動為EditText設置焦點
if(event.getAction() == MotionEvent.ACTION_UP) {
index= Integer.parseInt(view.getTag().toString());