package com.szu;

import com.szu.accountsoft.dao.DBOpenHelper;
import com.szu.accountsoft.dao.FlagDAO;
import com.szu.accountsoft.model.Tb_flag;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class FlagManage extends Activity {
	EditText txtFlag;// 创建EditText对象
	Button btnEdit, btnDel;// 创建两个Button对象
	TextView time,location;

	
	DBOpenHelper helper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.flagmanage);// 设置布局文件
		txtFlag = (EditText) findViewById(R.id.txtFlag);// 获取便签文本框
		btnEdit = (Button) findViewById(R.id.btnFlagManageEdit);// 获取修改按钮
		btnDel = (Button) findViewById(R.id.btnFlagManageDelete);// 获取删除按钮
		time = (TextView) findViewById(R.id.time);
		location = (TextView) findViewById(R.id.location);
		
		setTitle("请修改你的信息");
		Intent intent = getIntent();// 创建Intent对象
		final String id = intent.getStringExtra("id");
		displayData(id);
		
		
		final FlagDAO flagDAO = new FlagDAO(FlagManage.this);// 创建FlagDAO对象

		btnEdit.setOnClickListener(new OnClickListener() {// 为修改按钮设置监听事件
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Tb_flag tb_flag = new Tb_flag();// 创建Tb_flag对象
				tb_flag.setid(Integer.parseInt(id));// 设置便签id
				tb_flag.setFlag(txtFlag.getText().toString());// 设置便签值
				flagDAO.update(tb_flag);// 修改便签信息
				// 弹出信息提示
				Toast.makeText(FlagManage.this, "数据修改成功！",Toast.LENGTH_SHORT).show();
			}
		});

		btnDel.setOnClickListener(new OnClickListener() {// 为删除按钮设置监听事件
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				flagDAO.detele(Integer.parseInt(id));// 根据指定的id删除便签信息
				Toast.makeText(FlagManage.this, "数据删除成功！",Toast.LENGTH_SHORT).show();
				finish();
			}
		});
	}

	private void displayData(String id) {
		// TODO Auto-generated method stub
		helper = new DBOpenHelper(this);
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from tb_flag where _id = ? ;";
		Cursor cursor = db.rawQuery(sql, new String[]{id});
		cursor.moveToFirst();
		
		String flag = cursor.getString(cursor.getColumnIndex("flag"));
		String time_get = cursor.getString(cursor.getColumnIndex("time"));
		String address = cursor.getString(cursor.getColumnIndex("address"));
		
		txtFlag.setText(flag);
		time.setText(time_get);
		location.setText(address);
	}

}
