How to get the array from the given class Item?

2019-03-04 19:51发布

问题:

First of all i am a newbie and I am trying to get the media files details using mediastore and saving the details in the List mitems. Here is what I am doing

public class MusicRetriever {
final String TAG = "MusicRetriever";
ContentResolver mContentResolver;
// the items (songs) we have queried
List<Item> mItems = new ArrayList<Item>();

Here I am getting the file details from internal and external uri and saving them in arraylist. But I am not able to get the details of particular column. This is what i am trying

public class Item {
long id;
String artist;
String title;
String album;
long duration;

public Item(long id, String artist, String title, String album, long duration) {
    this.id = id;
    this.artist = artist;
    this.title = title;
    this.album = album;
    this.duration = duration;
}

public long getId() {
    return id;
}

public String getArtist() {
    return artist;
}

public String getTitle() {
    return title;
}

public String getAlbum() {
    return album;
}

public long getDuration() {
    return duration;
}

public Uri getURI() {
    return ContentUris.withAppendedId(
            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
}
}

How I can get the following details as an array from Item class in another class or fragment.

回答1:

you can get your array item like this. exthis is the example

for(int i =0; i<= mItem.size(); i++){
        String songName = mItem.get(i).getArtist();
        String songAlbum = mItem.get(i).getAlbum();
    }
  1. if you want set your data to listview you can set like this.

    first set this variable globally.

    ListAdapter myadapter;

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

    and in onCreate Method use like this:

 for(int i = 0; i<=mItem.size();i++)
 {
     songList.add(mItem.get(i).getArtist());
 }

 myadapter = new ArrayAdapter<String>         
(YourActivity.this, android.R.layout.simple_list_item_1, songList);

I hope it works.