2011-07-22 23 views
20

Posiadam dostosowaną kartę, która ma nagłówek i niestandardowe wiersze. Czasami moja funkcja v.getTag() zwraca wartość null, w której zapisałem mój obiekt ViewHolder. Nie zdarza się to cały czas i nie wiem, kiedy i dlaczego to się zgadza.Metoda v.getTag() zwraca wartość null zamiast obiektu ViewHolder

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 

    //Header 
    if(items.hasDescription() && 0 == position) { 
     LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(R.layout.app_list_header, null); 
     ((TextView) v.findViewById(R.id.app_list_header_description_text)).setText(items.getDescription()); 
     return v; 
    } 

    ViewHolder holder; 
    // Inflate app view. 
    if (v == null || v.getTag() == null) { 
     LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = vi.inflate(textViewResourceId, null); //TODO: parent instead of null? 
     holder = new ViewHolder(); 
     holder.title = (TextView) v.findViewById(R.id.title); 
     holder.company = (TextView) v.findViewById(R.id.company); 
     holder.priceOrStatus = (TextView) v.findViewById(R.id.price); 
     holder.rating = (RatingBar) v.findViewById(R.id.rating); 
     holder.icon = (ImageView) v.findViewById(R.id.icon); 
     v.setTag(holder); 
    } else { 
     holder = (ViewHolder) v.getTag(); 
    } 

} 
    App app; 
    if(items.hasDescription()) { 
     app = items.get(position-1);  
    } else { 
     app = items.get(position); 
    } 

    // TODO: Do we need this? 
    if (null == app || null == holder) { 
     Log.d(TAG, "app: " +app +" holder: " +holder); 
     return v; 
    } 

    //TODO: FIX THE XML BEFORE SO WE DO NOT NEED TO TRIM IT. 
    // And get rid of all these ifs!! 
    if(holder.title != null) { 
     holder.title.setText(app.getTitle().trim());        

    } 

Czy ktoś może mi pomóc?

Odpowiedz

38

Tutaj używasz standardowego wzoru dla niestandardowego ListAdapter. Nie wszystkie widoki zostaną poddane recyklingowi, np. kiedy są po raz pierwszy utworzone, aby wypełnić ListView.

Można też wziąć odniesienie The LayoutInflater podczas tworzenia adapter nieznacznie poprawić wydajność, patrz snippet poniżej

private class AlertListAdapter extends ArrayAdapter<Alert> 
{ 
     private ViewHolder  holder; 
     private LayoutInflater mInflater; 

     public AlertListAdapter(Context context, List<Alert> items) 
     { 
      super(context, R.layout.dashboard_layout, items); 
      mInflater = LayoutInflater.from(context); 
     } 

     public View getView(int position, View recycledView, ViewGroup parent) 
     { 
      if (recycledView == null || recycledView.getTag() == null) 
      { 
       recycledView = mInflater.inflate(R.layout.list_item, null); 

       holder = new ViewHolder();     
       holder.header = (LinearLayout) recycledView.findViewById(R.id.alert_list_item_header); 
       holder.header_text = (TextView) recycledView.findViewById(R.id.alert_list_item_header_text); 
       holder.header_count = (TextView) recycledView.findViewById(R.id.alert_list_item_header_count); 
       holder.name = (TextView) recycledView.findViewById(R.id.alert_list_item_name); 
       holder.distance = (TextView) recycledView.findViewById(R.id.alert_list_item_distance); 

       recycledView.setTag(holder); 
      } 
      else 
      { 
       holder = (ViewHolder) recycledView.getTag(); 
      } 

       holder.header_text.setText(title.substring(0, space)); 
       holder.name.setText(title.substring(space + 1)); 
       holder.header_count.setText(count);  
       holder.header.setBackgroundResource(resourceID);  

      return recycledView; 
     } 
    } 

Zasadniczo trzeba być zawsze przygotowanym na v.getTag() wrócić nieważną i nadmuchać nowa View odpowiednio.

+4

INSANE! Android 2.3 daje mi od czasu do czasu znaczniki NULL, Android 3+ nigdy tego nie robi – max4ever

+2

Na moim Galaxy Ace też to widzę. Zwykle getTag zawsze zwróci posiadacza. Ale w oparciu o to zgadzam się jedno_małe_ sprawdzenie, czy getTag jest ważny. – slott

Powiązane problemy