将数据对象从片段发送到活动

时间:2016-04-19 08:28:48

标签: android android-intent fragment

我有片段(电影)来显示json数据。现在我想将数据(对象)发送到活动(此类make show full inofrmation)。但我不知道在方法onClick上获取和发送数据。谁能告诉我一些建议?多谢 。 这是我的代码。

public class FragmentFilmAdapter extends RecyclerView.Adapter<FragmentFilmAdapter.ViewHolder> {

    private List<InforFilm> inforFilms;
    ImageLoader imageLoader = AppController.getInstance().getmImageLoader();
    //contructor
    public FragmentFilmAdapter(List<InforFilm> inforFilms) {
        this.inforFilms = inforFilms;
    }
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        NetworkImageView thumbNail;
        TextView gender;
        TextView rating;
        TextView title;
        TextView year;

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

            thumbNail = (NetworkImageView) view.findViewById(R.id.film_thumbnail);
            gender = (TextView) view.findViewById(R.id.film_genre);
            title = (TextView) view.findViewById(R.id.film_title);
            rating = (TextView) view.findViewById(R.id.film_rating);
            year = (TextView) view.findViewById(R.id.film_releaseYear);

        }
        @Override
        public void onClick(View v) {
        }
    }

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

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_film_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        InforFilm infor = inforFilms.get(position);

        holder.thumbNail.setImageUrl(infor.getThumbail(), imageLoader);
        holder.title.setText(infor.getTitle());
        holder.rating.setText(String.valueOf(infor.getRating()));
        holder.year.setText(String.valueOf(infor.getYear()));
        String genreStr = "";
        for (String str : infor.getGender()) {
            genreStr += str + ", ";
        }
        genreStr = genreStr.length() > 0 ? genreStr.substring(0,
                genreStr.length() - 2) : genreStr;
        holder.gender.setText(genreStr);
    }

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

1 个答案:

答案 0 :(得分:0)

RecyclerView没有自己的onclick监听器,因此创建一个监听器接口并获取适配器位置。 请参考此链接,它可能会对您有所帮助 Getting recyclerview position and passing object on click