简体   繁体   English

如何为 Intent 创建公共函数

[英]How can I create public function for Intent

I have a RecyclerView that I give OnItemClickListener then open a new activity我有一个 RecyclerView 给 OnItemClickListener 然后打开一个新活动

private TextView Judul, Sub;
        private ImageView Gambar;
        private RelativeLayout ListItem;
        private Context context;
private String[] komposisi;
    private String[] kemasan;
    private String[] caraPakai;

        ViewHolder(@NonNull final View itemView) {
            super(itemView);

            Resources res = itemView.getResources();
            Judul = itemView.findViewById(R.id.memetitle);
            Sub = itemView.findViewById(R.id.sub_meme);
            Gambar = itemView.findViewById(R.id.meme);
            komposisi = res.getStringArray(R.array.ikomposisi);
            kemasan = res.getStringArray(R.array.ikemasan);
            caraPakai = res.getStringArray(R.array.iCara_Pakai);
            context = itemView.getContext();
            ListItem = itemView.findViewById(R.id.item_list);
ListItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    int i = getAdapterPosition();
                    int a = R.drawable.comingsoon;

                        Intent intent = new Intent(context, Detail.class);
                        intent.putExtra("komposisi", komposisi[i]);
                        intent.putExtra("kemasan", kemasan[i]);
                        intent.putExtra("pakai", caraPakai[i]);

                        Bundle bundle = new Bundle();
                        bundle.putInt("gambar", memeList.get(i));
                        intent.putExtras(bundle);

                        context.startActivity(intent);
                    }
                }

and in other activitiy it also requires an Intent with some intent.putExtra, and I think I should make a public function for the Intent which will only call detail()在其他活动中,它还需要一个带有一些intent.putExtra的Intent,我想我应该为Intent创建一个公共函数,它只会调用detail()

ListItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        detail()
                     }
}

the question is how to make the function detail() Sorry if my question is confusing, and hopefully you understand my question, thank you very much问题是如何使函数detail() 对不起,如果我的问题令人困惑,希望您理解我的问题,非常感谢

you can just add in your activity你可以添加你的活动

 public void detail(){
 //some codes here

        int i = getAdapterPosition();
        int a = R.drawable.comingsoon;

        Intent intent = new Intent(context, Detail.class);
        intent.putExtra("komposisi", komposisi[i]);
        intent.putExtra("kemasan", kemasan[i]);
        intent.putExtra("pakai", caraPakai[i]);
        Bundle bundle = new Bundle();
        bundle.putInt("gambar", memeList.get(i));
        intent.putExtras(bundle);
        context.startActivity(intent);

 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM