从RecyclerView项目到其他活动的值

时间:2015-07-18 22:28:16

标签: android list android-recyclerview

我正在尝试将值从具有X位置的RecyclerView项目发送到显示这些值的其他活动,例如,在我的RecyclerView中我的X项目,具有图像,标题和副标题;我希望onClick能够显示在Image,Title和Subtitle等其他活动中。通过这种方式我的其他项目。 我认为应该用putExtra完成,但我无法得到它。

这是我的ViewHolder适配器:

"abcdefghijklmnopqrstuvwx"

以下是我的片段的活动,其中显示了RecyclerView:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.myViewHolder> {

private LayoutInflater inflater;
private List<Data> info = Collections.emptyList();
private Context context;

public MyAdapter (Context context, List<Data> info){
    this.context=context;
    inflater = LayoutInflater.from(context);
    this.info = info;

}


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

    View view = inflater.inflate(R.layout.recycler_item, parent, false);
    myViewHolder holder = new myViewHolder(view);
    return holder;
}

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

    Data current = info.get(position);
    holder.title.setText(current.title);;
    holder.subtitle.setText(current.subtitle);
    holder.image.setImageResource(current.imagenId);

}

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

class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    ImageView image;
    TextView title;
    TextView subtitle;

    public myViewHolder(View itemView) {
        super(itemView);

        image = (ImageView) itemView.findViewById(R.id.Image1);
        title = (TextView) itemView.findViewById(R.id.TextTitle);
        subtitle = (TextView) itemView.findViewById(R.id.TextSubTitle);
        itemView.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(context,ItemSelection.class);
        context.startActivity(intent);

    }
}
}

我的数据类:

public class ltfg0 extends Fragment {

private RecyclerView recyclerView;
private MyAdapter adapter;




public ltfg0() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_ltfg0, container, false);
    recyclerView = (RecyclerView) view.findViewById(R.id.ltfg0);
    adapter = new MyAdapter(getActivity(),getInfo());
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    return view;
}

public static List<Data> getInfo(){

    List<Data> info = new ArrayList<>();
    int[] srcs = {R.mipmap.img1, R.mipmap.img2, R.mipmap.img3, R.mipmap.img4};
    String[] titles = {"Title 1", "Title 2", "Title 3", "Title 4"};
    String[] subtitles = {"Description 1", "Description 2", "Description 3", "Description 4"};

    for (int i=0; i<srcs.length && i<titles.length && i<subtitles.length;i++){

        Data current = new Data();
        current.imagenId = srcs[i];
        current.title = titles[i];
        current.subtitle = subtitles[i];

        info.add(current);
    }

    return info;
}

}

1 个答案:

答案 0 :(得分:4)

您可以在ViewHolder

中执行此操作
  @Override
public void onClick(View view) {
    Intent intent = new Intent(context,ItemSelection.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable("DATA",info.get(getAdapterPosition()));
    intent.putExtras(bundle);
    context.startActivity(intent);

}

然后通过Activity#onCreate()

getIntent()中检索您的实例
  Data data = (Data)getIntent().getExtras().getSerializable("DATA");
  //if you have a TextView, for example...
  yourTextView.setText(data.getTitle());

当然,您的Data课程必须实施Serializable

   public class Data implements Serializale { ...