Click here to Skip to main content
15,313,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to display a list of songs from storage on my TracksActivity tab from my TabHost. I followed a tutorial on how to display songs from storage to a ListView but after running it doesnt show anything on the ListView and there are no errors on the Log. Pardon me if it's a silly mistake I'm still a noob.

What I have tried:

TracksActivity:

public class TracksActivity extends Activity {

private static final int MY_PERMISSION_REQUEST = 1;

ArrayList<String> arrayList;

ListView trackList;

ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tracks_layout);

    if (ContextCompat.checkSelfPermission(TracksActivity.this,
            Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale(TracksActivity.this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) {

            ActivityCompat.requestPermissions(TracksActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);

        } else {

            ActivityCompat.requestPermissions(TracksActivity.this,
                    new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSION_REQUEST);
            }

        } else {

            doStuff();

    }

}

public void doStuff() {

    trackList = findViewById(R.id.trackList);
    arrayList = new ArrayList<>();
    getMusic();
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
    trackList.setAdapter(adapter);

    trackList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long I) {

        }
    });

}

public void getMusic() {

    ContentResolver contentResolver = getContentResolver();
    Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor songCursor = contentResolver.query(songUri, null,null,null,null);


    if (songCursor != null && songCursor.moveToFirst()) {

        int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
        int songArtist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
        int songLocation = songCursor.getColumnIndex(MediaStore.Audio.Media.DATA);

        do {

            String currentTitle = songCursor.getString(songTitle);

            String currentArtist = songCursor.getString(songArtist);

            String currentLocation = songCursor.getString(songLocation);

            arrayList.add("Title: " + currentTitle + "\n" + "Artist: " + currentArtist + "\n" + "Location: " + currentLocation);

        } while (songCursor.moveToNext());

    }

}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

    switch (requestCode) {

        case MY_PERMISSION_REQUEST: {

            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                if (ContextCompat.checkSelfPermission(TracksActivity.this,
                        Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();

                    doStuff();

                }

            } else {

                Toast.makeText(this,"No permission granted", Toast.LENGTH_SHORT).show();

                finish();

            }

            return;

        }

    }

}}


tracks_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView
    android:id="@+id/trackList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true" />
Posted
Comments
David Crow 6-Dec-18 10:56am
   
How many items are contained in arrayList? What about adapter? Is the doStuff() method getting called? I see no try/catch blocks in your code. Is that intentional?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900