Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

android - Values from RecyclerView item to other Activity

I'm trying to send values from a RecyclerView Item with X position, to other activity which displays those values, for example, in my RecyclerView my X item, has an Image, a Title and a Subtitle; and I want onClick to be displayed in other activity that Image, Title and Subtitle. By that way for the rest of my items. I think it should be done with putExtra but I can′t get it.

Here is my Adapter with ViewHolder:

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

    }
}
}

Here is the activity of my fragment where the RecyclerView is shown:

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

}

And my Data class:

public class Data {

public int imagenId;
public String title;
public String subtitle;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it like this inside your 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);

}

Then retrieve your instance in your Activity#onCreate() via getIntent():

  Data data = (Data)getIntent().getExtras().getSerializable("DATA");
  //if you have a TextView, for example...
  yourTextView.setText(data.getTitle());

Off course, your Data class must implement Serializable:

   public class Data implements Serializale { ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...