2013-02-27 15 views
10

Myślę, że to jest pytanie dla ekspertów.Indeks tablicy Android ListView poza zakresem po filtrze

uzyskać połączenia do getView() z STANOWISKApoza granicami z listy danych ListView.
Dzieje się tak, gdy używam filtra adaptera. Metoda filtru publishResults() wypełnia dane przefiltrowaną listą, która jest mniejsza niż oryginalna lista.
Ten błąd pojawia się, gdy nowa przefiltrowana lista jest krótsza niż poprzednia filtrowana lista. Zmieniłem kod getView(), aby zwrócić manekina convertView, gdy jest poza zakresem, aby sprawdzić, ile takich połączeń jest wysyłanych.

Są to odpowiedni kod i wiadomości log Zalogowałem:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // No logs here to keep ListView performance good 
     Log.d(TAG, "+ getView(position=" + position + ")"); 
     ViewHolder holder; 

     if(position >= mData.size()) { 
      // This code allows to see how many bad calls I get 
      Log.w(TAG, "position out of bounds!"); 
      convertView = mInflater.inflate(mLayout, parent, false); 
      return convertView; 
     } 

     . . . // Normal getView code 

     return convertView; 
    } 

W filtrze (kod skopiowane z kodu ArrayAdapter źródłowego)

 @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      Log.pe(TAG, "+ publishResults(constraint:" + constraint + ", results.count:" + results.count + ")"); 
      //noinspection unchecked 
      mData = (ArrayList<String>) results.values; 
      if (results.count > 0) { 
       notifyDataSetChanged(); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
      Log.px(TAG, "- publishResults()"); 
     } 

Plik dziennika wynika, że ​​po filtr z 7 wynikami, przychodzi fitler z 3 wynikami, ale getView ciągle otrzymuje wywołania do 7 pozycji (oznaczono *** z połączeń poza granicami):

02-26 05:31:55.986: D/ViewerActivity(22857): + onQueryTextChange(newText:log) 
02-26 05:31:55.986: D/ViewerActivity(22857): - onQueryTextChange() 
02-26 05:31:56.029: D/LogScreenAdapter(22857): + performFiltering(prefix:log) 
02-26 05:31:56.113: D/dalvikvm(22857): GC_CONCURRENT freed 378K, 5% free 13577K/14215K, paused 0ms+1ms 
02-26 05:31:56.153: D/LogScreenAdapter(22857): - performFiltering() 
02-26 05:31:56.153: D/LogScreenAdapter(22857): + publishResults(constraint:log, results.count:7) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): - publishResults() 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=3) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=4) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=5) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=6) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=3) 
02-26 05:31:56.167: D/LogScreenAdapter(22857): + getView(position=4) 
02-26 05:31:56.493: D/LogScreenAdapter(22857): + getView(position=5) 
02-26 05:31:56.503: D/LogScreenAdapter(22857): + getView(position=6) 
02-26 05:32:23.793: D/ViewerActivity(22857): + onQueryTextChange(newText:logs) 
02-26 05:32:23.793: D/ViewerActivity(22857): - onQueryTextChange() 
02-26 05:32:23.813: D/LogScreenAdapter(22857): + performFiltering(prefix:logs) 
02-26 05:32:23.854: D/dalvikvm(22857): GC_CONCURRENT freed 383K, 5% free 13577K/14215K, paused 0ms+0ms 
02-26 05:32:23.924: D/dalvikvm(22857): GC_CONCURRENT freed 388K, 5% free 13573K/14215K, paused 0ms+1ms 
02-26 05:32:23.974: D/LogScreenAdapter(22857): - performFiltering() 
02-26 05:32:23.983: D/LogScreenAdapter(22857): + publishResults(constraint:logs, results.count:3) 
02-26 05:32:23.983: D/LogScreenAdapter(22857): - publishResults() 
02-26 05:32:23.983: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.074: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.093: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.113: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:32:24.155: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:32:24.164: D/LogScreenAdapter(22857): + getView(position=3) 
*** 02-26 05:32:24.193: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.233: D/LogScreenAdapter(22857): + getView(position=4) 
*** 02-26 05:32:24.263: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.284: D/LogScreenAdapter(22857): + getView(position=5) 
*** 02-26 05:32:24.313: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.333: D/LogScreenAdapter(22857): + getView(position=6) 
*** 02-26 05:32:24.343: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.353: D/LogScreenAdapter(22857): + getView(position=0) 
02-26 05:32:24.373: D/LogScreenAdapter(22857): + getView(position=1) 
02-26 05:32:24.383: D/LogScreenAdapter(22857): + getView(position=2) 
02-26 05:32:24.403: D/LogScreenAdapter(22857): + getView(position=3) 
*** 02-26 05:32:24.413: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.433: D/LogScreenAdapter(22857): + getView(position=4) 
*** 02-26 05:32:24.443: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.463: D/LogScreenAdapter(22857): + getView(position=5) 
*** 02-26 05:32:24.475: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:32:24.483: D/LogScreenAdapter(22857): + getView(position=6) 
*** 02-26 05:32:24.503: W/LogScreenAdapter(22857): position out of bounds! 
02-26 05:38:26.769: D/dalvikvm(22857): GC_CONCURRENT freed 316K, 5% free 13640K/14215K, paused 0ms+1ms 

Co widzisz tutaj metoda publishResults() zrobił zmienić Mdata z listy 7 elementów do krótszej listy 3 pozycji, patrz powyższy kod, ale Adapter staje się coraz getView() połączeń na liście 7 pozycji, nawet że go już nie ma.
Należy pamiętać, że notifyDataSetChanged() został wywołany z nowym przydziałem danych, więc ListView powinien znać nową listę.

+1

Czy wywoływana jest funkcja getItemCount, a jeśli tak, to, co powraca? Ir nadpisujesz to, opublikuj kod. –

+1

Co zwraca getItemCount? – alex

+0

Nie przesłonię getItemCount, czy powinienem? – ilomambo

Odpowiedz

20

co ty powrót w public int getCount() metody listy widoku niestandardowego zasilacza?

Należy zwrócić jak mData != null? mData.size() : 0,

Pozycja z obligacji jest coraz spowodowane może być wracasz wielkości liście więcej niż dane, aby pokazać się na liście

getCount() metodą niestandardową listę adaptera określa rozmiar z widoku listy więc powinien być rozmiar danych, które przechodzą na liście

18

Wygląda nadrzędnych „getCount()” metoda będzie rozwiązać problem:

@Override 
public int getCount() { 
    return mData.size(); 
} 
+0

Dziękuję bardzo ... +1 za to – Noman

+0

+1, dokładnie to, co było potrzebne. – Mahm00d

+0

to stwierdzenie istnieje, ale jest to ten sam problem – Prasad

0

zrobiłem to i to rozwiązać moje problemy

@Override 
public int getCount() { 

    if (isFiltered == true) { 
     return myFilteredArray.size(); 
    } 

    return myUnfilteredArray.size(); 
} 
Powiązane problemy