title: Two line ListView - combine alphabet scroller and section headers
body: <p>I have recently and successfully created a two line list view in Android Studio. However what I would like to do now is to implement alphabetical scrolling and section headers for letters of the alphabet that contain items beginning with that letter e.g. items beginning with A to be under a section header named 'A'. I also want section headers for letters containing empty content not to appear i.e. for the letter 'W' if there are no items in my list begging with W then I don't want the 'W' section header to appear etc.  </p>
  
  <p>The image below is what my activity currently looks like, and the image below that is what I am trying to achieve (two lines per item not one). The 4 sections of code below represent what I have used to create my list view (as seen in the first image).</p>
  
  <p>Please bear in mind all that needs to be done is to add an alphabet scroller and section headers hence I would be very grateful if when giving an answer to provide the most efficient way of achieving the above as well as indicating if something needs to be added, taken away or even combined.</p>
  
  <p><img src="https://i.stack.imgur.com/xKmWO.png" alt="Bakerloo line list">
  <img src="https://i.stack.imgur.com/4pm95.png" alt="list with alphabet scroller and section headers"></p>
  
  <p>actitivty_bakerloo_line.xml</p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context="com.apptacularapps.exitsexpertlondonlite.BakerlooLineActivity"&gt;
  
  &lt;ListView
      android:id="@+id/BakerloolineListView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>BakerlooLineActivity.java</p>
  
  <pre><code>package com.apptacularapps.exitsexpertlondonlite;
  
  import android.app.ActionBar;
  import android.app.Activity;
  import android.graphics.Color;
  import android.graphics.drawable.ColorDrawable;
  import android.os.Bundle;
  import android.text.Html;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ListView;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  
  
  public class BakerlooLineActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_bakerloo_line);
  
          getActionBar().setDisplayHomeAsUpEnabled(true);
  
          ActionBar bar = getActionBar();
          bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#B36305")));
          getActionBar().setTitle(Html.fromHtml("&lt;font color='#FFFFFF'&gt;Bakerloo line&lt;/font&gt;"));
  
          ArrayList&lt;SearchResults&gt; searchResults = GetSearchResults();
  
          final ListView lv = (ListView) findViewById(R.id.BakerloolineListView);
          lv.setAdapter(new MyCustomBaseAdapter(this, searchResults));
  
          lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; a, View v, int position, long id) {
                  Object o = lv.getItemAtPosition(position);
                  SearchResults fullObject = (SearchResults)o;
              }
          });
      }
  
      private ArrayList&lt;SearchResults&gt; GetSearchResults(){
          ArrayList&lt;SearchResults&gt; results = new ArrayList&lt;SearchResults&gt;();
  
          SearchResults sr = new SearchResults();
          sr.setStation("Baker Street");
          sr.setFareZone("Fare zone 1");
          results.add(sr);
  
          sr = new SearchResults();
          sr.setStation("Charing Cross");
          sr.setFareZone("Fare zone 1");
          results.add(sr);
  
          sr = new SearchResults();
          sr.setStation("Edgware Road");
          sr.setFareZone("Fare zone 1");
          results.add(sr);
  
          return results;
      }
  }
  </code></pre>
  
  <p>SearchResults.java</p>
  
  <pre><code>package com.apptacularapps.exitsexpertlondonlite;
  
  public class SearchResults {
      private String station = "";
      private String fareZone = "";
  
      public void setStation(String station) {
          this.station = station;
      }
      public String getStation() {
          return station;
      }
  
      public void setFareZone(String fareZone) {
          this.fareZone = fareZone;
      }
      public String getFareZone() {
          return fareZone;
      }
  }
  </code></pre>
  
  <p>MyCustomBaseAdapter.java</p>
  
  <pre><code>package com.apptacularapps.exitsexpertlondonlite;
  
  import java.util.ArrayList;
  
  import com.apptacularapps.exitsexpertlondonlite.R;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.TextView;
  
  public class MyCustomBaseAdapter extends BaseAdapter {
      private static ArrayList&lt;SearchResults&gt; searchArrayList;
  
      private LayoutInflater mInflater;
  
      public MyCustomBaseAdapter(Context context, ArrayList&lt;SearchResults&gt; results) {
          searchArrayList = results;
          mInflater = LayoutInflater.from(context);
      }
  
      public int getCount() {
          return searchArrayList.size();
      }
  
      public Object getItem(int position) {
          return searchArrayList.get(position);
      }
  
      public long getItemId(int position) {
          return position;
      }
  
      public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          if (convertView == null) {
              convertView = mInflater.inflate(R.layout.dualline_row_view, null);
              holder = new ViewHolder();
              holder.txtStation = (TextView) convertView.findViewById(R.id.station);
              holder.txtFareZone = (TextView) convertView.findViewById(R.id.farezone);
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
  
          holder.txtStation.setText(searchArrayList.get(position).getStation());
          holder.txtFareZone.setText(searchArrayList.get(position).getFareZone());
  
          return convertView;
      }
  
      static class ViewHolder {
          TextView txtStation;
          TextView txtFareZone;
      }
  }
  </code></pre>
  
  <p><strong>Side list</strong>
  <img src="https://i.stack.imgur.com/pC0wj.png" alt="side list"></p>
  
  <pre><code>package com.apptacularapps.exitsexpertlondonlite;
  
  import android.app.ActionBar;
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Color;
  import android.graphics.drawable.ColorDrawable;
  import android.os.Bundle;
  import android.text.Html;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class Bakerloo_line extends Activity {
  
      ListView sideList;
  
  
      ArrayList&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
      ListView listview=null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_bakerloo_line);
  
          getActionBar().setDisplayHomeAsUpEnabled(true);
          getActionBar().setDisplayShowHomeEnabled(false);
  
          ActionBar bar = getActionBar();
          bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#B36305")));
          getActionBar().setTitle(Html.fromHtml("&lt;font color='#FFFFFF'&gt;Bakerloo line&lt;/font&gt;"));
  
  
          listview=(ListView)findViewById(R.id.listView_Bakerlooline);
  
          items.add(new SectionItem("My Friends"));
          items.add(new EntryItem("Abhi Tripathi", "Champpu"));
          items.add(new EntryItem("Sandeep Pal", "Sandy kaliya"));
          items.add(new EntryItem("Amit Verma", "Budhiya"));
          items.add(new EntryItem("Awadhesh Diwaker ", "Dadda"));
  
          items.add(new SectionItem("Android Version"));
          items.add(new EntryItem("Jelly Bean", "android 4.2"));
          items.add(new EntryItem("IceCream Sandwich", "android 4.0"));
          items.add(new EntryItem("Honey Comb", "android 3.0"));
          items.add(new EntryItem("Ginger Bread ", "android 2.2"));
  
          items.add(new SectionItem("Android Phones"));
          items.add(new EntryItem("Samsung", "Galaxy"));
          items.add(new EntryItem("Sony Ericson", "Xperia"));
          items.add(new EntryItem("Nokia", "Lumia"));
  
  
          EntryAdapter adapter = new EntryAdapter(this, items);
          listview.setAdapter(adapter);
  
  
  
          sideList = (ListView)findViewById(R.id.sideIndex);
          List&lt;String&gt; sideIndexList = new ArrayList&lt;String&gt;();
          sideIndexList.add("A");
          sideIndexList.add("B");
          sideIndexList.add("C");
          sideIndexList.add("D");
          sideIndexList.add("E");
          sideIndexList.add("F");
          sideIndexList.add("G");
          sideIndexList.add("H");
          sideIndexList.add("I");
          sideIndexList.add("J");
          sideIndexList.add("K");
          sideIndexList.add("L");
          sideIndexList.add("M");
          sideIndexList.add("N");
          sideIndexList.add("O");
          sideIndexList.add("P");
          sideIndexList.add("Q");
          sideIndexList.add("R");
          sideIndexList.add("S");
          sideIndexList.add("T");
          sideIndexList.add("U");
          sideIndexList.add("V");
          sideIndexList.add("W");
          sideIndexList.add("X");
          sideIndexList.add("Y");
          sideIndexList.add("Z");
  
          ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.side_index_row,sideIndexList );
          sideList.setAdapter(arrayAdapter);
  
          listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int position, long id) {
                  //When clicked, go to specific activity
                  if(position == 0) {
                      //code specific to first list item
                      Intent myIntent = new Intent(view.getContext(), Bakerloo_line.class);
                      startActivityForResult(myIntent, 0);
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p><strong>XML page</strong>
      
      </p>
  
  <pre><code>    &lt;ListView
          android:id="@+id/listView_Bakerlooline"
          android:layout_width="0dp"
          android:layout_height="match_parent"
          android:layout_weight="1"
          android:fastScrollEnabled="true" /&gt;
  
      &lt;ListView
          android:id="@+id/sideIndex"
          android:layout_width="40dip"
          android:layout_height="fill_parent"
          android:background="#ff996633"
          android:divider="@null"
          android:dividerHeight="0dp"
          android:orientation="vertical" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>side_index_row.xml</strong></p>
  
  <pre><code>&lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/side_index_row"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:textColor="@color/white"
      android:textSize="14sp"
      android:gravity="center"&gt;
  &lt;/TextView&gt;
  </code></pre>