package com.yyrh.ui.userlist;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yyrh.utils.ResourceUtil;

import java.util.ArrayList;


public class QjUserListAdapter extends BaseAdapter {
	private Context context;
	public ArrayList<User> list;
	private LayoutInflater mInflater = null;
	DatabaseHelper dbHelper;


	public QjUserListAdapter(Context context, ArrayList<User> list) {
		this.context = context;
		this.list = list;
		this.mInflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(ResourceUtil.getLayoutId(context, "hnyy_user_list_item"), parent,false);
			holder = new ViewHolder();

			holder.accountName = convertView
					.findViewById(ResourceUtil.getId(context, "history_account"));
			holder.closeItem =  convertView
					.findViewById(ResourceUtil.getId(context, "item_close"));
			
			holder.closeItem.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					deleteItem(position);
				}
			});
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();

		}
		holder.accountName.setText(list.get(position).uname);
		return convertView;
	}

	class ViewHolder {
		TextView accountName;
		ImageView closeItem;

	}
	private void deleteItem(int position){

		
		dbHelper = new DatabaseHelper(context);
		SQLiteDatabase db = null;
		db = dbHelper.getReadableDatabase();
		String sql = "delete from user where name = '"+list.get(position).uname+"'";
		//执行SQL语句   
		db.execSQL(sql);   
		db.close();
		list.remove(position);
		this.notifyDataSetChanged();
	}

}
