如何使用onClick侦听器将字符串从TextView传输到主要活动

我想创建一个onclick侦听器,该侦听器从一个TextView中读取字符串,然后以某种方式将该字符串传递到主活动上,以打开第三个活动

所以我的主要活动是这样的

public class AdvancedResults extends AppCompatActivity {
private RecyclerView mRecyclerView;
private AdvancedAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mImageUrls = new ArrayList<>();
private ArrayList<String> saved_recipes_with_ingredients = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_advanced_results);
    advancedList.add(new AdvancedItem(R.drawable.ic_android, "test1", "test2");
    advancedList.add(new AdvancedItem(R.drawable.ic_android, "test3", "test4");
    mRecyclerView = findViewById(R.id.recyclerView1);
    mRecyclerView.setHasFixedSize(true);
    mLayoutManager = new LinearLayoutManager(this);
    mAdapter = new AdvancedAdapter(advancedList);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnItemClickListener(new AdvancedAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(int position) {
            // I want to open a new activity and append the string from the TextView here (maybe with get/set)
        }
    });
}

我的回收站适配器看起来像这样

public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
private ArrayList<AdvancedItem> mAdvancedList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
    void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
    mListener = listener;
}
public static class AdvancedViewHolder extends RecyclerView.ViewHolder {
    public ImageView mImageView;
    public TextView mTextView1;
    public TextView mTextView2;
    public AdvancedViewHolder(View itemView, final OnItemClickListener listener) {
        super(itemView);
        mImageView = itemView.findViewById(R.id.imageViewADV);
        mTextView1 = itemView.findViewById(R.id.textViewADV1);
        mTextView2 = itemView.findViewById(R.id.textViewADV2);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    int position = getAdapterPosition();
                    //how do I get the string from the TextView and pass it onto the main class?
                    if (position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(position);
                    }
                }
            }
        });
    }
}
public AdvancedAdapter(ArrayList<AdvancedItem> advancedList) {
    mAdvancedList = advancedList;
}

@Override
public AdvancedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.advanced_item, parent, false);
    AdvancedViewHolder evh = new AdvancedViewHolder(v, mListener);
    return evh;
}
@Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
    AdvancedItem currentItem = mAdvancedList.get(position);

    holder.mImageView.setImageResource(currentItem.getImageResource());
    holder.mTextView1.setText(currentItem.getText1());
    holder.mTextView2.setText(currentItem.getText2());
}
@Override
public int getItemCount() {
    return mAdvancedList.size();
}

我只是不确定我应该怎么做以及使用什么方法。

评论
  • matque
    matque 回复

    因此,如果要从文本视图中输入文本,请执行以下操作:

    String text = textViewId.getText();
    

    但是对于您的情况,您想为列表中的每个视图添加文本

    因此,在适配器上如何创建点击监听器:

                if (listener != null) {
                    int position = getAdapterPosition();
    
    
                    String text = mTextView1.getText();
    
                    Intent i = new Intent(getApplicationContext(), YourActitivity.class);  
                    i.putExtra("nameYouWant", text)
                    startActivity(i);  
    
    
                    if (position != RecyclerView.NO_POSITION) {
                        listener.onItemClick(position);
                    }
    

    现在开始您的活动:

    String text = intent.getStringExtra("nameYouWant");