android数据库

// android数据库处理  -- 初步


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


public class MainActivity extends Activity {
	private EditText edt1, edt2;
	private Button btn;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main);
		// 设置标题
		this.setTitle("录入歌曲信息");
		// 获取控件
		edt1 = (EditText) findViewById(R.id.EditTextName);
		edt2 = (EditText) findViewById(R.id.EditTextSinger);
		btn = (Button) findViewById(R.id.ButtonAdd);
		
		// 设置监听器
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 获得输入的内容,把它赋给变量
				String name = edt1.getText().toString();
				String singer = edt2.getText().toString();
				// 创建ContentValues对象,封装记录信息
				ContentValues value = new ContentValues();
				value.put("name", name);
				value.put("singer", singer);
				// 创建数据库工具类DBHelper
				DBHelper helper = new DBHelper(getApplicationContext());
				// 插入数据库中
				helper.insert(value);
				// 跳转到显示窗口
				Intent intent = new Intent(MainActivity.this,
				queryActivity.class);
				startActivity(intent);
			}
		});
	}
}


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


public class queryActivity extends ListActivity {
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setTitle("浏览信息列表");
		final DBHelper helper = new DBHelper(this);
		// 查询数据,获取游标
		Cursor c = helper.query();
		// 列表项目数组
		String[] from = { "_id", "name", "singer" };
		// 列表项ID
		int[] to = { R.id.text0, R.id.text1, R.id.text2 };
		// 适配器
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, c, from, to);
		// 列表视图
		ListView listView = getListView();
		// 为列表视图添加视图
		listView.setAdapter(adapter);
		// 设置提示对话框
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);


		listView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
				// TODO Auto-generated method stub
				final long temp = id;
				builder.setMessage("真的要删除吗?")
					.setPositiveButton("是", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub
							// 删除数据库
							helper.delecte((int) temp);
							Cursor c = helper.query();
							String[] from = { "_id", "name", "singer" };
							int[] to = { R.id.text0, R.id.text1, R.id.text2 };
							
							SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.row, c, from, to);
							ListView listView = getListView();
							listView.setAdapter(adapter);
							adapter.notifyDataSetChanged();
						}
				})
					.setNegativeButton("否", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub


						}
				});
				
				AlertDialog ad = builder.create();
				ad.show();
			}
		});
		
		helper.close();


	}
}


// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


public class DBHelper extends SQLiteOpenHelper {

	// 数据库名称
	private static final String DB_NAME = "music.db";
	// 表名
	private static final String DB_TABLE = "Music";
	// 声明SQLite对象
	private SQLiteDatabase db;


	public DBHelper(Context mContext) {
		super(mContext, DB_NAME, null, 2);
		// TODO Auto-generated constructor stub
	}


	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		this.db = db;
		db.execSQL("create table Music(_id integer primary key autoincrement," + "name text not null, " + "singer text )");
	}


	@Override
	public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
		// TODO Auto-generated method stub

	}


	// 插入
	public void insert(ContentValues values) {
		SQLiteDatabase db = getWritableDatabase();
		db.insert(DB_TABLE, null, values);
		db.close();
	}


	// 查询
	public Cursor query() {
		SQLiteDatabase db = getWritableDatabase();
		Cursor c = db.query(DB_TABLE, null, null, null, null, null, null);
		return c;
	}


	// 删除
	public void delecte(int id) {
		if (db == null){
			db = getWritableDatabase();
			db.delete(DB_TABLE, "_id=?", new String[] { String.valueOf(id) });
		}
	}


	// 关闭数据库
	public void close() {
		if (db != null)
		db.close();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值