[英](Android) items duplicated in ExpandableListView
I made an ExpandableListView
for an Album fragment, which dinamically calls all the songs of the same album when I click on it. 我为专辑片段制作了一个
ExpandableListView
,单击该片段时会动态调用同一专辑的所有歌曲。 The problem comes when I close and reopen the album's voice: 当我关闭并重新打开专辑的声音时,问题就来了:
And here's my Log when I click once: 当我单击一次时,这是我的日志:
And it continues if I click on the other album...why? 如果我单击另一张专辑,它会继续...为什么? Here's my code: SongLoader.java
这是我的代码: 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 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 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;
}
} }
Try this. 尝试这个。
@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;
}
I think the issue is your mSongChildList
never getting cleared. 我认为问题是您的
mSongChildList
从未被清除。 But I will suggest you to avoid doing this on getChildView
instead pass the mSongChildList
in the constructor of your adapter. 但我建议您避免在
getChildView
上执行此操作,而mSongChildList
在适配器的构造函数中传递mSongChildList
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.