安卓。在回收器视图中点击图片按钮时,从Textview中传递文本。

所以我有一个有2个文本和1个图像按钮的回收器视图。我想点击图像按钮,然后打开一个新的活动,并将文本从文本中转移到图片中。textViewADV1 转入下一个活动

在我的回收视图中,我的项目.xml包括以下内容

 <ImageButton
        android:id="@+id/imageButton1"
        android:clickable="true"
        android:onClick="openActivity2"/>

    <TextView
        android:id="@+id/textViewADV1"
        android:text="Line 1"/>

    <TextView
        android:id="@+id/textViewADV2"
        android:text="Line 2"/>

我的主活动中的openActivity2()

public void openActivity2(View view)
        {
            Intent intentLoadNewActivity = new Intent(AdvancedResults.this,OpenSelectedAdvanced.class);
            startActivity(intentLoadNewActivity);
        }

我试着做 textViewADV1.getText().toString() 但它只读取回收器视图中第一个项目的文本。

我的回收机适配器

public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {
    private ArrayList<AdvancedItem> mAdvancedList;

public String mImage;

    public static class AdvancedViewHolder extends RecyclerView.ViewHolder {
        public ImageButton mImagebtn;
        public TextView mTextView1;
        public TextView mTextView2;

        public AdvancedViewHolder(View itemView) {
            super(itemView);
            mImagebtn = itemView.findViewById(R.id.imageButtonADV);
            mTextView1 = itemView.findViewById(R.id.textViewADV1);
            mTextView2 = itemView.findViewById(R.id.textViewADV2);
        }
    }

    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);
        return evh;
    }

    @Override
    public void onBindViewHolder(AdvancedViewHolder holder, int position) {
        AdvancedItem currentItem = mAdvancedList.get(position);
        holder.mTextView1.setText(currentItem.getText1());
        holder.mTextView2.setText(currentItem.getText2());
        mImage = "www.testImageURL.com";
        Picasso.get().load(mImage).into(holder.mImagebtn);
    }

    @Override
    public int getItemCount() {
        return mAdvancedList.size();
    }
}

解决方案:

首先通过一个 context 对你的 adapter 当你初始化它的时候。

//in the main activity that you initialize the adapter in

AdvancedAdapter adapter = new AdvancedAdapter(your_list , MainActivity.this);

现在改变它的构造函数 AdvancedAdapter 接受 context

 public class AdvancedAdapter extends RecyclerView.Adapter<AdvancedAdapter.AdvancedViewHolder> {

private ArrayList<AdvancedItem> mAdvancedList;

private Context context;

.............

//constructor

public AdvancedAdapter(ArrayList<AdvancedItem> advancedList,Context context) {
mAdvancedList = advancedList;
this.context = context;
}

现在在 onBindViewHolderAdvancedAdapter

@Override
public void onBindViewHolder(AdvancedViewHolder holder, int position) {
AdvancedItem currentItem = mAdvancedList.get(position);
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
mImage = "www.testImageURL.com";
Picasso.get().load(mImage).into(holder.mImagebtn);

//on click image button

hodler.mImagebtn.setOnclickListener(new View.OnClickListener(){

@Override
public void onClick(View view){

Intent intent = new Intent(context , OpenSelectedAdvanced.class);
intent.putExtra("data" , currentItem.getText1());
context.startActivity(intent);
}

});

}

要从以下地方获取数据 OpenSelectedAdvanced 活动。

//in oncreate method:

Intent intent = getIntent();

//this is your text view text that came from the clicked image button
String transferedText = intent.getStringExtra("data");

给TA打赏
共{{data.count}}人
人已打赏
未分类

如何用 re.sub 捕获一个字符串,但也让部分结果再次被捕获?

2022-9-9 5:08:18

未分类

使用Vuetify.js在代码中更新了v-model但在屏幕上没有更新

2022-9-9 5:08:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索