Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. private ArrayList<MediaFileInfo> audioList = new ArrayList<>();
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. External();
  9. ListView view = (ListView) findViewById(R.id.listView);
  10. ListAdapter listAdapter = new CustomeAdapter(this, audioList);
  11. view.setAdapter(listAdapter);
  12. }
  13. private void External() {
  14. try {
  15. String[] proj = {MediaStore.Audio.Media._ID,
  16. MediaStore.Audio.Media.TITLE,
  17. MediaStore.Audio.Media.ARTIST,
  18. MediaStore.Audio.Media.ALBUM};// Can include more data for more details and check it.
  19.  
  20. String selection = MediaStore.Audio.Media.DURATION + ">=90000";
  21.  
  22. String[] selectionArgs = null;
  23.  
  24. String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
  25.  
  26. Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, selection, selectionArgs, sortOrder);
  27.  
  28. if (audioCursor != null) {
  29. if (audioCursor.moveToFirst()) {
  30. do {
  31. int audioTitle = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
  32. int audioartist = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
  33. int audioalbum = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
  34. MediaFileInfo info = new MediaFileInfo();
  35. info.setTitle(audioCursor.getString(audioTitle));
  36. info.setAlbum(audioCursor.getString(audioalbum));
  37. info.setArtist(audioCursor.getString(audioartist));
  38. audioList.add(info);
  39. } while (audioCursor.moveToNext());
  40. }
  41. }
  42. audioCursor.close();
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48.  
  49. public class MediaFileInfo {
  50. private String title,artist,album;
  51.  
  52. public String getTitle() {
  53. return title;
  54. }
  55.  
  56. public void setTitle(String title) {
  57. this.title = title;
  58. }
  59.  
  60. public String getArtist() {
  61. return artist;
  62. }
  63.  
  64. public void setArtist(String artist) {
  65. this.artist = artist;
  66. }
  67.  
  68. public String getAlbum() {
  69. return album;
  70. }
  71.  
  72. public void setAlbum(String album) {
  73. this.album = album;
  74. }
  75.  
  76. public class CustomeAdapter extends ArrayAdapter {
  77. private ArrayList<MediaFileInfo> audioList = new ArrayList<>();
  78.  
  79. public CustomeAdapter(Context context, int resource) {
  80. super(context, resource);
  81. }
  82.  
  83. public CustomeAdapter(MainActivity context, ArrayList<MediaFileInfo> audioList) {
  84. super(context, R.layout.custome_list, audioList);
  85. }
  86.  
  87. @Override
  88. public View getView(int position, View convertView, ViewGroup parent) {
  89.  
  90. LayoutInflater imthebest = LayoutInflater.from(getContext());
  91. @SuppressLint("ViewHolder") View custome = imthebest.inflate(R.layout.custome_list, parent, false);
  92. MediaFileInfo item = audioList.get(0);
  93. String item1 = (String) getItem(position);
  94. TextView text1 = (TextView) custome.findViewById(R.id.textView);
  95. text1.setText(item1);
  96.  
  97.  
  98. return custome;
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement