我有一个api,其结构如下:
{"seasons":[
{
"id":"1",
"titles":"s1episode1;s1episode2;s1episode3"
},
{
"id":"2",
"titles":"s2episode1;s2episode2;s2episode3;s2episode4;s2episode5"
}
]}
以此类推
并且我想要像这样在recycleview应用程序中显示数据:
s1episode1
s1episode2
s1episode3
s2episode1
s2episode2
s2episode3
s2episode4
s2episode5
所以每行代表一个recycleview项目
我解析数据,并在模型类中使用parcelable:
public class Series implements Parcelable {
private int id;
private String titles;
public Series(int id, String titles) {
this.id = id;
this.titles = titles;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(titles);
}
private Series(Parcel in) {
this.id = in.readInt();
this.titles = in.readString();
}
public Series() {
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<Series> CREATOR = new Creator<Series>() {
@Override
public Series createFromParcel(Parcel in) {
return new Series(in);
}
@Override
public Series[] newArray(int i) {
return new Series[i];
}
};
public int getId() {
return id;
}
public String getTitles() {
return titles;
}
public List<String> getEpisodes(){
String[] titles = getTitles().split(";");
List<String> titleList = Arrays.asList(titles);
String titlesArray = null;
for (String e : titleList) {
titlesArray += String.valueOf(e);
}
return titleList;
}
}
您可以在getEpisodes()中看到,我试图通过将数据从字符串数组转换为arraylist来处理数据,以便在将其用于recycleview之前对其进行处理
然后为RecyclerViewAdapter创建一个新集合:
class Episode {
int id;
String title;
public Episode(int id, String title) {
this.id = id;
this.title = title;
}
public static List<Episode> createEpisodeList(int id, List<String> titles) {
List<Episode> episodes = new ArrayList(titles.size());
for (String title : titles) {
episodes.add(new Episode(id, title));
}
return episodes;
}
}
这是recycleview适配器的代码:
public class SeriesAdapter extends RecyclerView.Adapter<SeriesAdapter.RecyclerViewHolder> {
ArrayList<Series> mSeries;
List<Episode> episodesList = new ArrayList();
private Context context;
private SeriesAdapterOnClickHandler mClickHandler;
public interface SeriesAdapterOnClickHandler {
void onClick(Series series);
}
public SeriesAdapter(SeriesAdapterOnClickHandler clickHandler) {
mClickHandler = clickHandler;
}
public void setClickListener(SeriesAdapterOnClickHandler callback) {
mClickHandler = callback;
}
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView title;
public RecyclerViewHolder(View view) {
super(view);
title = (TextView)itemView.findViewById(R.id.title);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
Series series = mSeries.get(adapterPosition);
mClickHandler.onClick(series);
}
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
context = viewGroup.getContext();
int layoutIdForListItem = R.layout.series_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
RecyclerViewHolder holder = new RecyclerViewHolder(view);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.title.setText(String.valueOf(mSeries.get(position).getEpisodes()));
}
@Override
public int getItemCount() {
if (null == mSeries)
return 0;
else {
return mSeries.size();
}
}
@Override
public int getItemViewType(int position) {
if (null == mSeries)
return 0;
else {
return mSeries.size();
}
}
public void setSeriesData(ArrayList<Series> seriesData) {
for (Series s : seriesData) {
episodesList.addAll(Episode.createEpisodeList(s.getEpisodes()));
}
// mSeries = seriesData;
notifyDataSetChanged();
}
}
但是在应用程序中,数据什么也没显示
那么我该如何处理数据,将其从字符串数组转换为arraylist,以准备将其用于recycleview并将每个情节显示为单个回收项
像这样:
s1episode1
s1episode2
s1episode3
s2episode1
s2episode2
s2episode3
s2episode4
s2episode5
答案 0 :(得分:1)
为RecyclerViewAdapter创建新集合:
class Episode {
int id;
String title;
public Episode(int id, String title) {
this.id = id;
this.title = title;
}
public static List<Episode> createEpisodeList(int id, List<String> titles) {
List<Episode> episodes = new ArrayList(titles.size());
for (String title : titles) {
episodes.add(new Episode(id, title));
}
return episodes;
}
}
并在适配器中将系列列表转换为新集合:
List<Episode> episodesList = new ArrayList();
void setSeries(List<Series> series) {
for (Series s : series) {
episodesList.addAll(Episode.createEpisodeList(s.id, s.getEpisododes()));
}
notifyDataSetChanged();
}
已编辑的适配器代码:
public class EpisodeAdapter extends RecyclerView.Adapter<EpisodeAdapter.RecyclerViewHolder> {
private static final int TYPE_EPISODE = 0; // it's possible to show multiple types recycler
List<Episode> episodesList = new ArrayList();
private Context context;
private AdapterOnClickHandler mClickHandler;
public interface AdapterOnClickHandler {
void onClick(Episode episode);
}
public EpisodeAdapter(AdapterOnClickHandler clickHandler) {
mClickHandler = clickHandler;
}
public void setClickListener(AdapterOnClickHandler callback) {
mClickHandler = callback;
}
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView title;
public RecyclerViewHolder(View view) {
super(view);
title = (TextView)itemView.findViewById(R.id.title);
view.setOnClickListener(this);
}
public void setData(Episode episode) {
title.setText(episode.title);
}
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
Episode episode = episodesList.get(adapterPosition);
mClickHandler.onClick(episode);
}
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
context = viewGroup.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.series_list_item, viewGroup, false);
return new RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
holder.setData(episodesList.get(position));
}
@Override
public int getItemCount() {
return episodesList.size();
}
@Override
public int getItemViewType(int position) {
return TYPE_EPISODE;
}
public void setSeriesData(ArrayList<Series> seriesData) {
episodesList.clear();
for (Series s : seriesData) {
episodesList.addAll(Episode.createEpisodeList(s.getEpisodes()));
}
notifyDataSetChanged();
}
}
答案 1 :(得分:0)
String.valueOf(list)
(来自holder.title.setText(String.valueOf(mSeries.get(position).getEpisodes()))
)肯定会返回这样的结果。您基本上只是在呼叫arrayListInstance.toString()
。您应该重写getItemCount()
方法来返回系列所有剧集列表大小的总和(根据您的偏好),并且每个绑定都应从列表中返回一个值,而不要调用list.toString()
( String.valueOf()
内部正在发生什么。我建议在recyclerview的实现的构造函数中传递所有系列剧集的合并列表。
答案 2 :(得分:0)
我猜想,简单的方法是在getEpisodes方法中串联一个换行器。这样的事情(我敢肯定还有其他可能更好的方法):
public List<String> getEpisodes(){
String[] titles = getTitles().split(";");
List<String> titleList = Arrays.asList(titles);
String titlesArray = null;
for (String e : titleList) {
titlesArray += String.valueOf(e) + "\n";
}
return titleList;
}