Auf Thema antworten

Hallo!

Ich habe eine ListView, die alle Audiodateien anzeigen soll. Problem: es wird gar nichts angezeigt (Liste ist leer).

Wo liegt der Fehler?


MainActivity.java

[Code=Java]

package musicplayer.ludevstudio.com.musikplayer;


import android.Manifest;

import android.app.Activity;

import android.content.ContentResolver;

import android.content.pm.PackageManager;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.support.design.widget.FloatingActionButton;

import android.support.design.widget.Snackbar;

import android.support.v4.app.ActivityCompat;

import android.view.View;

import android.support.design.widget.NavigationView;

import android.support.v4.view.GravityCompat;

import android.support.v4.widget.DrawerLayout;

import android.support.v7.app.ActionBarDrawerToggle;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.widget.Toolbar;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.ListView;

import android.widget.Toast;


import java.util.ArrayList;


public class MainActivity extends AppCompatActivity

        implements NavigationView.OnNavigationItemSelectedListener {



    public ArrayList<Song> songs;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        setSupportActionBar(toolbar);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

        fab.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View view) {

                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)

                        .setAction("Action", null).show();

            }

        });


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(

                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawer.addDrawerListener(toggle);

        toggle.syncState();


        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(this);



        checkPermissions();


    }


    @Override

    public void onBackPressed() {

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        if (drawer.isDrawerOpen(GravityCompat.START)) {

            drawer.closeDrawer(GravityCompat.START);

        } else {

            super.onBackPressed();

        }

    }


    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }


    @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will

        // automatically handle clicks on the Home/Up button, so long

        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();


        //noinspection SimplifiableIfStatement

        if (id == R.id.action_settings) {

            return true;

        }


        return super.onOptionsItemSelected(item);

    }


    @SuppressWarnings("StatementWithEmptyBody")

    @Override

    public boolean onNavigationItemSelected(MenuItem item) {

        // Handle navigation view item clicks here.

        int id = item.getItemId();


        if (id == R.id.nav_camera) {

            // Handle the camera action

        } else if (id == R.id.nav_gallery) {


        } else if (id == R.id.nav_slideshow) {


        } else if (id == R.id.nav_manage) {


        } else if (id == R.id.nav_share) {


        } else if (id == R.id.nav_send) {


        }


        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawer.closeDrawer(GravityCompat.START);

        return true;

    }


    // Check for permissions if there are granted or not

    public boolean checkPermissions() {

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

            searchAndShowSongs();


        } else {

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


        }

    return false;


    }


    // React to Permissions Dialog

    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] ergebnis) {

        if (requestCode == 1) {

            if (ergebnis.length > 0

                    && ergebnis[0] == PackageManager.PERMISSION_GRANTED) {


            } else {


            }

            return;

        }

    }



    public void searchAndShowSongs() {

        songs = new ArrayList<Song>();


        ContentResolver musicResolver = getContentResolver();

        Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        Cursor songCursor = musicResolver.query(musicUri, null, null, null, null);


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

            int titlelColumn = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);


            do {

                String thisTitle = songCursor.getString(titlelColumn);

                songs.add(new Song(thisTitle));


            } while (songCursor.moveToNext());



            songs.add(new Song("Fail of Searching"));


        }


        SongAdapter liedAdapter = new SongAdapter(this, songs);

        ListView liedListView = (ListView)findViewById(R.id.SongListView);

        liedListView.setAdapter(liedAdapter);


        Toast.makeText(this, "Test", Toast.LENGTH_SHORT).show();


    }



}

[/Code]


SongAdapter.Java (Der ListAdapter)

[Code=Java]

package musicplayer.ludevstudio.com.musikplayer;


import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.RelativeLayout;

import android.widget.TextView;

import java.util.ArrayList;



public class SongAdapter extends BaseAdapter {


    private ArrayList<Song> songs;

    private LayoutInflater liedInf;


    public SongAdapter(Context c, ArrayList<Song> songs){

        this.songs = songs;

        liedInf = LayoutInflater.from(c);

    }


    @Override

    public int getCount() {

        return songs.size();

    }


    @Override

    public Object getItem(int arg0) {

        return null;

    }


    @Override

    public long getItemId(int arg0) {

        return arg0;

    }


    @Override

    public View getView(int arg0, View arg1, ViewGroup arg2) {


        RelativeLayout songLay = (RelativeLayout)liedInf.inflate(R.layout.songlistview_entrees, arg2, false);


        TextView songView = (TextView)songLay.findViewById(R.id.LiedTitel);


        Song momentanesLied = songs.get(arg0);


        songView.setText(momentanesLied.getTitel());


        songLay.setTag(arg0);


        return songLay;

    }

}

[/Code]


Song.java

[Code=Java]

package musicplayer.ludevstudio.com.musikplayer;


public class Song {

    private String titel =  "";



    public Song(String title) {

        this.titel = title;

    }




    public String getTitel() {

        return titel;

    }


    public void setTitel(String titel) {

        this.titel = titel;

    }




}

[/Code]


activity_main.xml

[Code=XML]

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/drawer_layout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:fitsSystemWindows="true"

    tools:openDrawer="start">


    <include

        layout="@layout/app_bar_main"

        android:layout_width="match_parent"

        android:layout_height="match_parent" />




    <android.support.design.widget.NavigationView

        android:id="@+id/nav_view"

        android:layout_width="wrap_content"

        android:layout_height="match_parent"

        android:layout_gravity="start"

        android:fitsSystemWindows="true"

        app:headerLayout="@layout/nav_header_main"

        app:menu="@menu/activity_main_drawer" />


</android.support.v4.widget.DrawerLayout>

[/Code]


content_main.xml

[Code=XML]

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout android:layout_height="match_parent"

    android:layout_width="match_parent"

    xmlns:android="http://schemas.android.com/apk/res/android">


    <ListView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:id="@+id/SongListView"

        android:layout_alignParentTop="true"

        android:layout_alignParentLeft="true"

        android:visibility="visible"

        android:layout_alignParentStart="true" />



</RelativeLayout>

[/Code]


app_bar_main.xml

[Code=XML]

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity">


    <android.support.design.widget.AppBarLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:theme="@style/AppTheme.AppBarOverlay">


        <android.support.v7.widget.Toolbar

            android:id="@+id/toolbar"

            android:layout_width="match_parent"

            android:layout_height="?attr/actionBarSize"

            android:background="?attr/colorPrimary"

            app:popupTheme="@style/AppTheme.PopupOverlay" />


    </android.support.design.widget.AppBarLayout>


    <include layout="@layout/content_main" />


    <android.support.design.widget.FloatingActionButton

        android:id="@+id/fab"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_gravity="bottom|end"

        android:layout_margin="@dimen/fab_margin"

        app:srcCompat="@android:drawable/ic_dialog_email" />


    <include

        layout="@layout/content_main"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_gravity="top"

        />



</android.support.design.widget.CoordinatorLayout>

[/Code]


songlistview_entrees.xml

[Code=xml]

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent" android:layout_height="match_parent">


    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textAppearance="?android:attr/textAppearanceMedium"

        android:text="Medium Text"

        android:id="@+id/LiedTitel"

        android:layout_alignParentTop="true"

        android:layout_alignParentLeft="true"

        android:layout_alignParentStart="true" />

</RelativeLayout>

[/Code]



Oben