title: How to filter listView by information from database
body: <p>I have programmed an app that is able to save subjects and marks in a database. The last problem I have to solve is, how I can filter the ListView from the Marks class so that it only shows the according subjects.</p>
  
  <p>The app consists of three classes.</p>
  
  <p>Main:</p>
  
  <pre><code>package maturaarbeit.nicola_pfister.marks;
  
  import maturaarbeit.nicola_pfister.marks.database.DBAdapter;
  import android.app.AlertDialog.Builder;
  import android.app.ListActivity;
  import android.content.DialogInterface;
  import android.content.Intent;
  import android.database.Cursor;
  import android.os.Bundle;
  import android.support.v4.widget.SimpleCursorAdapter;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.EditText;
  import android.widget.ListView;
  
  public class Main extends ListActivity {
  
      DBAdapter db = new DBAdapter(this);
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.subjects);
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          db.close();
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          db.open();
          getData();
      }
  
      @SuppressWarnings("deprecation")
      private void getData() {
          SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                  android.R.layout.simple_list_item_1, 
                  db.getAllSubjects(), 
                  new String[] { "subject" }, 
                  new int[] { android.R.id.text1 });
  
          ListView listView = (ListView) findViewById(android.R.id.list);
          listView.setAdapter(adapter);
          listView.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long id) {
  
              Cursor cursor = db.getSubject(id);
              String subject = null;
              try {
                  subject = cursor.getString(cursor.getColumnIndex("subject"));
              } catch (Exception e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              Intent intent = new Intent(Main.this, Marks.class);
              intent.putExtra("selected", subject);
              startActivity(intent);
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.subjects, menu);
          return true;
      }
  
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case R.id.menu_add:
              Builder builder = new Builder(this);
              final EditText input = new EditText(this);
              builder
                  .setTitle(R.string.dialog_addsubject)
                  .setView(input)
                  .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  
                      public void onClick(DialogInterface dialog, int which) {
                          String value = input.getText().toString();
                          db.insertSubject(value);
                          getData();
                      }
                  })
                  .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  
                      public void onClick(DialogInterface dialog, int which) {
                      }
  
                  })
                  .show();
              break;
          case R.id.menu_delete:
  //          getData();
              break;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>Marks:</p>
  
  <pre><code>package maturaarbeit.nicola_pfister.marks;
  
  import java.util.Calendar;
  
  import maturaarbeit.nicola_pfister.marks.database.DBAdapter;
  import android.app.AlertDialog.Builder;
  import android.app.ListActivity;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.support.v4.widget.SimpleCursorAdapter;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.widget.EditText;
  import android.widget.ListView;
  
  
  public class Marks extends ListActivity {
  
      DBAdapter db = new DBAdapter(this);
  
  
      String selection = null;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.marks);
  
          final Bundle extras = getIntent().getExtras();
          selection = extras.getString("selected");
  
          if (extras!= null) {
              setTitle(selection);
          }
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          db.close();
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          db.open();
          getData();
  
      }
  
      @SuppressWarnings("deprecation")
      private void getData() {
          SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
                  android.R.layout.simple_list_item_1, 
                  db.getAllMarks(), 
                  new String[] { "value" }, 
                  new int[] { android.R.id.text1 });
  
  
          ListView listView = (ListView) findViewById(android.R.id.list);
          listView.setAdapter(adapter);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(R.menu.marks, menu);
          return true;
      }
  
      public boolean onOptionsItemSelected(MenuItem item) {
          switch (item.getItemId()) {
          case R.id.menu_add:
              Builder builder = new Builder(this);
              final EditText input = new EditText(this);
              builder
                  .setTitle(R.string.dialog_addmark)
                  .setView(input)
                  .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  
                      public void onClick(DialogInterface dialog, int which) {
                          String value = input.getText().toString();
  
                          Calendar cal = Calendar.getInstance();
                          int day = cal.get(Calendar.DAY_OF_MONTH);
                          int month = cal.get(Calendar.MONTH) + 1;
                          final String date = day + "." + month + ".";
  
                          db.insertMark(date, selection, value);
                          getData();
                      }
                  })
                  .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
  
                      public void onClick(DialogInterface dialog, int which) {
                      }
  
                  })
                  .show();
              break;
          case R.id.menu_delete:
              break;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>And the DBAdapter:</p>
  
  <pre><code>package maturaarbeit.nicola_pfister.marks.database;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.SQLException;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class DBAdapter {
  
      int id = 0;
      public static final String KEY_ROWID = "_id";
      public static final String KEY_DATE = "date";
      public static final String KEY_SUBJECT = "subject";
      public static final String KEY_VALUE = "value";
      public static final String TAG = "DBAdapter";
  
      private static final String DATABASE_NAME = "database";
      private static final String DATABASE_TABLE_MARKS = "marks";
      private static final String DATABASE_TABLE_SUBJECTS = "subjects";
      private static final int DATABASE_VERSION = 1;
  
      private static final String DATABASE_CREATE_MARKS =
              "create table " + DATABASE_TABLE_MARKS + " (" + KEY_ROWID + " integer primary key autoincrement, " +
                      KEY_DATE +" text not null, " +
                      KEY_SUBJECT +" text not null, " +
                      KEY_VALUE +" text not null" +  ");";
  
      private static final String DATABASE_CREATE_SUBJECTS =
              "create table " + DATABASE_TABLE_SUBJECTS + " (" + KEY_ROWID + " integer primary key autoincrement, " +
                      KEY_SUBJECT +" text not null " + ");";
  
      @SuppressWarnings("unused")
      private final Context context;
  
      private DatabaseHelper DBHelper;
      private SQLiteDatabase db;
  
      public DBAdapter(Context context) {
          this.context = context;
          DBHelper = new DatabaseHelper(context);
      }
  
      private static class DatabaseHelper extends SQLiteOpenHelper {
  
          DatabaseHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              db.execSQL(DATABASE_CREATE_MARKS);
              db.execSQL(DATABASE_CREATE_SUBJECTS);
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_MARKS);
              db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE_SUBJECTS);
              onCreate(db);
          }
      }
  
      public DBAdapter open() throws SQLException {
          db = DBHelper.getWritableDatabase();
          return this;
      }
  
      public void close() {
          DBHelper.close();
      }
  
      public long insertMark (String date, String subject, String value) {
          ContentValues initialValues = new ContentValues();
          initialValues.put(KEY_DATE, date);
          initialValues.put(KEY_SUBJECT, subject);
          initialValues.put(KEY_VALUE, value);
          return db.insert(DATABASE_TABLE_MARKS, null, initialValues);
      }
  
      public boolean deleteMark(long rowId) {
          return db.delete(DATABASE_TABLE_MARKS, KEY_ROWID + "=" + rowId, null) &gt; 0;
      }
  
      public Cursor getAllMarks() {
          return db.query(DATABASE_TABLE_MARKS, new String[] {
                  KEY_ROWID,
                  KEY_DATE,
                  KEY_SUBJECT,
                  KEY_VALUE
          }, null, null, null, null, null);
      }
  
      public Cursor getMark(long rowId) throws SQLException {
          Cursor mCursor =
                  db.query(true, DATABASE_TABLE_MARKS, new String[] {
                          KEY_ROWID,
                          KEY_DATE,
                          KEY_SUBJECT,
                          KEY_VALUE
                  },
                  KEY_ROWID + "=" + rowId,
                  null, null, null, null, null);
          if (mCursor != null) {
              mCursor.moveToFirst();
          }
          return mCursor;
      }
  
      public boolean updateMark(long rowId, String date, String subject, String value) {
          ContentValues args = new ContentValues();
          args.put(KEY_DATE, date);
          args.put(KEY_SUBJECT, subject);
          args.put(KEY_VALUE, value);
          return db.update(DATABASE_TABLE_MARKS, args, KEY_ROWID + "=" + rowId, null) &gt;0;
      }
  
      public void deleteMark() {
          db.delete(DATABASE_TABLE_MARKS, null, null);
      }
  
      public long insertSubject (String subject) {
          ContentValues initialValues = new ContentValues();
          initialValues.put(KEY_SUBJECT, subject);
          return db.insert(DATABASE_TABLE_SUBJECTS, null, initialValues);
      }
  
      public boolean updateSubject(long rowId, String subject) {
          ContentValues args = new ContentValues();
          args.put(KEY_SUBJECT, subject);
          return db.update(DATABASE_TABLE_MARKS, args, KEY_ROWID + "=" + rowId, null) &gt;0;
      }
  
      public Cursor getSubject(long rowId) throws SQLException {
          Cursor mCursor =
                  db.query(true, DATABASE_TABLE_SUBJECTS, new String[] {
                          KEY_ROWID,
                          KEY_SUBJECT
                  },
                  KEY_ROWID + "=" + rowId,
                  null, null, null, null, null);
          if (mCursor != null) {
              mCursor.moveToFirst();
          }
          return mCursor;
      }
  
      public Cursor getAllSubjects() {
          return db.query(DATABASE_TABLE_SUBJECTS, new String[] {
                  KEY_ROWID,
                  KEY_SUBJECT
          }, null, null, null, null, null);
      }
  }
  </code></pre>
  
  <p>As it is now, the Marks Activity shows all Marks of any subject even if the title says something else. I'd like to filter the ListView using the "subject" column of my database so that only the items of the database get displayed, which have the same subject as the title says. Im starting to get overwhelmed by all the different solutions I find on the internet and I'm not able to adapt any of them to my problem...</p>
  
  <p>Thank you in advance</p>