2014-10-14 13 views
40

Próbuję pokazać moje wyszukiwanie Skutkuje to ListView na Fragment, ale to nie on:getLayoutInflater() w fragmencie

LayoutInflater inflater = getLayoutInflater(); 

Sposób getLayoutInflater (Bundle) w typie Fragment nie jest dotyczy argumentów()

to jest mój kod:

public View getView(int position, View convertView, ViewGroup parent) 
    { 
    LayoutInflater inflater = getLayoutInflater(); 
    View row; 

    row = inflater.inflate(R.layout.list_single, parent, false); 

    TextView textview = (TextView) row.findViewById(R.id.TextView01); 
    ImageView imageview = (ImageView) row 
    .findViewById(R.id.ImageView01); 

    textview.setText(data_text[position]); 
    imageview.setImageResource(data_image[position]); 

    return (row); 

    } 

Jak mogę to naprawić?

+0

'LayoutInflater inflater = LayoutInflater.from (getActivity());' – Blackbelt

Odpowiedz

137

jeśli jesteś we fragmencie chcesz

getActivity().getLayoutInflater(); 

lub

LayoutInflater.from(getActivity()); 

również można zrobić

View.inflate(); 

lub

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
+0

próbuję to LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); ale błąd, „kontekst nie może być rozwiązany” – udinsekomplek

+1

Tak trzeba uzyskać kontekst ... – tyczj

+0

nie rozumiem nie mam kontekstowe Variabel – udinsekomplek

2

teraz i nie wiem od kiedy można je dostać w swoim fragment (wsparcie v4 biblioteka) przez

getLayoutInflater(null); 
+0

Przekazywanie wartości null powoduje utratę wartości motywów. – busylee

+0

@ busylee Parametr jest pakietem zerowalnym - savedFragmentState. Nie rozumiem, gdzie może być połączenie z "wartościami tematycznymi". –

0

Użyj go:

LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
+0

można dodać, proszę dodać więcej informacji powyżej pytania – Kondal

2

W działalności można używać tak:

this.getLayoutInflater(); 

dla Fragment trzeba zastąpić słowo kluczowe "to" z "getActivity()"

getActivity().getLayoutInflater(); 

Mam nadzieję, że to pomoże!

Powiązane problemy