未使用AsyncTask加载ListView项

时间:2016-07-04 06:13:55

标签: android listview android-asynctask

我正在尝试使用AsyncTask从ListView中加载SD卡中的歌曲。当我运行应用程序时,ListView中没有添加任何内容。 ListView为空。当我没有实现AsyncTask方法时,应用程序正在工作。由于从SD卡获取歌曲,应用程序会冻结2或3秒。所以有人建议在你的代码中实现AsyncTask方法。当我添加方法时,ListView显示为空。我不知道我的代码在哪里犯了错误。请更正代码。感谢您的建议和帮助

SongsManager类

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类

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片段类

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");
 }
 }

1 个答案:

答案 0 :(得分:0)

ListAdapter adapter = new SimpleAdapter(getContext(), songsListData,
        R.layout.playlist_item, new String[]{"songTitle"}, new int[]{
        R.id.songTitle});
setListAdapter(adapter);
Log.i(TAG, "onSongsReceivedMessage");

只需将songsListData替换为歌曲,您就可以了:)