Ich versuche, Songs von SD-Karte in ListView mit AsyncTask zu laden. Wenn ich die App starte, wird in ListView nichts hinzugefügt. ListView ist leer. App funktionierte, wenn ich die AsyncTask-Methode nicht implementiert habe. App friert für 2 oder 3 Sekunden ein, weil Songs von der SD-Karte abgerufen werden. Also schlug jemand vor, die AsyncTask-Methode in Ihrem Code zu implementieren. Wenn ich die Methode hinzufüge, zeigt ListView leer an. Ich weiß nicht, wo ich Fehler in meinem Code mache. Bitte korrigieren Sie den Code. Danke für Anregungen & Hilfe.ListView-Elemente, die nicht mit AsyncTask geladen wurden
SongsManager Klasse
public class SongsManager {
private static final String TAG = "songsManagerMessage";
final String MEDIA_PATH = Environment.getExternalStorageDirectory() .getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
// Constructor
public SongsManager() {
}
/** * Function to read all mp3 files and store the details in * ArrayList * */
public ArrayList<HashMap<String, String>> getPlayList() {
Log.i(TAG,"getPlaylist");
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
return songsList;
}
private void scanDirectory(final File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
Log.i(TAG,"scanDirectory");
}
private void addSongToList(File song) {
Log.i(TAG,"scanDirectory");
if (song.getName().endsWith(mp3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle", song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
}
GetSongsAsyncTask Klasse
public class GetSongsAsyncTask extends AsyncTask<Void,Void, List<HashMap<String, String>>> {
private static final String TAG = "AsyncTaskMessgae";
private final SongsManager mSongsManager;
public interface SongsReceivedListener {
void onSongsReceived(List<HashMap<String, String>> songs);
}
private SongsReceivedListener mSongsReceivedListener;
public GetSongsAsyncTask(SongsReceivedListener mSongsReceivedListener) {
this.mSongsReceivedListener = mSongsReceivedListener;
mSongsManager = new SongsManager();
Log.i(TAG,"GetSongsAsyncTaskMessage");
}
@Override
protected List<HashMap<String, String>> doInBackground(Void... params) {
Log.i(TAG,"doInBackgroundMessage");
return mSongsManager.getPlayList();
}
@Override
protected void onPostExecute(List<HashMap<String, String>> v) {
Log.i(TAG,"onPostExecuteMessage");
if(mSongsReceivedListener != null) {
mSongsReceivedListener.onSongsReceived(v);
}
}
}
Tab1 Fragment Klasse
public class Tab1 extends ListFragment implements GetSongsAsyncTask.SongsReceivedListener {
private static final String TAG = "tab1Message";
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.i(TAG, "onCreateView");
View V = inflater.inflate(R.layout.tab1, container, false);
new GetSongsAsyncTask(this).execute();
return V;
}
@Override
public void onSongsReceived(List<HashMap<String, String>> songs) {
if(getContext() == null)
return;
// looping through playlist
for (int i = 0; i < songsList.size(); i++) {
// creating new HashMap
HashMap<String, String> song = songs.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(getContext(), songsListData,
R.layout.playlist_item, new String[]{"songTitle"}, new int[]{
R.id.songTitle});
setListAdapter(adapter);
Log.i(TAG, "onSongsReceivedMessage");
}
}
Sie nehmen die Berechtigungen für externen Speicher richtig und Ihr Song-Manager gibt die Liederliste richtig zurück? – Ramesh
Ja .. Ich habe Berechtigungen in der Manifest-Datei hinzugefügt. App funktioniert, wenn ich AsyncTask nicht in Code implementiere. –
erhalten Sie Werte in 'onSongsReceived' Versuchen Sie, Größe der' songs' Liste zu drucken –