无法在RecyclerView中显示歌曲列表

时间:2016-02-03 17:54:30

标签: java android eclipse android-recyclerview

我使用的是recyclerView来显示歌曲列表,但我无法这样做。请参考代码,并告诉我在哪里弄错了。三江源。

SongsFragment: -

  public class Fragment_Songs extends android.support.v4.app.Fragment {
/**
 * Big list with all the Songs found.
 */
public ArrayList<Song> songs = new ArrayList<Song>();

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;

public static Fragment_Songs newInstance() {
    return new Fragment_Songs();
}


/**
 * Returns a new list with all songs.
 *
 * @note This is different than accessing `songs` directly
 *       because it duplicates it - you can then mess with
 *       it without worrying about changing the original.
 */
public ArrayList<Song> getSongs() {
    ArrayList<Song> songList = new ArrayList<Song>();

    for (Song song : songs)
        songList.add(song);

    return songList;
}

public Fragment_Songs() {
    // Required empty public constructor
}





@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_songs, container, false);


}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mRecyclerView = (RecyclerView) view.findViewById(R.id.songs_recyclerView);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(layoutManager);



    mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter(getActivity(), getSongs()));
    mRecyclerView.setAdapter(mAdapter);


    MaterialViewPagerHelper.registerRecyclerView(getActivity(), mRecyclerView, null);
}
}

这是我的recyclerView适配器: -

      public class Songs_Adapter extends   RecyclerView.Adapter<Songs_Adapter.ViewHolder> {
private ArrayList<Song> songs = new ArrayList<Song>();


public Songs_Adapter(Context c, ArrayList<Song> theSongs) {
    songs = theSongs;
}



@Override
public int getItemCount() {
    return songs.size();
}




@Override
public Songs_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item_card, parent, false);
    ViewHolder holder = new ViewHolder(view);
    return holder;

}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    Song currentSong = songs.get(position);
    holder.trackName.setText(currentSong.getArtist());
    holder.albumInfo.setText(currentSong.getAlbum());
    holder.yearInfo.setText(currentSong.getYear());
    holder.songDuration.setText((int) currentSong.getDurationMinutes());
}


public class ViewHolder extends RecyclerView.ViewHolder  {

     CardView cv;
     ImageView albumArt;
    TextView trackName;
    TextView albumInfo;
    TextView yearInfo;
    TextView songDuration;



    public ViewHolder(View itemView) {
        super(itemView);

        cv = (CardView)itemView.findViewById(R.id.list_item_card);
        albumArt = (ImageView)itemView.findViewById(R.id.albumArtImage);
        trackName = (TextView)itemView.findViewById(R.id.trackName);
        albumInfo = (TextView)itemView.findViewById(R.id.albumData);
        yearInfo = (TextView)itemView.findViewById(R.id.yearInfo);
        songDuration = (TextView)itemView.findViewById(R.id.songDuration);


    }



}

public ArrayList<Song> getSongs() {
    return songs;
}

}

我的arrayList代码是否正确?如果有人帮忙,我无法让它工作? 。谢谢。

2 个答案:

答案 0 :(得分:0)

乍一看,您的适配器中没有使用正确的构造函数。 您正在使用

mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter());

您应该使用

mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter(getActivity(), songs));

答案 1 :(得分:0)

您正在创建歌曲ArrayList但从未将其传递给您的适配器。那你的适配器将如何获取数据?做这样的事情 -

mAdapter = new RecyclerViewMaterialAdapter(new Songs_Adapter(getActivity(), songs));
mRecyclerView.setAdapter(mAdapter);

这样您的歌曲ArrayList将传递给适配器,并可用于填充RecyclerView。