title: ListView, cursors and null sets
body: <p>I will go straight to the point. I have 1 class where I have 6 buttons. Each button saves to an SQLiteDatabase some <strong>parameters</strong> and then it launches an Activity. </p>
  
  <p>The new Activity takes the <strong>parameters</strong> and queries the database to pull data accordingly. When the activity launches I clear the <strong>parameters</strong> in order to save them again if I press another or the same button.</p>
  
  <p>If 1 table(which is linked to the button) is empty it returns the message I want. The problem is that if 1 is empty then all the tables return the message even if they have data!!</p>
  
  <p>My 1st class</p>
  
  <pre><code>public class HRecords extends Activity {
   SQLiteDatabase myDB=null;
   Button tes,con,al,me,pr,va;
  
  protected void onCreate(Bundle savedInstanceState){
       super.onCreate(savedInstanceState);
       setContentView(R.layout.records);
       tes=(Button) findViewById(R.id.testbut);
       con=(Button) findViewById(R.id.condbut);
       al=(Button) findViewById(R.id.albut);
       me=(Button) findViewById(R.id.medbut);
       pr=(Button) findViewById(R.id.procbut);
       va=(Button) findViewById(R.id.vacbut);
  
       Database openHelper = new Database(this);//create new Database to take advantage of the SQLiteOpenHelper class
       myDB = openHelper.getWritableDatabase(); // or getWritableDatabase();
       myDB=SQLiteDatabase.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READWRITE);//set myDB to aeglea
  
       doclicks();
  
  }
  
  private void doclicks(){
       tes.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
                  ContentValues values = new ContentValues();
                  values.put("ton", "Tests");
                  values.put("tazle","user_test");
                  myDB.insert("history_go",null, values);
                  //create new intent
                  Intent record = new Intent(getApplicationContext(), Record.class);
                  // Close all views before launching logged
                  record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(record);
                  // Close Login Screen
                  onPause();
              }});
       con.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
                  ContentValues values = new ContentValues();
                  values.put("ton", "Medical Conditions");
                  values.put("tazle","user_cond");
                  myDB.insert("history_go",null, values);
                  //create new intent
                  Intent record = new Intent(getApplicationContext(), Record.class);
                  // Close all views before launching logged
                  record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(record);
                  // Close Login Screen
                  onPause();
              }});
       al.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
                  ContentValues values = new ContentValues();
                  values.put("ton", "Allergies");
                  values.put("tazle","user_all");
  
                  myDB.insert("history_go",null, values);
                  //create new intent
                  Intent record = new Intent(getApplicationContext(), Record.class);
                  // Close all views before launching logged
                  record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(record);
                  // Close Login Screen
                  onPause();
              }});
       me.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
                  ContentValues values = new ContentValues();
                  values.put("ton", "Medication");
                  values.put("tazle","user_med");
  
                  myDB.insert("history_go",null, values);
                  //create new intent
                  Intent record = new Intent(getApplicationContext(), Record.class);
                  // Close all views before launching logged
                  record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(record);
                  // Close Login Screen
                  onPause();
              }});
       pr.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
                  ContentValues values = new ContentValues();
                  values.put("ton", "Medical Procedures");
                  values.put("tazle","user_proc");
  
                  myDB.insert("history_go",null, values);
                  //create new intent
                  Intent record = new Intent(getApplicationContext(), Record.class);
                  // Close all views before launching logged
                  record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(record);
                  // Close Login Screen
                  onPause();
              }});
       va.setOnClickListener(new View.OnClickListener() {
              public void onClick(View arg0) {
                  ContentValues values = new ContentValues();
                  values.put("ton", "Vaccinations");
                  values.put("tazle","user_vacc");
  
                  myDB.insert("history_go",null, values);
                  //create new intent
                  Intent record = new Intent(getApplicationContext(), Record.class);
                  // Close all views before launching logged
                  record.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(record);
                  // Close Login Screen
                  onPause();
              }});
  }
  
  }
  </code></pre>
  
  <p>The second class which as you can see it pulls data from the SQLite database(you can also see the message for empty results "Nothing Added here. Go to the site to add more.")</p>
  
  <pre><code>public class Record extends Activity{
  SQLiteDatabase myDB=null;
  TextView title=null;
  Cursor cur,cur2=null;
  ListView list=null;
   private ArrayAdapter&lt;String&gt; listAdapter ;  
  
  protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.record);
      title = (TextView) findViewById(R.id.recordTitle);
      list = (ListView) findViewById(R.id.listView1);
  
      Database openHelper = new Database(this);
      myDB = openHelper.getReadableDatabase(); 
      myDB=SQLiteDatabase.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READONLY);
  
      Database db = new Database(getApplicationContext());
      cur = fetchOption("SELECT * FROM history_go");
      title.setText(cur.getString(cur.getColumnIndex("ton")));
  
  
      ArrayList&lt;String&gt; itemlist = new ArrayList&lt;String&gt;();  
      String[] names=null;
      //do query
      cur2=fetchOption("SELECT * FROM "+cur.getString(cur.getColumnIndex("tazle")));
      //check for results
      if (cur2.getCount()==0) {
          names = new String[] { "Nothing Added here. Go to the site to add more."}; 
      }else{
          names = new String[] {cur2.getString(cur2.getColumnIndex("name"))};
  
      }
       //add the array as list to the ArrayList
       itemlist.addAll( Arrays.asList(names) ); 
       listAdapter = new ArrayAdapter&lt;String&gt;(this, R.layout.item, itemlist); 
       //if results add the rest
       if(cur2.getCount()!=0){
           for(int i=0;i&lt;(cur2.getCount()-1);i++){
              cur2.moveToNext();
              listAdapter.add(cur2.getString(cur2.getColumnIndex("name"))); 
           }
       }
       // Set the ArrayAdapter as the ListView's adapter.  
       list.setAdapter(listAdapter); 
       //remove the navigation history
       db.resetHistoryNavigation();
       cur.close();
       cur2.close();
  }
  
  
  
  public Cursor fetchOption(String query) throws SQLException {
      Cursor mCursor = myDB.rawQuery(query, null);
      if (mCursor != null) {
          mCursor.moveToFirst();
      }
      return mCursor;
  }
  
  }
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Also, I forgot to mention that if I set the verification conditions as  cur2 == null the application crushes because the cursor goes out of bounds(the for loop fires)</p>