๐คAndroid/Java
[Android] ์๋๋ก์ด๋ - ๋ฆฌ์ฌ์ดํด๋ฌ๋ทฐ์์ ๋ช๋ฒ ์งธ ํ์ ๋๋ ๋ ์ง ์ ์ ์๋, ์ด๋ํฐ ํจ์(getAdapterPosition())
๋ฃจ๋ฆฌ์ผใ
2023. 12. 30. 10:08
๋ฐ์ํ
getAdapterPosition()์ RecyclerView.ViewHolder์์ ํ์ฌ ์์ดํ
์ ์ด๋ํฐ ์์น๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฉ์๋์ด๋ค.
์ด ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฆฌ์ฌ์ดํด๋ฌ๋ทฐ์์ ํ์ฌ ์์ดํ
์ ์์น๋ฅผ ํ์ธํ ์ ์๋ค.
์ฃผ๋ก ์์ดํ
์ ํด๋ฆญํ์ ๋ ํด๋น ์์ดํ
์ ์์น๋ฅผ ๊ฐ์ ธ์์ ์์
์ ์ํํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
getAdapterPosition()
์นด๋๋ทฐ๋ฅผ ์ ํํ ๋ ํด๋น ์นด๋๋ทฐ๋ฅผ ์ธ๋ฑ์ค๋ก ๊ฐ์ ธ์ค๋ ๋ฐฉ๋ฒ์ด๋ค.
getAdapterPostion()์ index ๋ณ์์ ๋ด์ ๋ค์์
ํด๋น ArrayList๋ฅผ get()์ผ๋ก ๋ฐ์ ๋ index๋ก ๋ฐ์ ์ ์๋ค.
์๋๋ Intent์ putExtra() ํจ์๋ก ์ธ๋ฑ์ค๋๊ฐ์ด ๋ณด๋ธ๋ค.
cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// UpdateActivity ์คํ
int index = getAdapterPosition();
Employee employee = employeeArrayList.get(index);
Intent intent = new Intent(context, UpdateActivity.class);
intent.putExtra("index", index);
intent.putExtra("employee",employee);
((MainActivity)context).launcher.launch(intent);
// cast ํ launcher ํฌํจ๋์ด ์๋ Main์์ public์ผ๋ก ๋ณ๊ฒฝํด์ค์ผ ํจ.
}
});
๋ฐ์ํ