1

I have this structure of json api:

{
seasons: [
{
seasonstitle: "Season 1",
titles: "S1E1; S1E2; S1E3",
},
{
seasonstitle: "Season 2",
titles: "S2E1; S2E2; S2E3",
},
]
}

and I'm trying to display the values of these two keys: seasonstitle and titles but as you see the titles key has multiple values so I parsing the json like this:

ParsingClass:

public final class JsonDetailSeries {

    public static ArrayList<SeriesItem> getSimpleMovieStringsFromJson(Context context, String moviesJsonString)
            throws JSONException {

        final String SEASONS = "seasons";
        final String SEASONTITLE = "seasonstitle";
        final String TITLES = "titles";

        ArrayList<SeriesItem> parsedMovieData = new ArrayList<>();

        JSONObject moviesObject = new JSONObject(moviesJsonString);
        JSONArray moviesArray = moviesObject.getJSONArray(SEASONS);

        for (int i = 0; i < moviesArray.length(); i++) {
            String seasontitle;
            String titles;

            moviesObject = moviesArray.getJSONObject(i);

            seasontitle = moviesObject.getString(SEASONTITLE);
            titles = moviesObject.getString(TITLES);

            String[] titlesArrray = titles.split(Pattern.quote(";"));

            for (int j=0; j<titlesArrray.length; j++) {
                Log.i("titles ", "=" + titlesArrray[j]);
            }

            parsedMovieData.add(new SeriesItem(seasontitle, titlesArrray));
        }

        return parsedMovieData;
    }
}

when I saw it in log cat it splits correctly like this:

titles = S1E1
titles = S1E2
titles = S1E3

and so on, in my custom arraylist class which I return the data for it:

public class SeriesItem implements Parcelable {

    private String seasontitle;
    private String[] titlesArrray;

    public SeriesItem(String seasontitle, String[] titlesArrray) {
        this.seasontitle = seasontitle;
        this.titlesArrray = titlesArrray;
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        out.writeString(seasontitle);
        out.writeStringArray(titlesArrray);
    }

    private SeriesItem(Parcel in) {
        this.seasontitle         = in.readString();
        this.titlesArrray         = in.createStringArray();
    }

    public SeriesItem() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public static final Creator<SeriesItem> CREATOR = new Creator<SeriesItem>() {
        @Override
        public SeriesItem createFromParcel(Parcel in) {
            return new SeriesItem(in);
        }
        @Override
        public SeriesItem[] newArray(int i) {
            return new SeriesItem[i];
        }
    };

    public String getSeasontitle() {
        return seasontitle;
    }

    public String[] gettitlesArrray() {
        return titlesArrray;
    }

}

when I debug this class the data of titlesArrray recevied well each title split from the other one so I'm trying to display this data in recyleview like this way:

Season1
S1E1
S1E2
S1E3

Season2
S2E1
S2E2
S2E3

so this is my adapter of recycleview:

public class SeriesAdapter extends RecyclerView.Adapter<SeriesAdapter.RecyclerViewHolder> {

        ArrayList<SeriesItem> mMoviesItems;
        private Context context;
        private final SeriesAdapterOnClickHandler mClickHandler;

public interface SeriesAdapterOnClickHandler {
    void onClick(SeriesItem movie);
}

    public SeriesAdapter(SeriesAdapterOnClickHandler clickHandler) {
        mClickHandler = clickHandler;
    }

class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public final TextView seasontitle;
    public final ListView titlesArray;

    public RecyclerViewHolder(View view) {
        super(view);
        seasontitle = (TextView)itemView.findViewById(R.id.seasontitle);
        titlesArray = (ListView) itemView.findViewById(R.id.titlesArray);
        view.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int adapterPosition = getAdapterPosition();
        SeriesItem movie = mMoviesItems.get(adapterPosition);
        mClickHandler.onClick(movie);
    }
}

    @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);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.seasontitle.setText(String.valueOf(mMoviesItems.get(position).getSeasontitle()));
        holder.titlesArray.setText(String.valueOf(mMoviesItems.get(position).gettitlesArrray()));
    }

    @Override
    public int getItemCount() {
        if (null == mMoviesItems)
            return 0;
        else {
            return mMoviesItems.size();
        }
    }

    public void setMovieData(ArrayList<SeriesItem> movieData) {
        mMoviesItems = movieData;
        notifyDataSetChanged();
    }
}

I tried to include a listview to display the titlesArray inside this recycleview and the problem is with this line:

holder.titlesArray.setText(String.valueOf(mMoviesItems.get(position).gettitlesArrray()));

I can't use setText for ListView so how can to display the titlesArray content inside this recycleview?

muklah
  • 855
  • 2
  • 11
  • 19

2 Answers2

1

I tried to include a listview to display the titlesArray inside this recycleview

Do not do that.

What you want to do is handle two different types, the season and the episode. This question that will help you with that.

How to create RecyclerView with multiple view type?

dominicoder
  • 9,338
  • 1
  • 26
  • 32
0

Use Gson to parse Json instead of the native, it is much easier to implement.

Murtadha S.
  • 442
  • 6
  • 15