title: How to sort ListView alphabetically by spinner selection? [Android]
body: <p>I have a ListView populated by a database. The ListView contains 2 columns, "name" and "venue". In my spinner I have 4 options, "Artist - Ascending", "Artist - Descending", "Venue - Ascending", "Venue - Descending".</p>
  
  <p>My question is, is it possible to change the printed order of my list based on what option is selected in the spinner?</p>
  
  <p>Here is my code so far. Currently the spinner does nothing but display the 4 options.</p>
  
  <p><strong>UPDATE</strong>
  I have updated my code as per the suggestions of the comments. I now have 4 different getAllConcert() methods in my DBManager class and a switch statement in my MainActivity to call each one according to the spinner item selected. Here's what it currently does: When you select an item from the spinner the list doesn't change however when you click a list item and go back to the initial list screen, the list is updated according to the previously selected spinner item.</p>
  
  <p>Can anyone tell me how to update the list live as a spinner item is selected?</p>
  
  <pre><code>package com.example.msdproject;
  
  import android.app.ListActivity;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.content.Intent;
  import android.widget.ArrayAdapter;
  import android.widget.ImageButton;
  import android.widget.ListView;
  import android.view.View;
  import java.sql.SQLException;
  import android.util.Log;
  import android.widget.SimpleCursorAdapter;
  import android.widget.Spinner;
  
  public class MainActivity extends ListActivity {
  
      DBManager db =  new DBManager(this);
      String [] sortBy;
      Spinner spinner1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          spinner1 = (Spinner) findViewById(R.id.spinner1);
          sortBy = getResources().getStringArray(R.array.sortBy);
          ArrayAdapter&lt;String&gt; spinnerAdapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_item, sortBy);
          spinner1.setAdapter(spinnerAdapter);
  
          ImageButton add = (ImageButton)findViewById(R.id.addConcertButton);
          add.setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View v)
              {
                  Intent i = new Intent(MainActivity.this, AddArtist.class);
                  startActivity(i);
              }
          });
  
          addData();
      }
  
      @Override
      public void onResume()
      {
          super.onResume();
          addData();
      }
  
      public void addData()
      {
          long spinnerId = spinner1.getSelectedItemId();
          int spinnerInt = (int) spinnerId;
  
          Log.d("test", "Spinner ID = " + spinnerInt);
          switch (spinnerInt)
          {
              case 0:
                  try
                  {
                      db.open();
                      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                              R.layout.row_layout,
                              db.getAllConcertsNameASC(),
                              new String[] { "name", "venue" },
                              new int[] { R.id.listName, R.id.listDate });
  
                      ListView listView = (ListView) findViewById(android.R.id.list);
                      listView.setAdapter(adapter);
  
                  }
  
                  catch (SQLException e)
                  {
                      e.printStackTrace();
                  }
  
                  db.close();
                  break;
  
              case 1:
                  try
                  {
                      db.open();
                      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                              R.layout.row_layout,
                              db.getAllConcertsNameDESC(),
                              new String[] { "name", "venue" },
                              new int[] { R.id.listName, R.id.listDate });
  
                      ListView listView = (ListView) findViewById(android.R.id.list);
                      listView.setAdapter(adapter);
                  }
  
                  catch (SQLException e)
                  {
                      e.printStackTrace();
                  }
  
                  db.close();
                  break;
  
              case 2:
                  try
                  {
                      db.open();
                      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                              R.layout.row_layout,
                              db.getAllConcertsVenueASC(),
                              new String[] { "name", "venue" },
                              new int[] { R.id.listName, R.id.listDate });
  
                      ListView listView = (ListView) findViewById(android.R.id.list);
                      listView.setAdapter(adapter);
                  }
  
                  catch (SQLException e)
                  {
                      e.printStackTrace();
                  }
  
                  db.close();
                  break;
  
              case 3:
                  try
                  {
                      db.open();
                      SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                              R.layout.row_layout,
                              db.getAllConcertsVenueDESC(),
                              new String[] { "name", "venue" },
                              new int[] { R.id.listName, R.id.listDate });
  
                      ListView listView = (ListView) findViewById(android.R.id.list);
                      listView.setAdapter(adapter);
                  }
  
                  catch (SQLException e)
                  {
                      e.printStackTrace();
                  }
  
                  db.close();
                  break;
          }
      }
  
      protected void onListItemClick(ListView l, View v, int position, long id)
      {
          super.onListItemClick(l, v, position, id);
  
          try
          {
              db.open();
  
              Cursor c = db.getConcert(id);
              String string_id = c.getString(0);
              long long_id = Long.parseLong(string_id);
              Log.d("test", "id passed = " + long_id);
  
              Intent i =  new Intent(MainActivity.this, ViewArtist.class);
              i.putExtra("id", long_id);
              startActivity(i);
          }
  
          catch (SQLException e)
          {
              e.printStackTrace();
          }
          db.close();
      }
  }
  </code></pre>
  
  <p>DBManager class:</p>
  
  <pre><code>package com.example.msdproject;
  
  import android.content.ContentValues;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.content.Context;
  
  import java.sql.SQLException;
  
  
  public class DBManager {
  
      public static final String COL_ROWID = "_id";
      public static final String COL_NAME = "name";
      public static final String COL_VENUE = "venue";
      public static final String COL_DATE = "date";
      public static final String COL_COMMENTS = "comments";
  
      private static final String DB_NAME = "Concerts";
      private static final String DB_TABLE = "Concert_Info";
      private static final int DB_VERSION = 1;
  
      private static final String DB_CREATE =
              "create table " + DB_TABLE +
      " (_id integer primary key autoincrement, " +
              "name text not null, " +
              "venue text not null, " +
              "comments text not null, " +
              "date text not null);";
  
      private  Context context;
  
      private DatabaseHelper DBHelper;
      private SQLiteDatabase db;
  
      public DBManager(Context ctx)
      {
          this.context = ctx;
          DBHelper = new DatabaseHelper(context);
      }
  
      public static class DatabaseHelper extends SQLiteOpenHelper
      {
          DatabaseHelper(Context context)
          {
              super(context, DB_NAME, null, DB_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase db)
          {
              db.execSQL(DB_CREATE);
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
          {
              /*
              //taken from YouTube Tutorial video
              Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                      + newVersion + ", which will destroy all old data");
              db.execSQL("drop table if exists Concert_Info");
              onCreate(db);*/
          }
      }
  
      public DBManager (){
      }
  
      public DBManager open() throws SQLException
      {
          db = DBHelper.getWritableDatabase();
          //db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE + ";");
          return this;
      }
  
      public void close()
      {
          DBHelper.close();
      }
  
      public long insertConcert(String name, String venue, String date, String comments)
      {
          ContentValues initialValues = new ContentValues();
          initialValues.put(COL_NAME, name);
          initialValues.put(COL_VENUE, venue);
          initialValues.put(COL_DATE, date);
          initialValues.put(COL_COMMENTS, comments);
          return db.insert(DB_TABLE, null, initialValues);
      }
  
      public boolean deleteConcert(String name)
      {
          return db.delete(DB_TABLE, "name='"+ name+"'", null) &gt; 0;
  
      }
  
      public Cursor getAllConcertsNameASC() {
          return db.query(DB_TABLE, new String[]
                          {
                                  COL_ROWID,
                                  COL_NAME,
                                  COL_VENUE,
                                  COL_DATE,
                                  COL_COMMENTS
                          },
                          null,
                          null,
                          null,
                          null,
                          COL_NAME+ " ASC"
                  );
      }
  
      public Cursor getAllConcertsNameDESC() {
          return db.query(DB_TABLE, new String[]
                          {
                                  COL_ROWID,
                                  COL_NAME,
                                  COL_VENUE,
                                  COL_DATE,
                                  COL_COMMENTS
                          },
                  null,
                  null,
                  null,
                  null,
                  COL_NAME+ " DESC"
          );
      }
  
      public Cursor getAllConcertsVenueASC() {
          return db.query(DB_TABLE, new String[]
                          {
                                  COL_ROWID,
                                  COL_NAME,
                                  COL_VENUE,
                                  COL_DATE,
                                  COL_COMMENTS
                          },
                  null,
                  null,
                  null,
                  null,
                  COL_VENUE+ " ASC"
          );
      }
  
      public Cursor getAllConcertsVenueDESC() {
          return db.query(DB_TABLE, new String[]
                          {
                                  COL_ROWID,
                                  COL_NAME,
                                  COL_VENUE,
                                  COL_DATE,
                                  COL_COMMENTS
                          },
                  null,
                  null,
                  null,
                  null,
                  COL_VENUE+ " DESC"
          );
      }
  
      public Cursor getConcert(long ROW_ID) throws SQLException
      {
          Cursor mCursor =
                  db.query(DB_TABLE, new String[]
                          {
                                  COL_ROWID,
                                  COL_NAME,
                                  COL_VENUE,
                                  COL_DATE,
                                  COL_COMMENTS
                          },
                          COL_ROWID + "=" + ROW_ID,
                          null,
                          null,
                          null,
                          null
                  );
          if (mCursor != null)
          {
              mCursor.moveToFirst();
          }
  
          return mCursor;
      }
  
      public boolean updateConcert(String name, String venue, String date, String comments)
      {
          ContentValues updateValues = new ContentValues();
          updateValues.put(COL_NAME, name);
          updateValues.put(COL_VENUE, venue);
          updateValues.put(COL_DATE, date);
          updateValues.put(COL_COMMENTS, comments);
          return db.update(DB_TABLE, updateValues, "name='"+ name+"'", null) &gt; 0;
      }
  
      public void reset () throws SQLException {
          db.delete(DB_TABLE, null, null);
          db.close();
          this.DBHelper.onCreate(this.db);
      }
  }
  </code></pre>