티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to Get a Layout Inflater Given a Context?

컨텍스트가 지정된 레이아웃 인플레이터를 얻는 방법

 문제 내용 

I am writing a custom implementation of a ListAdapter.

ListAdapter의 커스텀 구현을 하는 중입니다.

 

In its constructor, I'm taking in a Context, a resource ID (i.e. R.id.xxx representing the layout file), and a list and a map (these contain the data).

생성자에서 저는 Context, 리소스 ID(예: 레이아웃 파일을 나타내는 R.id.xxx), 리스트 및 맵(데이터 포함)을 가져옵니다.

 

Now, the problem is that i will need a LayoutInflater to get the View object which is in the separate layout XML file.

이제 문제는 별도의 레이아웃 XML 파일에 있는 View 개체를 가져오려면 LayoutInflater가 필요하다는 것입니다.

 

How can I get hold of the LayoutInflater given only the Context?

컨텍스트만 제공되는 레이아웃 인플레이터를 어떻게 사용할 수 있습니까?

 

Now, why I think this is possible, is that this is quite similar to what is being passed in to the constructor of an ArrayAdapter (context, resource, textViewResourceId, data array), and I figure the ArrayAdapter also has to make use of a LayoutInflater given only a Context.

제가 이것이 가능하다고 생각하는 이유는 이것이 ArrayAdapter의 생성자(콘텍스트, 리소스, textViewResourceId, 데이터 배열)에게 전달되는 것과 상당히 비슷하기 때문입니다. 그리고 ArrayAdapter는 또한 컨텍스트만 주어진 LayoutInflater를 사용해야 합니다.

 

But how can it be done?

하지만 어떻게 할 수 있을까요?

 

 

 

 높은 점수를 받은 Solution 

You can use the static from() method from the LayoutInflater class:

LayoutInflater 클래스의 정적 from() 메서드를 사용할 수 있습니다.
 LayoutInflater li = LayoutInflater.from(context);

 

 

 가장 최근 달린 Solution 

You can also use this code to get LayoutInflater:

이 코드를 사용하여 LayoutInflater를 가져올 수도 있습니다.
LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)

 

 

출처 : https://stackoverflow.com/questions/2212197/how-to-get-a-layout-inflater-given-a-context

반응형
댓글
공지사항
최근에 올라온 글