【问题标题】:Values from RecyclerView item to other Activity从 RecyclerView 项到其他 Activity 的值
【发布时间】:2015-10-08 09:01:51
【问题描述】:

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

这是我的带有 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);

    }
}
}

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

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

}

还有我的数据类:

public class Data {

public int imagenId;
public String title;
public String subtitle;
}

【问题讨论】:

    标签: android list android-recyclerview


    【解决方案1】:

    你可以在你的 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);
    
    }
    

    然后通过getIntent()Activity#onCreate() 中检索您的实例:

      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 { ...
    

    【讨论】:

    • 感谢您的回答,对不起,我是新手,我应该如何替换新活动中的值?我的意思是,将它们设置在布局中。因为我尝试创建一个新方法,并在其中找到布局中的文本,然后将其替换为 (data.title) 但我的应用程序崩溃
    • @JavierGuerrero 更新了答案。在setContentView() 之后,在ActivityonCreate() 中执行操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 2018-12-03
    • 1970-01-01
    相关资源
    最近更新 更多