欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android小程序?qū)崿F(xiàn)音樂播放列表

 更新時間:2020年05月22日 16:50:17   作者:adorable_  
這篇文章主要為大家詳細(xì)介紹了Android小程序?qū)崿F(xiàn)音樂播放列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)音樂播放列表的具體代碼,供大家參考,具體內(nèi)容如下

(1)創(chuàng)建一個數(shù)據(jù)類工具類DBHelper,該類繼承SQLiteOpenHelper,重寫onCreate()和onUpgrade()方法,并添加insert()、delete()、query()方法,分別實現(xiàn)數(shù)據(jù)的添加、刪除和查詢。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"; //數(shù)據(jù)庫名稱
 private static final String TBL_NAME = "MusicTbl"; //表名
 private SQLiteDatabase db; //聲明SQLiteDatabase對象

 //構(gòu)造函數(shù)
 DBHelper(Context c){
  super(c, DB_NAME, null, 2);
 }

 @Override
 public void onCreate(SQLiteDatabase db){
  //獲取SQLiteDatabase對象
  this.db = db;
  //創(chuàng)建表
  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)});
 }

 //關(guān)閉數(shù)據(jù)庫
 public void close(){
  if(db != null){
   db.close();
  }
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 }
}

(2)創(chuàng)建添加音樂的AddActivity,添加界面提供兩個文本框和一個按鈕,用于輸入音樂名和歌手名,當(dāng)單擊“添加”按鈕時,將數(shù)據(jù)插入到表中,具體代碼如下:

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();

    //創(chuàng)建ContentValues對象。封裝記錄信息 key 和 values 值成對出現(xiàn)
    ContentValues values = new ContentValues();
    values.put("name", name);
    values.put("singer", singer);

    //創(chuàng)建數(shù)據(jù)庫工具類DBHelper
    DBHelper helper = new DBHelper(getApplicationContext());

    //調(diào)用insert()方法插入數(shù)據(jù)
    helper.insert(values);

    //跳轉(zhuǎn)到QueryActivity,顯示音樂列表
    Intent intent = new Intent(AddActivity.this, QueryActivity.class);
    startActivity(intent);
   }
  });
 }
}

當(dāng)單擊“添加”按鈕時,先將用戶輸入的音樂名和歌手信息封裝到ContentValues對象中,再調(diào)用DBHelper的insert()方法將記錄插入到數(shù)據(jù)庫中,然后跳轉(zhuǎn)到QueryActivity來顯示音樂列表。

AddActivity的布局文件內(nèi)容如下:

<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)創(chuàng)建顯示音樂列表的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();

  //查詢數(shù)據(jù),獲取游標(biāo)
  Cursor c = helpter.query();

  //列表項數(shù)組
  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);

  //設(shè)置ListView單擊監(jiān)聽器
  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) {
      //刪除數(shù)據(jù)
      helpter.del((int)temp);
      //重新查詢數(shù)據(jù)
      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();
 }
}

上述代碼中調(diào)用DBHelper的query()方法查詢數(shù)據(jù)庫并返回一個Cursor游標(biāo),然后使用SimpleCursorAdapter適配器將數(shù)據(jù)綁定到ListView控件上,并在ListView控件上注冊單擊監(jiān)聽器,當(dāng)單擊一條記錄時,顯示一個警告對話框提示是否刪除,單擊“是”,則調(diào)用DBHelper的del()方法刪除指定記錄。

QueryActivity布局文件內(nèi)容如下:

<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>

運行程序,添加音樂信息:

在音樂列表中單擊一條記錄,彈出警告對話框刪除一條記錄:

更多關(guān)于播放器的內(nèi)容請點擊《java播放器功能》進(jìn)行學(xué)習(xí)。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android開發(fā)之Service用法實例

    Android開發(fā)之Service用法實例

    這篇文章主要介紹了Android開發(fā)之Service用法,實例分析了Android中Service的功能及使用技巧,需要的朋友可以參考下
    2015-05-05
  • android textview 顯示html方法解析

    android textview 顯示html方法解析

    現(xiàn)在網(wǎng)絡(luò)的繁盛時代,光文字是不能滿足人們的胃口的,圖片,flash,音頻,視頻就成為瀏覽網(wǎng)頁的主流顯示,在手機上也一樣,本文將詳細(xì)介紹此功能的實現(xiàn)方法
    2012-11-11
  • Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法

    Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法

    這篇文章主要介紹了Android開發(fā)使用自定義View將圓角矩形繪制在Canvas上的方法,結(jié)合實例形式分析了Android自定義view繪制圓角矩形的相關(guān)方法與使用技巧,需要的朋友可以參考下
    2017-10-10
  • Android編程實現(xiàn)仿iphone抖動效果的方法(附源碼)

    Android編程實現(xiàn)仿iphone抖動效果的方法(附源碼)

    這篇文章主要介紹了Android編程實現(xiàn)仿iphone抖動效果的方法,結(jié)合實例形式分析了仿iphone抖動效果的頁面布局及功能實現(xiàn)技巧,并附帶實例源碼供讀者下載,需要的朋友可以參考下
    2015-11-11
  • Android UI使用HTML布局方法實例

    Android UI使用HTML布局方法實例

    這篇文章主要介紹了Android UI使用HTML布局方法實例,布局文件直接用一個WebView來代替,這樣就可以在WebView中使用HTML布局了,需要的朋友可以參考下
    2015-05-05
  • Android 中Crash時如何獲取異常信息詳解及實例

    Android 中Crash時如何獲取異常信息詳解及實例

    這篇文章主要介紹了Android 中Crash時如何獲取異常信息詳解及實例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android studio刪除Android項目方法

    Android studio刪除Android項目方法

    在本篇內(nèi)容里我們給大家介紹的是關(guān)于Android studio刪除Android項目方法和步驟,需要的可以學(xué)習(xí)下。
    2018-12-12
  • 詳細(xì)分析Fresco源碼之圖片加載流程

    詳細(xì)分析Fresco源碼之圖片加載流程

    Fresco是一個強大的圖片加載組件,使用它之后,你不需要再去關(guān)心圖片的加載和顯示這些繁瑣的事情!它支持Android2.3及以后的版本
    2021-01-01
  • Android優(yōu)化提升應(yīng)用啟動速度及Splash頁面的設(shè)計

    Android優(yōu)化提升應(yīng)用啟動速度及Splash頁面的設(shè)計

    這篇文章主要介紹了Android性能優(yōu)化的一些相關(guān)資料,文章圍繞提升應(yīng)用啟動速度及Splash頁面的設(shè)計的內(nèi)容展開介紹,需要的朋友可以參考一下,希望對你有所幫助
    2021-12-12
  • Android SharedPreferences數(shù)據(jù)存儲詳解

    Android SharedPreferences數(shù)據(jù)存儲詳解

    SharedPreferences是安卓平臺上一個輕量級的存儲類,用來保存應(yīng)用的一些常用配置,比如Activity狀態(tài),Activity暫停時,將此activity的狀態(tài)保存到SharedPereferences中;當(dāng)Activity重載,系統(tǒng)回調(diào)方法onSaveInstanceState時,再從SharedPreferences中將值取出
    2022-11-11

最新評論