Bojan Dolić
Newbie
Hi.
I m making simple music app that will list all music files from phone and play them.
I made activity that has recyclerview and it should list all songs.It works but not how i expected.
It lists all songs but problem is that song names are all the same.
For example: I have 3 songs and I download fourth song and now in app all song names are the same,same as song I downloaded last.
Here is code of MainActivity where I list all songs.
I m making simple music app that will list all music files from phone and play them.
I made activity that has recyclerview and it should list all songs.It works but not how i expected.
It lists all songs but problem is that song names are all the same.
For example: I have 3 songs and I download fourth song and now in app all song names are the same,same as song I downloaded last.
Here is code of MainActivity where I list all songs.
Java:
public class MainActivity extends AppCompatActivity {
RecyclerView listaPjesama;
ArrayList<Pjesma> pjesme;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(R.layout.action_bar);
getSupportActionBar().setElevation(0);
//View view = getSupportActionBar().getCustomView();
listaPjesama = findViewById(R.id.recycler_pjesme);
//pjesme = new ArrayList<Pjesma>();
//getSongs();
requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE },1);
listaPjesama.setLayoutManager(new LinearLayoutManager(this));
RecyclerSongAdapter adapter = new RecyclerSongAdapter(this,getSongs());
listaPjesama.setAdapter(adapter);
//rv.setAdapter(adapter);
}
ArrayList<Pjesma> getSongs() {
ArrayList<Pjesma> pjesme = new ArrayList<>();
Pjesma pjesma;
ContentResolver contentResolver = getContentResolver();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor c = contentResolver.query(songUri, null, null, null, null);
//grantUriPermission(getPackageName(),songUri);
if (c != null && c.moveToFirst()) {
int songId = c.getColumnIndex(MediaStore.Audio.Media._ID);
int songName = c.getColumnIndex(MediaStore.Audio.Media.TITLE);
do {
long trenutniId = c.getLong(songId);
String songTitle = c.getString(songName);
pjesma = new Pjesma(trenutniId,songTitle);
pjesme.add(pjesma);
}
while (c.moveToNext());
c.close();
}
return pjesme;
}
}