2012-03-12 96 views
0

我按照本教程http://xjaphx.wordpress.com/2011/06/16/viewholder-pattern-caching-view-efficiently/。但是,它並不奏效。你們能幫我怎麼做這個工作嗎?ViewHolder無法正常工作

class Custom extends Activity { 

private ListView listview; 
private ArrayList mListItem; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
     setContentView(R.layout.custom); 

    listview = (ListView) findViewById(R.id.list_view); 
    mListItem = ItemsDisplay.getItems(true); 

    listview.setAdapter(new CustomListAdapter(this, R.id.list_view,mListItem)); 
} 

static class ViewHolder { 
     public TextView textView; 
    } 

    private class CustomListAdapter extends ArrayAdapter { 
     private ArrayList mList; //--CloneChangeRequired 
     private Context mContext; 

     public CustomListAdapter(Context context, int textViewResourceId, 
       ArrayList list) { //--CloneChangeRequired 
      super(context, textViewResourceId, list); 
      this.mList = list; 
      this.mContext = context; 
     } 

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

      final ToggleButton tb; 

      System.out.println("position "+position+" mList.size"+mList.size()); 
      if (view == null) { 

       LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = vi.inflate(R.layout.list_item_bn, null); 
       ViewHolder holder = new ViewHolder(); 
       holder.textView = (TextView)convertView.findViewById(R.id.tv_name); 
       view.setTag(holder); 
      } 

      ItemsDisplay listItem = (ItemsDisplay) mList.get(position); 
      System.out.println("listItem.getName() " +listItem.getName()); 
       if (listItem != null) { 

       ViewHolder holder1 = (ViewHolder)view.getTag(); 
       holder1.textView.setText(listItem.getName()); 

       } 

      return view; 
     } 

    } 
} 

我已經看過這些帖子,但我不知道爲什麼我的代碼仍然沒有工作。 「如果」 條件和getTag 你必須setTag()中: 解決方案 - : How to implement a view holder? ​​

回答

0

變化:

 final ToggleButton tb; 

     System.out.println("position "+position+" mList.size"+mList.size()); 
     if (convertView == null) { 

      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = vi.inflate(R.layout.list_item_bn, null); 
      ViewHolder holder = new ViewHolder(); 
      holder.textView = (TextView)convertView.findViewById(R.id.tv_name); 
      convertView.setTag(holder); 
     } 

     ItemsDisplay listItem = (ItemsDisplay) mList.get(position); 
     System.out.println("listItem.getName() " +listItem.getName()); 
      if (listItem != null) { 

      ViewHolder holder1 = (ViewHolder)convertView.getTag(); 
      holder1.textView.setText(listItem.getName()); 

      } 

     return convertView; 
+0

這給了我一個想法,雖然。謝謝... – antonoVodka 2012-03-12 08:29:59

+0

你也可以看看這個代碼爲什麼它不工作?我想要做的就是在列表視圖的最後一項上使用不同的佈局。 – antonoVodka 2012-03-12 08:49:05

+0

int lastpos = mList.size() - 1; System.out.println(「position:」+ position +「mlist:」+ lastpos); if(position == lastpos){ view = vi.inflate(R.layout.list_item_record,null); holder.textView =(TextView)view.findViewById(R.id.record_view);其他{ view = vi.inflate(R.layout.list_item_bn,null); holder.textView =(TextView)view.findViewById(R.id.tv_name); } view.setTag(holder); – antonoVodka 2012-03-12 08:49:15

0

試試這個:

public View getView(int position, View convertView, ViewGroup parent) { 
    View view = convertView; 
    ViewHolder holder; 
    final ToggleButton tb; 
    System.out.println("position "+position+" mList.size"+mList.size()); 
    if (view == null) { 
     LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = vi.inflate(R.layout.list_item_bn, null); 
     holder = new ViewHolder(); 
     holder.textView = (TextView)view.findViewById(R.id.tv_name); 
     view.setTag(holder); 
      } 
      else { 
       holder = (ViewHolder)view.getTag(); 
      } 
    ItemsDisplay listItem = (ItemsDisplay) mList.get(position); 
    System.out.println("listItem.getName() " +listItem.getName()); 
    if (listItem != null) { 
     holder.textView.setText(listItem.getName()); 
     } 
     return view; 
    } 
0

希望這個作品()在「其他」部分。

 public View getView(int position, View convertView, ViewGroup parent) { 
     View view = convertView; 
     ViewHolder holder ; 
     final ToggleButton tb; 

     System.out.println("position "+position+" mList.size"+mList.size()); 
     if (view == null) { 
      holder = new ViewHolder(); 
      LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      view = vi.inflate(R.layout.list_item_bn, null); 

      holder.textView = (TextView)convertView.findViewById(R.id.tv_name); 
      view.setTag(holder); 
     } 
     else{ 
      holder = (ViewHolder)view.getTag(); 
     } 
     //set the list data here.. 
     ItemsDisplay listItem = (ItemsDisplay) mList.get(position); 

     holder1.textView.setText(listItem.getName()); 



     return view; 
    }