新手android中ListView实现音乐列表

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

  1. main.xml

        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android :color/holo_blue_light"
        tools:context="com.example.mymusiclist.MainActivity$PlaceholderFragment" >

                android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="21sp"
            android:text="音乐列表" />
                android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView1"  
            >

    listview.xml(布局xml)


        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

                android:id="@+id/music_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:text="music_title" />

                android:id="@+id/ll1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:orientation="horizontal" >

                        android:id="@+id/music_artist"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="3dp"
                android:text="music_artist" />

                        android:id="@+id/music_duration"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:text="music_duration" />
       




  2. MainActivity.java

    package com.example.mymusiclist;


    import java.util.ArrayList;
    import java.util.List;

    import android.support.v7.app.ActionBarActivity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.widget.ListView;
    import android.provider.MediaStore;

    public class MainActivity extends ActionBarActivity {
    private ListView listView;
    private ArrayList musicList = new ArrayList();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_main);
            listView = (ListView) findViewById(R.id.listView1);
            musicList = getMp3Infos();
            MusicListAdapter adapter = new MusicListAdapter(this, musicList);
            listView.setAdapter(adapter);
        }
        public ArrayList getMp3Infos() {  //获取音乐列表信息
            Cursor cursor = getContentResolver().query(  
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,  
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);  
            ArrayList mp3Infos = new ArrayList();  
            for (int i = 0; i < cursor.getCount(); i++) {  
                MusicMp3 mp3Info = new MusicMp3();  
                cursor.moveToNext();  
                long id = cursor.getLong(cursor  
                    .getColumnIndex(MediaStore.Audio.Media._ID));   //音乐id  
                String title = cursor.getString((cursor   
                    .getColumnIndex(MediaStore.Audio.Media.TITLE)));//音乐标题  
                String artist = cursor.getString(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家  
                long duration = cursor.getLong(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.DURATION));//时长  
                long size = cursor.getLong(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.SIZE));  //文件大小  
                String url = cursor.getString(cursor  
                    .getColumnIndex(MediaStore.Audio.Media.DATA));              //文件路径  
            int isMusic = cursor.getInt(cursor  
                .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐  
            if (isMusic != 0) {     //只把音乐添加到集合当中  
                mp3Info.setId(id);  
                mp3Info.setTitle(title);  
                mp3Info.setArtist(artist);  
                mp3Info.setDuration(duration);  
                mp3Info.setSize(size);  
                mp3Info.setUrl(url);  
                mp3Infos.add(mp3Info);  
                }  
            }  
        return mp3Infos;  
        }  

    }


  3. MusicListAdapter.java

    package com.example.mymusiclist;

    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;

    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.TextView;

    public class MusicListAdapter extends BaseAdapter {
        private final static String TAG = "MusicListAdapter";
        private LayoutInflater inflater;
        private Context mContext;
        private ArrayList mList;
    public MusicListAdapter(Context context, ArrayList list){
        mContext = context;
        mList = list;
        inflater = LayoutInflater.from(context);
    }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return mList.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view;
            if(convertView != null){
                view = convertView;
            } else {
                view = inflater.inflate(R.layout.listview_item, parent, false);
            }
            TextView  title = (TextView)view.findViewById(R.id.music_title);
            TextView artist = (TextView) view.findViewById(R.id.music_artist);
            TextView duration = (TextView) view.findViewById(R.id.music_duration);
            String music_title = mList.get(position).getTitle();
            String music_artist = mList.get(position).getArtist();
            long music_duration = mList.get(position).getDuration();
            title.setText(music_title);
            if(music_artist.equals("")){            
                artist.setText("未知艺术家");
            }else{
                artist.setText(music_artist);
            }
            Log.d(TAG, "music_artist============="+music_artist);
            duration.setText(getStringTime(music_duration));
            return view;
        }
    public String getStringTime(long duration){
           SimpleDateFormat sDateFormat = new SimpleDateFormat("mm:ss");
           return sDateFormat.format(new Date(duration));
    }
    }



  4. 属性实体MusicMp3.java

    package com.example.mymusiclist;

    public class MusicMp3 {

    //属性值,生成get和set方法  构造方法
        long id;
        long duration;
        long size;
        String title;
        String url;
        String artist;
        public MusicMp3(){ }
        
        public MusicMp3(long id, long duration, long size, String title, String url,
                String artist) {
            super();
            this.id = id;
            this.duration = duration;
            this.size = size;
            this.title = title;
            this.url = url;
            this.artist = artist;
        }

        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public long getDuration() {
            return duration;
        }
        public void setDuration(long duration) {
            this.duration = duration;
        }
        public long getSize() {
            return size;
        }
        public void setSize(long size) {
            this.size = size;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getUrl() {
            return url;
        }
        public void setUrl(String url) {
            this.url = url;
        }
        public String getArtist() {
            return artist;
        }
        public void setArtist(String artist) {
            this.artist = artist;
        }
        @Override
        public String toString() {
            return "Mp3Info [id=" + id + ", duration=" + duration + ", size="
                    + size + ", title=" + title + ", url=" + url + ", artist="
                    + artist + "]";
        }
        
        
        
    }


转载于:https://my.oschina.net/u/1422716/blog/649423

你可能感兴趣的:(新手android中ListView实现音乐列表)