Android realizes reading all the music in the phone's memory

Source: Internet
Author: User

The following steps are required to read the music information in the phone's memory:

1) Create an XML file that holds the retrieved information, using the ListView control

2) Create a new XML file to lay out the item object in the ListView

3) need to create a music class to hold the primary information for the music that needs to be acquired

4) need to create a helper class to get the music information from the phone and pass the obtained information through a collection class

5) Create a custom adapter to load each child item of the ListView

6) load the data and return to the user interface

The main code implementations are as follows:

First, Help class module

Public arraylist<object> getmusic (Context context)  {  arraylist =  new ArrayList<Object> ();   resolver = context.getcontentresolver ();   cursor = resolver.query (mediastore.audio.media.external_content_uri,     Null, null, null, mediastore.audio.media.default_sort_order);  if  (cursor ! = null)  {   while  (Cursor.movetonext ())  {    musicinfo  = new musicinfo ();     musicinfo.setsinger_name (cursor.getString (cursor       .getcolumnindex (MediaStore.Audio.Media.ARTIST));//  singer name      musicinfo.setsong_name (Cursor.getstring (Cursor      .getcolumnindex ( MediaStore.Audio.Media.TITLE));//  Song name     musicinfo.setsong_size ( Formatchange.formatsize (CURSOR&NBSP;&NBSP;&NBsp;   .getint (Cursor        .getcolumnindex ( MediaStore.Audio.Media.SIZE)));//  the size of the song     musicInfo       .setsong_time (Formatchange.totime (Cursor.getint (cursor         Getcolumnindexorthrow (MediaStore.Audio.Media.DURATION)));//  song Time     musicinfo.seturl (Cursor.getstring (Cursor      .getcolumnindex (MediaStore.Audio.Media.DATA)));     arraylist.add (Musicinfo);   }  }     Cursor.close ();   return arraylist; }

I do it in a normal way, or I can use a static implementation.

Second, the adapter module

Private arraylist<object> arraylist; private context context; private  TextView music_singer,music_name,music_size; private MusicInfo musicInfo;  Public music_adapter (context context,arraylist<object> arraylist) {   this.context=context;  this.arraylist=arraylist; }  @Override  public int  GetCount ()  {  return arraylist.size (); }  @Override  public Object  GetItem (int position)  {  return arraylist.get (position); }  @Override   Public long getitemid (int id)  {  return id; }  @Override   Public view getview (INT&NBSP;POSITION,&NBSP;VIEW&NBSP;VIEW,&NBSP;VIEWGROUP&NBSP;ARG2)  {   if  (View==null)  {   view=layoutinflater.from (context). Inflate (r.layout.music_ Format,null);   }     music_name= (TextView)  view.findviewbyid (r.id.music_name);   music_singer= (TextView)   View.findviewbyid (R.id.music_singer);   music_size= (TextView)  view.findviewbyid (R.id.music_size) ;   musicinfo= (Musicinfo)  arraylist.get (position);   music_name.settext ( Musicinfo.getsong_name ());   music_singer.settext (Musicinfo.getsinger_name () + "|" +musicinfo.getsong_time ());   music_size.settext (Musicinfo.getsong_size ());  //it must be converted to a string type.    return view; }

The basic realization of the ability to read the song information, as long as the main interface to set up the adapter.

Android realizes reading all the music in the phone's memory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.