我正在构建一个 Android 应用程序,作为其中的一部分,我正在显示带有复选框的歌曲列表以允许选择。我正在使用自定义列表适配器来执行此操作。代码似乎可以加载并与复选框一起使用,但我遇到的唯一问题是只有列表视图中最顶部的项目显示文本,直到我向下滚动列表,然后其他项目呈现文本。
我知道这可能是列表视图不刷新或类似问题的问题,但我不知道如何解决它。
这是我的适配器的代码:
private class PlaylistDetailsAdapter extends ArrayAdapter<Song> {
boolean[] checkBoxState;
ViewHolder viewHolder;
public PlaylistDetailsAdapter(Context context, int textViewResourceId, ArrayList<Song> songs) {
super(context, textViewResourceId, songs);
checkBoxState = new boolean[songs.size()];
}
private class ViewHolder {
TextView songName;
CheckBox checkBox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.activity_playlist_details_row, null);
viewHolder = new ViewHolder();
viewHolder.songName = (TextView)convertView.findViewById(R.id.playlist_details_song_name);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.playlist_details_checkbox);
convertView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder)convertView.getTag();
Song song = playlistSongs.get(position);
viewHolder.songName.setText(song.getTitle() + " - " + song.getArtist());
viewHolder.checkBox.setChecked(checkBoxState[position]);
viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox)v).isChecked()) {
checkBoxState[position] = true;
}
else {
checkBoxState[position] = false;
}
}
});
}
return convertView;
}
}
这是设置列表视图的代码:
playlistSongs = db.getPlaylistTracks(getIntent().getLongExtra("id", 0));
TextView playlistName = (TextView)findViewById(R.id.playlist_details_name);
playlistName.setText(getIntent().getStringExtra("name"));
final PlaylistDetailsAdapter adapter = new PlaylistDetailsAdapter(this, R.layout.activity_playlist_details_row, playlistSongs);
ListView playlistSongList = (ListView)findViewById(R.id.listview_playlist_details);
playlistSongList.setAdapter(adapter);
任何帮助将不胜感激。
谢谢