Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewHoler class를 안쓰고 직접 코딩해보려고 했는데요 #3

Open
doqsa opened this issue Dec 15, 2018 · 3 comments
Open

Comments

@doqsa
Copy link

doqsa commented Dec 15, 2018

   if (adapterData != null) {
        val item = adapterData!![position]
        val textView1 : TextView = view.findViewById(R.id.text1)
        //vh.textTextView.text = item.title
        textView1.text= item.title
        vh.dateTextView.text = DateFormat.format("yyyy/MM/dd", item.date)
    }

trxtView1이라는 변수를 만들어서 R,id,text1으로 직접 할당했습니다.

이렇게 바꾸니까 에러는 안나는데 텍스트 출력이 안되네요.ㅠㅠ

text1을 직접 지정해서 출력하는 방법은 없는지요.

조언 부탁합니다.

@junsuk5
Copy link
Owner

junsuk5 commented Dec 15, 2018

getView( ) 오버라이드 부분을 뷰홀더 패턴 미적용 버전으로 한다면 다음과 같습니다.
코드는 더 짧은데 매번 레이아웃을 인플레이트하여 성능은 안 좋기 때문에 구글에서 뷰홀더 패턴 적용을 권장하고 있습니다.
그래서 ListView의 단점을 보완한 RecyclerView의 경우에는 뷰홀더 패턴이 강제되고 있습니다.

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View? {
        val view = LayoutInflater.from(parent?.context)
                .inflate(R.layout.item_todo, parent, false)

        val item = adapterData!![position]

        val textView1: TextView = view.findViewById(R.id.text1)
        val dateTextView: TextView = view.findViewById(R.id.text2)
        textView1.text = item.title
        dateTextView.text = DateFormat.format("YYYY/MM/dd", item.date)

        return view
    }

@junsuk5
Copy link
Owner

junsuk5 commented Dec 15, 2018

레이아웃 인플레이트도 문제지만 매번 findViewById를 하는 것도 성능상 안 좋습니다.

@doqsa
Copy link
Author

doqsa commented Dec 15, 2018

아 네 고맙습니다. 늦은시간인데도 답변해주셔서 감사합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants