2010-02-06 2 views
183

ListAdapter의 사용자 지정 구현을 작성 중입니다.컨텍스트가 지정된 레이아웃 Inflater를 얻는 방법?

생성자에서 리소스 ID (즉, 레이아웃 파일을 나타내는 R.id.xxx)와 목록 및지도 (여기에 데이터가 포함되어 있음)를 가져옵니다.

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

문맥 만 주어진 LayoutInflater를 어떻게 잡아낼 수 있습니까?

지금, 이것이 가능한 이유는 이것이 ArrayAdapter (context, resource, textViewResourceId, 데이터 배열)의 생성자에 전달되는 것과 매우 유사하며 ArrayAdapter가 Context 만 주어진 LayoutInflater를 사용하십시오.

하지만 어떻게 할 수 있습니까?

LayoutInflater li = LayoutInflater.from(context); 

답변

450

당신은이 staticfrom() method from the LayoutInflater class 사용할 수 있습니다

LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) 
+10

감사합니다! Context.getSomething(). getAnotherThing(). getLayoutInflater()를 찾으려고했습니다. –

+0

이것은 나를 위해 일한 유일한 방법입니다. 지금까지 시도한 다른 모든 것은 예외를 던졌습니다. – num1

+0

가장 좋은 해결책, 200 번째 upvote가되어 기쁩니다 :) – tbraun

48

또한 위해 LayoutInflater를 얻기 위해이 코드를 사용할 수 있습니다 :

+37

LayoutInflater.from (Context ctx)과이 getSustemService (...)의 차이점은 무엇입니까? –

+8

+1, 좋은 질문을 위해 LayoutInflater.from (컨텍스트) 메서드 구현에서 context.getSystemService()를 호출하여 System Manager에서 LayoutInflater Service Provider를 가져옵니다. 그래서 호출 스택에 쓰레기 차이가있을 수 있습니다. 인플레이터가 입수해온 수없는 경우 – NguyenDat

+9

LayoutInflater.from (문맥)과 같은 오류가 발생 여기 코드 (컨텍스트 컨텍스트) { LayoutInflater에서 LayoutInflater에서 = (LayoutInflater에서) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE)로부터 공개 LayoutInflater에서 고정하는 단계; if (LayoutInflater == null) { 새로운 AssertionError를 throw합니다 ("LayoutInflater not found."); } return LayoutInflater; } – Hiep

관련 문제