获取 recyclerview 项目的上下文而不是其位置 android

Geting context of recylerview item instead of its postion android

我的应用程序中有一个 recylerview,我可以获取每个项目的位置,但是,我不太确定如何从 recylerview 获取位置的上下文;

    mRecyclerView.addOnItemTouchListener(
            new RecyclerItemClickListener(getActivity(), new RecyclerItemClickListener.OnItemClickListener() {

                @Override
                public void onItemClick(View view, int position) {
                    Snackbar.make(view, "clicked" + " " + position, Snackbar.LENGTH_LONG).setAction("Action", null).show();
                }
            })
    );

此外,每个 item/column 结构都包含一个 textView,我要向其添加数据,如下所示;

list.add(new itemtoadd("John Smith" + "\nMale" + "\nSingle”));

我想做的是因为我只有一个 textview 包含 3 组数据 Name+gender+status,同时获取位置的上下文我只想从中获取 1 条数据,例如人名。

编辑:

现在我写的代码获取 recylerview 项目的位置,例如它会说点击 1,我想做的不是说点击 1,我希望它获取位置 1 的上下文例如,在这种情况下得到 'John Smith, Male, Single'

我的意思是,例如我的 recylerview 中有这些数据;

list.add(new itemtoadd("John Smith0" + "\nMale0" + "\nSingle0”));
list.add(new itemtoadd("John Smith1" + "\nMale1" + "\nSingle1”));
list.add(new itemtoadd("John Smith1" + "\nMale1" + "\nSingle1”));

在这种情况下,我添加到我的 recylerview 的第一个数据将位于位置 0,而不是打印出来 'position 0'我想在位置 0 中获取数据,在这种情况下是;约翰·史密斯0,男0,单身0。

使用模型对象 itemtoadd 检索数据,而不是 TextView:

@Override
public void onItemClick(View view, int position) {

    Snackbar.make(view, "clicked" + " " + position, Snackbar.LENGTH_LONG).setAction("Action", null).show();

    itemtoadd yourItem = list.get(position); // here you get your clicked model item

    // then you can get the String value from your "itemtoadd" model.
}

编辑:

这是您的 itemtoadd 模型的示例:

public class itemtoadd {

    private String mName;
    private String mGender;
    private String mStatus;


    public itemtoadd(String name, String gender, String status){
       mName = name;
       mGender = gender;
       mStatus = status;
    }

    public String getName(){
      return mName;
    }

    public String getGender(){
      return mGender;
    }

    public String getStatus(){
      return mStatus;
    }

    public String getPersonInformation(){
      return getName()+"\n"+getGender()+ "\n"+getStatus();
    }

}

通过上面的 class,您可以获得 姓名性别身份 与分别 yourItem.getName(), yourItem.getGender(), yourItem.getStatus().