(Android)项目在ExpandableListView中重复

时间:2017-08-28 08:18:43

标签: java android expandablelistview expandablelistadapter

我为一张专辑片段制作了一个ExpandableListView,当我点击它时,它会动态地调用同一专辑中的所有歌曲。当我关闭并重新打开专辑的声音时会出现问题: enter image description here

当我点击一次时,这是我的日志: enter image description here

如果我点击另一张专辑,它会继续......为什么?这是我的代码: 的 SongLoader.java

 public ArrayList<Song> loadSongsByAlbum(String albumSelected, String method){
    Log.d("ALBUM SELECTED: ", albumSelected+" BY METHOD: "+method);
    Song mSong;
    String selection = MediaStore.Audio.Media.ALBUM + " = '"+albumSelected+"' ";
    String orderBy = MediaStore.Audio.Media.TITLE + " ASC";
    Uri allSongsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] projection = new String[]{
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.TRACK,
            MediaStore.Audio.Media.ARTIST_ID,
            MediaStore.Audio.Media.ALBUM_ID
    };

    mCursor = mContext.getContentResolver().query(allSongsUri, projection, selection, null, orderBy);

    if(mCursor != null && mCursor.moveToFirst()){
        do{
            long id = mCursor.getLong(0);
            String title = mCursor.getString(1);
            String artist = mCursor.getString(2);
            String album = mCursor.getString(3);
            int duration = mCursor.getInt(4);
            int trackNumber = mCursor.getInt(5);
            long artistId = mCursor.getInt(6);
            long albumId = mCursor.getLong(7);

            mSong = new Song(id, albumId, artistId, title, artist, album, duration, trackNumber);
            mSongList.add(mSong);
        }while(mCursor.moveToNext());
        mCursor.close();
        mCursor = null;
    }
    return mSongList;
}

AlbumFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mContext = container.getContext();
    mInflater = inflater;
    mContainer = container;
    checkAlbumList();
    return mRootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        AlbumLoader sl = new AlbumLoader(getContext());
        mAlbumList = sl.loadAlbums();
}

public void checkAlbumList(){
    if((mAlbumList == null) || mAlbumList.isEmpty()){
        mRootView = mInflater.inflate(R.layout.error_no_tracks, mContainer, false);
    } else showAlbums();
}

public void showAlbums(){
    mRootView = mInflater.inflate(R.layout.fragment_album, mContainer, false);

    ExpandableListView mExpList = (ExpandableListView) mRootView.findViewById(R.id.listview_album);
    mExpList.setDivider(null);

    ExpandableAlbumListAdapter mExpAdapter = new ExpandableAlbumListAdapter(getContext(), mAlbumList);
    mExpList.setAdapter(mExpAdapter);
    mExpAdapter.notifyDataSetChanged();
}

}

ExpandableAlbumListAdapter.java

public class ExpandableAlbumListAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ArrayList<Song> mSongChildList;
private ArrayList<Album> mAlbumList;
private SongLoader songLoader;

public ExpandableAlbumListAdapter(Context cx, ArrayList<Album> albumData){
    this.mContext = cx;
    this.mAlbumList = albumData;
    songLoader = new SongLoader(mContext);
}


@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_album, parent, false);
    }

    Album albumView = mAlbumList.get(groupPosition);

    TextView mAlbumTitle = (TextView) convertView.findViewById(R.id.album_title);
    TextView mAuthorName = (TextView) convertView.findViewById(R.id.album_artist);
    TextView mTracksNumb = (TextView) convertView.findViewById(R.id.album_tracks);
    ImageView mAlbumCover = (ImageView) convertView.findViewById(R.id.album_cover);

    mAlbumTitle.setText(albumView.albumTitle);
    mAuthorName.setText(albumView.albumArtist);
    mTracksNumb.setText(""+albumView.albumNumTracks);
    ImageLoader.getInstance().displayImage(LyricaUtils.getAlbumArtUri(albumView.albumID).toString(), mAlbumCover);

    return convertView;
}

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_album_track, parent, false);
    }

    mSongChildList = songLoader.loadSongsByAlbum(mAlbumList.get(groupPosition).albumTitle, "getChildView");
    TextView mSongTitle = (TextView) convertView.findViewById(R.id.album_track_child);
    mSongTitle.setText(mSongChildList.get(childPosition).songTitle);
    return convertView;
}

@Override
public int getGroupCount() {
    return mAlbumList.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    Album albumPos = mAlbumList.get(groupPosition);
    mSongChildList = songLoader.loadSongsByAlbum(albumPos.albumTitle, "getChildrenCount");
    return this.mSongChildList.size();
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

@Override
public Object getGroup(int groupPosition) {
    notifyDataSetChanged();
    return this.mAlbumList.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    Album albumPos = mAlbumList.get(groupPosition);
    mSongChildList = songLoader.loadSongsByAlbum(albumPos.albumTitle, "getChild");
    Song songPos = mSongChildList.get(childPosition);
    notifyDataSetChanged();
    return songPos;
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

}

1 个答案:

答案 0 :(得分:0)

试试这个。

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.item_album_track, parent, false);
    }
    if (mSongChildList != null && mSongChildList.size() > 0) {
        mSongChildList.clear();
    }
    mSongChildList = songLoader.loadSongsByAlbum(mAlbumList.get(groupPosition).albumTitle, "getChildView");
    TextView mSongTitle = (TextView) convertView.findViewById(R.id.album_track_child);
    mSongTitle.setText(mSongChildList.get(childPosition).songTitle);
    return convertView;
}

我认为问题是你的mSongChildList永远不会被清除。但我建议你不要在getChildView上执行此操作,而是在适配器的构造函数中传递mSongChildList