Improved the music player 2.0 and the music player 2.0
Compared with the music player 1.0, the 2.0 version has made the following improvements:
Extract the Retrieved Data
Added Music information to the data category class MusicDao.
/*** Music information Data category class * Data Access Object */public class MusicDao {public List
GetMusicList () {List
Musics = new ArrayList <> (); if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {File musicDir = Environment. getExternalStoragePublicDirectory (Environment. DIRECTORY_MUSIC); if (musicDir. exists () {File [] files = musicDir. listFiles (); if (files! = Null & files. length> 0) {String title = ""; for (int I = 0; I
When loading data
MusicDao dao = new MusicDao();musics = dao.getMusicList();
Create different packages
Activity can be placed in Activity or UI package
DAL-> Data Access Layer
Entity-> Entity class
Adapter Improvement
Add BaseAdapter
Public abstract class BaseAdapter
Extends android. widget. BaseAdapter {private Context context; private List
Data; private LayoutInflater inflater; public BaseAdapter (Context context, List
Data) {this. context = context; this. data = data; inflater = LayoutInflater. from (this. context);}/*** get the LayoutInflate object * @ return */protected final LayoutInflater getLayoutInflater () {return inflater;} protected final List
GetData () {return data;} @ Override public int getCount () {return data. size () ;}@ Override public Object getItem (int I) {return null ;}@ Override public long getItemId (int I) {return 0 ;} @ Override public View getView (int I, View view, ViewGroup viewGroup) {return null ;}}
Modify MusicAdapter
public class MusicAdapter extends BaseAdapter
{ public MusicAdapter(Context context, List
data) { super(context, data); } @Override public View getView(int i, View view, ViewGroup viewGroup) { Music music = getData().get(i); if(view == null){ view = getLayoutInflater().inflate(R.layout.item_music,null); } TextView tvTitle = view.findViewById(R.id.tv_music_title); TextView tvPath = view.findViewById(R.id.tv_music_path); tvTitle.setText(music.getTitle()); tvPath.setText(music.getPath()); return super.getView(i, view, viewGroup); }}