如何通过recycleview适配器显示字符串数组内容

时间:2018-06-10 22:57:24

标签: java android

我有json api的这种结构:

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;
    }
}

我尝试显示这两个键的值:seasonstitle和titles,但是当你看到titles键有多个值时,我就像这样解析json:

ParsingClass:

titles = S1E1
titles = S1E2
titles = S1E3

当我在log cat中看到它时,它会像这样正确分割:

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;
    }

}

依此类推,在我的自定义arraylist类中,我为它返回数据:

Season1
S1E1
S1E2
S1E3

Season2
S2E1
S2E2
S2E3

当我调试这个类时,titlesArrray的数据很好地接收了每个标题与另一个标题分开 所以我试图像这样在recyleview中显示这些数据:

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

所以这是我对recycleview的适配器:

    let keyframeAnimation = CAKeyframeAnimation(keyPath: "hidden")
keyframeAnimation.calculationMode = kCAAnimationDiscrete
keyframeAnimation.repeatCount = 1.0
keyframeAnimation.values = [true, false,true,false,true]
keyframeAnimation.keyTimes = [0.0, 0.25,0.5,0.75, 1.0]
keyframeAnimation.duration = 30.0 //duration of the video in my case
keyframeAnimation.beginTime = 0.1
keyframeAnimation.isRemovedOnCompletion = false
keyframeAnimation.fillMode = kCAFillModeBoth

textLayer.add(keyframeAnimation, forKey:  "hidden")

我试图包含一个listview来显示此循环视图中的titlesArray,问题在于这一行:

  

holder.titlesArray.setText(将String.valueOf(mMoviesItems.get(位置).gettitlesArrray()));

我无法在ListView中使用setText,那么如何在此循环视图中显示titlesArray内容?

2 个答案:

答案 0 :(得分:1)

  

我尝试包含listview以在此循环视图中显示titlesArray

不要那样做。

你想要做的是处理两种不同的类型,即季节和剧集。这个问题可以帮助你。

How to create RecyclerView with multiple view type?

答案 1 :(得分:0)

使用Gson来解析Json而不是本机,它更容易实现。