๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ์—์„œ ๋ช‡๋ฒˆ ์งธ ํ–‰์„ ๋ˆŒ๋ €๋Š” ์ง€ ์•Œ ์ˆ˜ ์žˆ๋Š”, ์–ด๋Œ‘ํ„ฐ ํ•จ์ˆ˜(getAdapterPosition()) ๋ณธ๋ฌธ

๐Ÿค–Android

[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์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•จ.

    }
});

 

๋ฐ˜์‘ํ˜•