(1)创建一个数据类工具类DBHelper,该类继承SQLiteOpenHelper,重写onCreate()和onUpgrade()方法,并添加insert()、delete()、query()方法,分别实现数据的添加、删除和查询。DBHelper类的代码如下:
package com.example.musiclist;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class DBHelper extends SQLiteOpenHelper{
private static final String DB_NAME = "music.db"; //数据库名称
private static final String TBL_NAME = "MusicTbl"; //表名
private SQLiteDatabase db; //声明SQLiteDatabase对象
//构造函数
DBHelper(Context c){
super(c, DB_NAME, null, 2);
}
@Override
public void onCreate(SQLiteDatabase db){
//获取SQLiteDatabase对象
this.db = db;
//创建表
String CREATE_TBL = "create table MusicTbl(_id integer primary key autoincrement, name text, singer text)";
db.execSQL(CREATE_TBL);
}
//插入
public void insert(ContentValues values){
SQLiteDatabase db = getWritableDatabase();
db.insert(TBL_NAME, null, values);
db.close();
}
//查询
public Cursor query(){
SQLiteDatabase db = getWritableDatabase();
Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
return c;
}
//删除
public void del(int id){
if(db == null){
db = getWritableDatabase();
}
db.delete(TBL_NAME, "_id=?", new String[]{String.valueOf(id)});
}
//关闭数据库
public void close(){
if(db != null){
db.close();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
(2)创建添加音乐的AddActivity,添加界面提供两个文本框和一个按钮,用于输入音乐名和歌手名,当单击“添加”按钮时,将数据插入到表中,具体代码如下:
package com.example.musiclist;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends Activity {
private EditText et1, et2;
private Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
this.setTitle("添加收藏信息");
et1 = (EditText)findViewById(R.id.EditTextName);
et2 = (EditText)findViewById(R.id.EditTextSinger);
b1 = (Button)findViewById(R.id.ButtonAdd);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 获取用户输入的文本信息
String name = et1.getText().toString();
String singer = et2.getText().toString();
//创建ContentValues对象。封装记录信息 key 和 values 值成对出现
ContentValues values = new ContentValues();
values.put("name", name);
values.put("singer", singer);
//创建数据库工具类DBHelper
DBHelper helper = new DBHelper(getApplicationContext());
//调用insert()方法插入数据
helper.insert(values);
//跳转到QueryActivity,显示音乐列表
Intent intent = new Intent(AddActivity.this, QueryActivity.class);
startActivity(intent);
}
});
}
}
当单击“添加”按钮时,先将用户输入的音乐名和歌手信息封装到ContentValues对象中,再调用DBHelper的insert()方法将记录插入到数据库中,然后跳转到QueryActivity来显示音乐列表。
AddActivity的布局文件内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp">
<TableLayout
android:id="@+id/TableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="1">
"
<TableRow
android:id="@+id/TableRow01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌名"/>
<EditText
android:id="@+id/EditTextName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
TableRow>
<TableRow
android:id="@+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歌手"/>
<EditText
android:id="@+id/EditTextSinger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>
TableRow>
<Button
android:id="@+id/ButtonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"/>
TableLayout>
LinearLayout>
(3)创建显示音乐列表的QueryActivity,具体代码如下:
package com.example.musiclist;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.text.AlteredCharSequence;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class QueryActivity extends ListActivity {
//列表视图
private ListView listView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("浏览音乐列表信息");
final DBHelper helpter = new DBHelper(this);
//获取listview对象,引用变量和实例化对象
listView = getListView();
//查询数据,获取游标
Cursor c = helpter.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.activity_query, c, from, to); //为列表视图添加适配器
listView.setAdapter(adapter);
//提示对话框
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置ListView单击监听器
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView>arg0, View arg1, int arg2, long arg3){
final long temp = arg3;
builder.setMessage("真的要删除该记录吗?").setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//删除数据
helpter.del((int)temp);
//重新查询数据
Cursor c = helpter.query();
String[] from = {"_id", "name", "singer"};
int[] to = {R.id.text0, R.id.text1, R.id.text2};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.activity_query, c, from, to); //为列表视图添加适配器
listView.setAdapter(adapter);
}
}).setNegativeButton("否", null);
AlertDialog ad = builder.create();
ad.show();
}
});
helpter.close();
}
}
上述代码中调用DBHelper的query()方法查询数据库并返回一个Cursor游标,然后使用SimpleCursorAdapter适配器将数据绑定到ListView控件上,并在ListView控件上注册单击监听器,当单击一条记录时,显示一个警告对话框提示是否删除,单击“是”,则调用DBHelper的del()方法删除指定记录。
QueryActivity布局文件内容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25px"/>
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25px"/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25px"/>
LinearLayout>