如何将值从 onItemClick RecycleView 发送到其他 java class

How to send value from onItemClick RecycleView to other java class

 @Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    String clicktemplate = modelClassesListt.get(i).getTitle();
    Toast.makeText(Template.this ,modelClassesListt.get(i).getTitle() , Toast.LENGTH_SHORT).show();
}

我想将此 clicktemplate 字符串值发送给其他 class

You have to put value in Intent or Bundle and send it to the next screen.

这里是 Activity.

中传递数据的示例
Intent intent = new Intent(this, YourNextActivity.class);
                intent.putExtra("key_clicktemplate", clicktemplate);
                startActivity(intent);

To get value in next screen like below

String value = getIntent().getExtras().getString("key_clicktemplate");

您可以使用多种方法来实现您的 need.below 线程将对您有用。

在你的 recyclerview class 添加 onclicklistener 接口并在构造函数中包含你的侦听器:

private final OnItemClickListener listener;

public interface OnItemClickListener {
        void onItemClick(myObject item, int position, String message);
    }

public MyRecyclerViewAdapter(Activity pActivity, ArrayList<myObject> pList, int pItemLayout, OnItemClickListener listener) {
        activity = pActivity;
        rewardsList = pList;
        layoutID=pItemLayout;
        this.listener = listener;
    }

然后。在 onCreateViewHolder 中:

@Override
    public void onBindViewHolder(MyHolder holder, int position) {
        myObject currentItem = rewardsList.get(position);
        holder.bindList(currentItem, listener, position);
    }

接下来,在您的 ViewHolder 中添加:

class MyHolder extends RecyclerView.ViewHolder {

        View masterView;


        MyHolder(View itemView) {
            super(itemView);

            masterView = itemView;


        }

        void bindList(final myObject item, final OnItemClickListener listener, int position){

            masterView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(item, position,"edit");
                }
            });

...

最后,在调用 activity/fragment 时:

MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(mActivity, list, R.layout.item, new MyRecyclerViewAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(MyObject item, int position, String message) {

                        //item clicks handled here

                        switch (message){
                            case ITEM_OPEN:
                                // Do whatever you need with clicked item here
                                break;

                            case ITEM_EDIT:
                                // Do whatever you need with clicked item here
                                break;


                        }
                    }
                }, mRecyclerView);