gpt4 book ai didi

java - 在 recyclerview 适配器中使用字符串数组数据之前,将其转换为 arraylist

转载 作者:行者123 更新时间:2023-12-02 10:53:21 26 4
gpt4 key购买 nike

我有一个 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() 中看到,我尝试通过将数据从字符串数组转换为数组列表来处理数据,以便在 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();
}
}

但在应用程序中数据没有显示

那么我应该如何处理数据,将其从字符串数组转换为数组列表,以准备在回收 View 中使用并将每个剧集显示为单个回收项目

像这样:

    s1episode1
s1episode2
s1episode3
s2episode1
s2episode2
s2episode3
s2episode4
s2episode5

最佳答案

为 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();
}
}

关于java - 在 recyclerview 适配器中使用字符串数组数据之前,将其转换为 arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51986860/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com