Android 如何访问“回收器”视图中的值?

Android 如何访问“回收器”视图中的值?,android,Android,我正在尝试访问我的RecyclerView中的值,并将它们传递给另一个项目。我有一个onClick() private-List-mItemList; 私有上下文; 公共静态类ViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener { 公共CheckedTextView名称CheckedTextView; 公共文本视图描述框,日期,纬度,经度; 公共视图持有者(视图项视图) { 超级(项目视图); setOnClickListene

我正在尝试访问我的
RecyclerView
中的值,并将它们传递给另一个项目。我有一个
onClick()

private-List-mItemList;
私有上下文;
公共静态类ViewHolder扩展了RecyclerView.ViewHolder实现了View.OnClickListener
{
公共CheckedTextView名称CheckedTextView;
公共文本视图描述框,日期,纬度,经度;
公共视图持有者(视图项视图)
{
超级(项目视图);
setOnClickListener(这个);
nameCheckedTextView=(CheckedTextView)itemView.findViewById(R.id.CheckedTextView);
descriptionBox=(TextView)itemView.findViewById(R.id.itemDescription);
date=(TextView)itemView.findViewById(R.id.itemDate);
纬度=(TextView)itemView.findViewById(R.id.itemLatitude);
经度=(TextView)itemView.findViewById(R.id.itemLength);
}
@凌驾
公共void onClick(视图)
{
Intent Intent=新Intent(这个,EditItem.class);
Toast.makeText(view.getContext(),“position=“+getPosition(),Toast.LENGTH_SHORT).show();
}
}
问题是,我不知道如何获取该项的实际字段值,以便将其传递给下一个活动。有人知道是否有一种简单的方法可以做到这一点吗?

您可以访问
onBindViewHolder()
(在recyclerView适配器中)中的“项”:

和内部视窗支架:

public void update(Item item) {
    // here you can use "item"
}
编辑:您可以在
ViewHolder
内和
update()内设置
private Item项目
,执行以下操作:

public void update(Item item) {
    this.item = item;
}

现在您可以在onBindViewHolder中使用
this.item
inside
onClick()

如果您将一些数据作为最终字符串或最终整数,然后创建一个具有这些最终值的onClickListener,那么每次调用新的onClickListener时,都会调用其中的最终值

这是我的应用程序的简化版本:

public void onBindViewHolder(FR_Adapter.ViewHolder holder, final int positionCard) {

    //this is where you call the db and fill the card with data,
    CardView cardView = holder.cardView;
    TextView textView = (TextView) cardView.findViewById(R.id.question);
    final String stringInTextView = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    textView.setText(stringInTextView); // this example should only show the time when the card was created

    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            reactToClick(positionCard,stringInTextView,true);
        }
    });
}

private void reactToClick(int positionCard,String stringInTextView, boolean wasClick) {
    Log.v("custom Log.v call","click at position " + positionCard + "text in card was" + stringInTextView);
}
(基本上,如果您有20张卡,那么20个onClickListener中的每一张都会记住您在第一次显示卡时保存的字符串+该int positionCard)

//若要添加更多变量,只需1将其创建为最终变量,2将其添加到方法调用中,3修改方法使其具有更多参数:

最后一个字符串newVariable=MainActivity.getAppStartTime()//如果我的onClick方法在ViewHolder中,这对我有什么帮助?它无法识别
,我也完全不明白你想做什么。因此,如果卡上有多个字符串,我需要将该信息传递给
reactoclick
方法吗?上面是info@GabrielaMarinho
public void onBindViewHolder(FR_Adapter.ViewHolder holder, final int positionCard) {

    //this is where you call the db and fill the card with data,
    CardView cardView = holder.cardView;
    TextView textView = (TextView) cardView.findViewById(R.id.question);
    final String stringInTextView = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    textView.setText(stringInTextView); // this example should only show the time when the card was created

    cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            reactToClick(positionCard,stringInTextView,true);
        }
    });
}

private void reactToClick(int positionCard,String stringInTextView, boolean wasClick) {
    Log.v("custom Log.v call","click at position " + positionCard + "text in card was" + stringInTextView);
}
//to add more vars just 1create them as final, 2add them in the method call, 3modify the method to have more arguments:

final String newVariable = MainActivity.getAppStartTime() // << 1add this

reactToClick(positionCard,stringInTextView,true,newVariable); // << 2edit this

reactToClick(int positionCard,String stringInTextView, boolean wasClick, String newVariable) // << 3edit this