title: Different row layouts in ListView
body: <p>This post is related to this <a href="https://stackoverflow.com/questions/9663569/viewholder-not-working/9663680">ViewHolder not working</a>. On that post, I was following a tutorial on how to use <code>ViewHolder</code> on a <code>ListView</code>.  What I want now is to have the last item on a <code>ListView</code> to have a different layout than the rest. Here's my code:</p>
  
  <pre><code>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);
            }else{
             view = vi.inflate(R.layout.list_item_bn, null);
             holder.textView = (TextView)view.findViewById(R.id.tv_name);
            }
            view.setTag(holder);
  </code></pre>
  
  <p>I just get the <code>ListView</code> size minus <code>1</code> and have a condition that checks the if current position is equal to the last item. However, this code sets the last item's layout same as the rest. <code>getView</code> method full code:</p>
  
  <pre><code>private class CustomListAdapter extends ArrayAdapter { 
      private ArrayList mList;
      private Context mContext;
  
      public CustomListAdapter(Context context, int textViewResourceId,
              ArrayList list) {
          super(context, textViewResourceId, list);
          this.mList = list;
          this.mContext = context;
      }   
  
      public View getView(final int position, View convertView, ViewGroup parent) {
          View view = convertView;
  
          final ToggleButton tb;
  
           if (view == null) {
            ViewHolder holder = new ViewHolder();
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
            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);
            }else{
             view = vi.inflate(R.layout.list_item_bn, null);
             holder.textView = (TextView)view.findViewById(R.id.tv_name);
            }
            view.setTag(holder);
           }
  
           final ItemsDisplay listItem = (ItemsDisplay) mList.get(position);
              if (listItem != null) {
               ViewHolder holder1 = (ViewHolder)view.getTag();
               holder1.textView.setText(listItem.getTag());
  
               view.setOnClickListener(new OnClickListener() {
                      public void onClick(View arg0) { //--clickOnListItem
                       Toast.makeText(getBaseContext(),
                         "Button is "+position+" "+listItem.getTag(),
                         Toast.LENGTH_LONG).show();
                      }
                  });
  
                  tb  = (ToggleButton) view.findViewById(R.id.toggleButton);
                  tb.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
  
                 if(tb.isChecked()){
                  Toast.makeText(getBaseContext(),
                             "Button is "+position+" checked:"+tb.isChecked()+" "+listItem.getTag(),
                             Toast.LENGTH_LONG).show();
                 }else{
                  Toast.makeText(getBaseContext(),
                    "Button is "+position+" checked:"+tb.isChecked()+" "+listItem.getTag(),
                             Toast.LENGTH_LONG).show();
                 }
                }
                  });
              }
  
  
  
          return view;  
      }
  
  }
  </code></pre>
  
  <p>Could anybody help me with this?</p>