显示所有歌曲来自SD卡Genre Wise

时间:2012-03-20 10:54:08

标签: android android-listview expandablelistview

我正在做音乐播放器应用程序。并希望显示所有歌曲的类型。如果可能的话请给我一些暗示。我能够显示所有与艺术家和专辑相关的歌曲,但在寻找类型明智歌曲时面临问题。我的输出显示每种类型的所有歌曲类别。根据流派来说,它并不是在颂扬歌曲。我的代码在下面。 LocalGenre.java

package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class LocanGenre extends Activity {

    ExpandableListView listLocalArtists;
    TextView txttitle;
    Cursor musiccursor, musiccursor1;
    int music_column_index, music_column_index1;
    int count, count1;

    ArrayList<String> genresName = new ArrayList<String>();
    ArrayList<String> genreID = new ArrayList<String>();
    ArrayList<Integer> albumID = new ArrayList<Integer>();
    ArrayList<String> numberOFSongs = new ArrayList<String>();
    ArrayList<String> artistName = new ArrayList<String>();
    ArrayList<String> path = new ArrayList<String>();
    ArrayList<String> path12 = new ArrayList<String>();
    ArrayList<ArrayList<String>> pathDisplay = new ArrayList<ArrayList<String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.localartists);

        txttitle = (TextView) findViewById(R.id.title);

        txttitle.setText("Genres");

        listLocalArtists = (ExpandableListView) findViewById(R.id.listView1);

        init_phone_music_grid();

        listLocalArtists.setAdapter(new ExpandableListGenreAdapter(this, path, genresName,
                genresName, pathDisplay,albumID));
    }

    private void init_phone_music_grid() {
        // TODO Auto-generated method stub

        System.gc();

        String[] proj = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.ALBUM_ID};

        musiccursor1 = managedQuery(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null,
                null);

        count1 = musiccursor1.getCount();

        if (count1 > 0) {
            musiccursor1.moveToFirst();
            do {

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

                String filename0 = musiccursor1.getString(music_column_index1);
                path.add(filename0);

                Log.i("LocalGenres  ", "Path  Main" + path);

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media._ID);

                String filename123 = musiccursor1
                        .getString(music_column_index1);
                path12.add(filename123);

                Log.i("LocalGenre", "Media ID  " + path12);

                music_column_index1 = musiccursor1
                        .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

                int filename1 = musiccursor1.getInt(music_column_index1);


                albumID.add(filename1);
                Log.i("LOCAL Genres!!!", " ALBUM ID" + albumID);


            } while (musiccursor1.moveToNext());
        }

        String[] projection = { MediaStore.Audio.Genres._ID,
                MediaStore.Audio.Genres.NAME};

        musiccursor = managedQuery(
                MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, projection, null,
                null, null);

        genresName.clear();

        count = musiccursor.getCount();

        if (count > 0) {
            musiccursor.moveToFirst();
            do {

                music_column_index = musiccursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);

                String filename = musiccursor.getString(music_column_index);
                if(!genreID.contains(filename))
                {
                genreID.add(filename);
                }

                Log.i("Local Genres  ", "Genre ID" + genreID);

                music_column_index = musiccursor
                        .getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);

                String filename1 = musiccursor.getString(music_column_index);
                if(!genresName.contains(filename1))
                {
                genresName.add(filename1);
                }

                Log.i("Local Genres  ", "Genres Name  " + genresName);

                /*
                 * music_column_index = musiccursor
                 * .getColumnIndexOrThrow(MediaStore.Audio.Genres._COUNT);
                 * 
                 * String filename3 = musiccursor.getString(music_column_index);
                 * artistName.add(filename3);
                 * 
                 * Log.i("Local Albums  ", "Album ID for Gen  " + artistName);
                 */

            } while (musiccursor.moveToNext());
        }

        for (int j = 0; j < genreID.size(); j++) {

            ArrayList<String> arr = new ArrayList<String>();

            for (int i = 0; i < path12.size(); i++) {
                Log.i("EEEEEE", "Inside If path12.get(i) :"+path12.get(i));
                Log.i("EEEEEE", "Inside If genreID.get(j) :"+genreID.get(j));
                Log.i("EEEEEE", "Inside If Integer.parseInt(path12.get(i)) :"+Integer.parseInt(path12.get(i)));
                Log.i("EEEEEE", "Inside If j : "+j);
                if (path12.get(i).equalsIgnoreCase(genreID.get(j)) || Integer.parseInt(path12.get(i))>j) {

                    Log.i("EEEEEE", "Inside If");
                    arr.add(path.get(i));
                }

                else
                    Log.i("xxxxxxx", "Inside else");
                arr.add(path.get(i));
            }

            Log.i("EEEEEE", "Inside outerloop " + arr);

            pathDisplay.add(arr);
        }
    }
}

ExpandableListGenreAdapter.java

    package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListGenreAdapter  extends BaseExpandableListAdapter{

    private Context context;

    private ArrayList<String> artist;

    private ArrayList<String> genres;

    private ArrayList<ArrayList<String>> children;

    public ArrayList<String> pathmain ;

    public ArrayList<Integer> genresID;

    public ArrayList<Integer> albumID;

    public ExpandableListGenreAdapter(Context context, ArrayList<String> path, ArrayList<String> groups,ArrayList<String> artist,
            ArrayList<ArrayList<String>> children, ArrayList<Integer> albumID) {
        this.context = context;
        this.genres = groups;
        this.artist = artist;
        this.pathmain = path;
        this.children = children;
        this.albumID=albumID;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    /*public Bitmap getAlbumart(int album_id) 
       {
            Bitmap bm = null;
            try 
            {
                final Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");

                Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

                ParcelFileDescriptor pfd = context.getContentResolver()
                    .openFileDescriptor(uri, "r");

                if (pfd != null) 
                {
                    FileDescriptor fd = pfd.getFileDescriptor();
                    bm = BitmapFactory.decodeFileDescriptor(fd);
                }
        } catch (Exception e) {
        }
        return bm;
    }*/

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final String vehicle = (String) getChild(groupPosition, childPosition);

        Log.i("ExpandableListAdapter", "Group Position  "+groupPosition);

        Log.i("ExpandableListAdapter", "Vehicle  "+vehicle);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }

        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        ImageView imageview1=(ImageView)convertView.findViewById(R.id.ImageView01);
   //     bm=getAlbumart(albumids.get(1));
       // Log.i("LIST ADAPTER","@@@@@@@@@@@@@@@@@@@ALBUM IDS "+albumids.get(0)+"BITMAPPPPP@@@"+bm);

       // imageview1.setImageBitmap(coverart.get(childPosition));
        tv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                MainActivity.flag = true;

                TestFragment3.flag = true;

                Firstpage.flag = true;

                Log.i("ExpandableListGenreAdapter", "path  "+childPosition);

                MainActivity.currentPosition = groupPosition;

                Log.i("ExpandableListGenreAdapter", "currentPosition  "+MainActivity.currentPosition);

                MainActivity.genre=true;
                MainActivity.currentgenreposition = albumID.get(childPosition);

                Log.i("ExpandableListGenreAdapter", "currentGenrePosition  "+MainActivity.currentgenreposition);

                MainActivity.Media_full_path = "/sdcard/"+vehicle;

                Log.i("ExpandableListAdapter", "Onclick  "+MainActivity.Media_full_path);

                ((Activity)context).finish();


            }
        });

        tv.setText("   " + vehicle.toString());

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return children.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return genres.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return genres.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
         String group = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.group_layout, null);
            }
            TextView txtArtistsName = (TextView) convertView.findViewById(R.id.txtArtistsName);
            TextView txtartistssongs = (TextView) convertView.findViewById(R.id.txtartistssongs);
            txtArtistsName.setText(group);
            txtartistssongs.setText(genres.get(groupPosition)+" Song(s)");
            return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

请建议我上述代码中缺少哪里。谢谢。

2 个答案:

答案 0 :(得分:11)

希望这会对你有所帮助。它显示了流派和他们的歌曲。

int index;
long genreId;
Uri uri;
Cursor genrecursor;
Cursor tempcursor;
String[] proj1 = {MediaStore.Audio.Genres.NAME, MediaStore.Audio.Genres._ID};     
String[] proj2 = {MediaStore.Audio.Media.DISPLAY_NAME};

genrecursor = managedQuery(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, proj1, null, null, null);
if (genrecursor.moveToFirst()) {
  do {
    index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);              
    Log.i("Tag-Genre name", genrecursor.getString(index));

    index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);               
    genreId = Long.parseLong(genrecursor.getString(index));
    uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

    tempcursor = managedQuery(uri, proj2, null,null,null);
    Log.i("Tag-Number of songs for this genre", tempcursor.getCount() + "");
    if (tempcursor.moveToFirst()) {
      do {
        index = tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
        Log.i("Tag-Song name", tempcursor.getString(index));
      } while(tempcursor.moveToNext());
    }
  } while(genrecursor.moveToNext());       
}

答案 1 :(得分:1)

我更新了@Abhijeet的答案,以便使用旧版本

public void getGenres(int total) {
  int index;
  long genreId;
  Uri uri;
  Cursor genrecursor;
  Cursor tempcursor;
  String[] proj1 = {MediaStore.Audio.Genres.NAME, MediaStore.Audio.Genres._ID};   
  String[] proj2={MediaStore.Audio.Media.DISPLAY_NAME};
  String result;

  genrecursor = MyApplication.getAppContext().getContentResolver().query(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, proj1, null, null, null);
  ArrayList<Genre> genres = new ArrayList<Genre>();
  Genre genre = null;

  if (genrecursor!=null && genrecursor.moveToFirst()) {
    do {
      genre = new Genre();
      index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);            
      if (BuildConfig.DEBUG)  Log.i("Tag-Genre name", genrecursor.getString(index));
      genre.setName(genrecursor.getString(index));
      if(genre.getName().equalsIgnoreCase("")) {
        genre.setName("no-genre");
      }

      index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);             
      genreId = Long.parseLong(genrecursor.getString(index));
      uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

      tempcursor =  MyApplication.getAppContext().getContentResolver().query(uri, proj2, null, null, null);
      if (BuildConfig.DEBUG)  Log.i("Tag-Number of songs for this genre", tempcursor.getCount()+"");
      genre.setNumberSongs( tempcursor.getCount());


      if (!genres.contains(genre)) {
        genres.add(genre);
      }
      if (tempcursor.moveToFirst()) {
        do {
          index = tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
          if (BuildConfig.DEBUG)  Log.i("Tag-Song name", tempcursor.getString(index));
          for(int i = 0;i<songs.size();i++) {
            if (BuildConfig.DEBUG)  Log.i("SONG", songs.get(i).getDisplayName()+ " - "+ tempcursor.getString(index));
            if (tempcursor!=null && songs!=null &&  tempcursor.getString(index).equalsIgnoreCase(songs.get(i).getDisplayName()) ) {                             
              songs.get(i).setGenre(genre.getName());
              if (genre.isOldVersion()) {
                genre.setNumberSongs(genre.getNumberSongs()+1);
              }
              if (genre.getNumberSongs()==0) { //Is an oldversion of android, less than 3.0
                genre.setOldVersion(true);
                genre.setNumberSongs(1);
              }

            }
          }                      
        } while(tempcursor.moveToNext());
      }
    } while(genrecursor.moveToNext());       
  }

 orderList(genres);
 result = "";
 for(int i = 0; i < 10 && i<genres.size()  ;i++) {
    result += genres.get(i).toString()+",";
  }
  result += "Total:"+total;
}