get Music List from SQLiteDatabase - Android Database

Android examples for Database:SQL Query

Description

get Music List from SQLiteDatabase

Demo Code


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.HashMap;

public class Main{
    public static ArrayList<HashMap<String, String>> getMusicList(
            Context context, String sql, FileUtil.SortKey sortKey) {
        ArrayList<HashMap<String, String>> musicList = new ArrayList<HashMap<String, String>>();
        SQLiteDatabase db = null;/*w w  w. ja v a 2 s . c  o  m*/
        Cursor cursor = null;
        try {
            db = DBUtil.getReadableDB(context, DBUtil.databaseName);
            cursor = DBUtil.rawQueryCursor(db, sql, null);
            final Cursor cursorFinal = cursor;
            if (cursorFinal != null) {
                while (cursorFinal.moveToNext()) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put("path", cursorFinal.getString(cursorFinal
                            .getColumnIndex("path")));
                    map.put("title", cursorFinal.getString(cursorFinal
                            .getColumnIndex("title")));
                    map.put("pinyin", cursorFinal.getString(cursorFinal
                            .getColumnIndex("pinyin")));
                    map.put("album", cursorFinal.getString(cursorFinal
                            .getColumnIndex("album")));
                    map.put("artist", cursorFinal.getString(cursorFinal
                            .getColumnIndex("artist")));
                    if (sortKey == FileUtil.SortKey.PlayList) {
                        map.put("playlist", cursorFinal
                                .getString(cursorFinal
                                        .getColumnIndex("playlist")));
                    }
                    musicList.add(map);
                }
                return musicList;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
        return musicList;
    }
}

Related Tutorials