1. Are you ready for the Galaxy S20? Here is everything we know so far!

Problem with listing song names.

Discussion in 'Android Development' started by Bojan Dolić, Mar 1, 2019.

  1. Bojan Dolić

    Bojan Dolić Newbie
    Thread Starter

    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.



    Code (Java):
    1. public class MainActivity extends AppCompatActivity {
    2.  
    3.     RecyclerView listaPjesama;
    4.  
    5.     ArrayList<Pjesma> pjesme;
    6.  
    7.  
    8.     @Override
    9.     protected void onCreate(Bundle savedInstanceState) {
    10.         super.onCreate(savedInstanceState);
    11.         setContentView(R.layout.activity_main);
    12.  
    13.         getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    14.         getSupportActionBar().setDisplayShowCustomEnabled(true);
    15.         getSupportActionBar().setCustomView(R.layout.action_bar);
    16.         getSupportActionBar().setElevation(0);
    17.         //View view = getSupportActionBar().getCustomView();
    18.  
    19.  
    20.         listaPjesama = findViewById(R.id.recycler_pjesme);
    21.  
    22.         //pjesme = new ArrayList<Pjesma>();
    23.  
    24.         //getSongs();
    25.  
    26.         requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE },1);
    27.  
    28.         listaPjesama.setLayoutManager(new LinearLayoutManager(this));
    29.         RecyclerSongAdapter adapter = new RecyclerSongAdapter(this,getSongs());
    30.  
    31.         listaPjesama.setAdapter(adapter);
    32.         //rv.setAdapter(adapter);
    33.     }
    34.  
    35.  
    36.     ArrayList<Pjesma> getSongs() {
    37.  
    38.         ArrayList<Pjesma> pjesme = new ArrayList<>();
    39.         Pjesma pjesma;
    40.  
    41.         ContentResolver contentResolver = getContentResolver();
    42.         Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    43.         Cursor c = contentResolver.query(songUri, null, null, null, null);
    44.  
    45.         //grantUriPermission(getPackageName(),songUri);
    46.  
    47.         if (c != null && c.moveToFirst()) {
    48.  
    49.             int songId = c.getColumnIndex(MediaStore.Audio.Media._ID);
    50.             int songName = c.getColumnIndex(MediaStore.Audio.Media.TITLE);
    51.  
    52.             do {
    53.                 long trenutniId = c.getLong(songId);
    54.                 String songTitle = c.getString(songName);
    55.                 pjesma = new Pjesma(trenutniId,songTitle);
    56.                 pjesme.add(pjesma);
    57.             }
    58.             while (c.moveToNext());
    59.             c.close();
    60.         }
    61.  
    62.  
    63.         return pjesme;
    64.     }
    65.  
    66. }
     


Loading...

Share This Page

Loading...