2010-11-10 15 views
11

Używam widza do wyświetlania z dynamicznego arrayadapter.it działa, ale wyświetlane dane zmieniają się nieregularnie po przewinięciu listy. Chcę, aby mój widok listy był wypełniany tylko raz, Nie cały czas, kiedy przewijam listę. Jakieś sugestie? Oto mój kodJak zaimplementować uchwyt do widoku?

public View getView(int position, View convertView, ViewGroup parent) {    

    // A ViewHolder keeps references to children views to avoid unneccessary calls    
    // to findViewById() on each row.    
    ViewHolder holder;    
    // When convertView is not null, we can reuse it directly, there is no need    
    // to reinflate it. We only inflate a new View when the convertView supplied    
    // by ListView is null.    

    if (convertView == null) {     

    convertView = mInflater.inflate(R.layout.sample, null); 
    // Creates a ViewHolder and store references to the two children views     
    // we want to bind data to.    
    holder = new ViewHolder();     
    holder.name = (TextView) convertView.findViewById(R.id.text);    
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);     
    convertView.setTag(holder);    
    } else {     
    // Get the ViewHolder back to get fast access to the TextView     
    // and the ImageView. 


    holder = (ViewHolder) convertView.getTag(); 

    }    


    // Bind the data efficiently with the holder. 
    if(_first==true) 
    { 
    if(id<myElements.size()) 
    { 
     holder.name.setText(myElements.get(id)); 
    holder.icon.setImageBitmap(mIcon1); 
    id++; 
    } 
    else 
    { 
    _first=false; 
    } 
    } 
    //holder.icon.setImageBitmap(mIcon2); 
    /*try{ 
    if(id<myElements.size()) 
    id++; 
    else 
    { 
     id--; 
    } 
    } 
    catch(Exception e) 
    { 
    android.util.Log.i("callRestService",e.getMessage()); 
    }*/ 



    return convertView; 

    }   
static class ViewHolder {    
TextView name;    
ImageView icon;   
} 

gdy lista jest ładowany wygląda to tak: http://i.stack.imgur.com/NrGhR.pngalt text po przewinięciu niektóre dane http://i.stack.imgur.com/sMbAD.pngalt text wygląda to tak, i jeszcze raz, gdybym przewinąć do początku wygląda http://i.stack.imgur.com/0KjMa.pngalt text

PS: moja lista musi być w porządku alfabetycznym

+0

można opublikować zrzut ekranu z wydrukiem? –

+0

@Tilsan The Fighter: opublikowali migawki. – Ads

Odpowiedz

5

Aby to zrobić, musisz napisać własną wersję ListView (która jest zła). Jeśli ListView nie działa poprawnie, prawdopodobnie oznacza to, że robisz coś źle.

Skąd pochodzi element id? Otrzymujesz pozycję w swojej metodzie getView(), więc nie musisz martwić się o przekroczenie granic list. Stanowisko jest związane z pozycji elementu na liście, dzięki czemu można uzyskać prawidłowego elementu takiego:

myElements.get(position); 

Gdy dane na liście zmian, można nazwać:

yourAdapter.notifyDataSetChanged() 

Że odbuduje twoją listę z nowymi danymi (przy zachowaniu przewijania i innych rzeczy).

27

Czy próbowałeś tego?

public View getView(int position, View convertView, ViewGroup parent) {    

    // A ViewHolder keeps references to children views to avoid unneccessary calls    
    // to findViewById() on each row.    
    ViewHolder holder;    
    // When convertView is not null, we can reuse it directly, there is no need    
    // to reinflate it. We only inflate a new View when the convertView supplied    
    // by ListView is null.    

    if (convertView == null) {     

    convertView = mInflater.inflate(R.layout.sample, null); 
    // Creates a ViewHolder and store references to the two children views     
    // we want to bind data to.    
    holder = new ViewHolder();     
    holder.name = (TextView) convertView.findViewById(R.id.text);    
    holder.icon = (ImageView) convertView.findViewById(R.id.icon);     
    convertView.setTag(holder);    
    } else {     
    // Get the ViewHolder back to get fast access to the TextView     
    // and the ImageView. 


    holder = (ViewHolder) convertView.getTag(); 

    }    


    // Bind the data efficiently with the holder. 
    holder.name.setText(myElements.get(id)); 
    holder.icon.setImageBitmap(mIcon1); 

    return convertView; 
    } 

static class ViewHolder {    
    TextView name;    
    ImageView icon;   
} 

Jeśli tak, co jest z nim nie tak?

Nie sądzę, aby ładowanie wszystkich wierszy naraz było dobrym pomysłem. W efekcie będziesz miał mnóstwo niepotrzebnych widoków w pamięci, które spowolnią aplikację za darmo. Widoki i operacje na widokach (takich jak nadmuchanie, findViewById, getChild ..) są drogie, powinieneś spróbować ich użyć jak najwięcej. Dlatego używamy ViewHolders.

+0

tak! próbowałem. kiedy przewijam do ostatniego elementu (> 92) nie można załadować elementów w widoku listy! myElement to tablica z pomocą id, otrzymuję każdy element do wyświetlenia w widoku tekstowym. tak, wygląda na to, że ładuję wszystkie dane raz, ale nie wiem, jak je wykorzystać, czy możesz w tym pomóc – Ads

Powiązane problemy