本文实例为大家分享了android实现音乐播放列表的具体代码,供大家参考,具体内容如下
(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的布局文件内容如下:
"
(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(adapterviewarg0, 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布局文件内容如下:
运行程序,添加音乐信息:
在音乐列表中单击一条记录,弹出警告对话框删除一条记录: