군침이 싹 도는 코딩

리사이클러뷰에서 몇 번째 행을 눌렀는지 알 수 있는 어댑터의 함수 ( getAdapterPosition() ) 본문

Android

리사이클러뷰에서 몇 번째 행을 눌렀는지 알 수 있는 어댑터의 함수 ( getAdapterPosition() )

mugoori 2023. 2. 1. 17:42
public class viewHolder extends RecyclerView.ViewHolder{

    TextView txtName;
    TextView txtPhone;
    ImageView imgDelete;
    CardView cardView;

    public viewHolder(@NonNull View itemView) {
        super(itemView);
        txtName = itemView.findViewById(R.id.txtName);
        txtPhone = itemView.findViewById(R.id.txtPhone);
        imgDelete = itemView.findViewById(R.id.imgDelete);
        cardView = itemView.findViewById(R.id.cardView);

        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 클릭하면 해당 인덱스를 뽑는것
                int index = getAdapterPosition();
		// 뽑아온 인덱스의 행의 정보를 가져오는것 
                Contact contact = contactList.get(index);

# 행을 눌렀을때 인덱스를 뽑아오는 함수는 getAdapterPosition() 을 쓰고

뽑아온 인덱스를 통해 행의 정보를 가져오는것은 contactList.get( index ) 를 쓴다