package com.cwbuyer.adapter;

import java.util.LinkedList;
import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.cwbuyer.lib.Utilis;
import com.pwbuyer.main.R;

@SuppressLint("InflateParams")
public class SizeAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	
	private LinkedList<SizeData> mList = new LinkedList<SizeData>();
	
	class SizeData {
		int id;
		String strSize;
	}

	public String getSize(int pos){
		return mList.get(pos).strSize;
	}
	
	public int getId(int pos){
		return mList.get(pos).id;
	}
	
	public SizeAdapter(Context context) {
	      /* °Ñ¼Æªì©l¤Æ */
	      mInflater = LayoutInflater.from(context);
	      getData(context);
	}
	
	public void getData(Context context){
		if(mList != null){
			mList.clear();
		}
		SQLiteDatabase db = Utilis.getDB(context);
		Cursor cursor = db.rawQuery("select * from qc_size", null);
		if(cursor != null){
			try{
				if(cursor.getCount() > 0){
					cursor.moveToFirst();
					for(int i = 0; i < cursor.getCount(); i++){
						SizeData data = new SizeData();
						data.id = cursor.getInt(cursor.getColumnIndex("_ID"));
						data.strSize = cursor.getString(cursor.getColumnIndex("SIZES"));
						mList.add(data);
						cursor.moveToNext();
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				cursor.close();
				cursor = null;
			}
		}
		db.close();
		db = null;
	}
	
	public int getCount() {
//		return mSize.length;
		return mList.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.list_size, null);
			holder = new ViewHolder();
			holder.textName = (TextView) convertView.findViewById(R.id.text_name);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		SizeData data = mList.get(position);
		holder.textName.setText(data.strSize);
		return convertView;
	}

	/* class ViewHolder */
	private class ViewHolder {
		TextView textName;
	}
}
