title: Reading Data from SQLite Database to a new Activity - Android Studio
body: <p>I am creating an android app that has basically 6 inputs (2 from spinners and the rest textviews) and am using an SQLite Database. I have got as far as when the user clicks on a "start button" the data is saved successfully to the database and also displays on the same activity.</p>
  
  <p>The problem that i am asking help for is.. i want to display the saved data, not on the same activity(screen) in which the data is being saved but on a different activity. So, for instance everything is working in terms of storing and displaying the data on screen 1, but how do i retrieve the data again on screen 2?</p>
  
  <p>Here is the code that i have to save the data (DataBaseHelper):</p>
  
  <pre><code> @Override
  public void onCreate(SQLiteDatabase db) {
      db.execSQL("create table " + TABLE_NAME +"  (ID INTEGER PRIMARY KEY AUTOINCREMENT,LINETYPE TEXT,PACKAGETYPE TEXT,QUANTITY TEXT,DURATION TEXT,STARTTIME TEXT,ENDTIME TEXT)");
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int i, int i1) {
      db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
      onCreate(db);
  
  }
  
  public boolean insertData(String linetype, String packagetype, String quantity, String duration, String starttime, String endtime) {
      SQLiteDatabase db = this.getWritableDatabase();
      ContentValues contentValues = new ContentValues();
      contentValues.put(COL_2,linetype);
      contentValues.put(COL_3,packagetype);
      contentValues.put(COL_4,quantity);
      contentValues.put(COL_5,duration);
      contentValues.put(COL_6,starttime);
      contentValues.put(COL_7,endtime);
      long result = db.insert(TABLE_NAME,null,contentValues);
      if(result == -1)
          return false;
      else
          return true;
  }
  
  public Cursor getAllData() {
      SQLiteDatabase db = this.getWritableDatabase();
      Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
      return res;
  }
  </code></pre>
  
  <p>And some code from the activity:</p>
  
  <pre><code>//INSERT DATA TO DATABASE
              boolean isInserted = myDb.insertData(
                      spinnerSelection,
                      spinnerSelection2,
                      q,
                      d,
                      formattedtime,
                      endtimecalc);
  
              if(isInserted == true)
                  Toast.makeText(screen2.this, "Data Inserted Successfully", Toast.LENGTH_LONG).show();
              else
                  Toast.makeText(screen2.this, "Data not Inserted", Toast.LENGTH_LONG).show();
  
          }
      });
  
      nextButton.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View view) {
              Cursor res = myDb.getAllData();
              if (res.getCount() == 0) {
                  //show messages
                  showMessage("Error", "Nothing Found");
                  return;
              }
  
              StringBuffer buffer = new StringBuffer();
              while (res.moveToNext()) {
                  buffer.append("ID: " + res.getString(0)+"\n");
                  buffer.append("LineType: " + res.getString(1)+"\n");
                  buffer.append("PackageType: " + res.getString(2)+"\n");
                  buffer.append("Quantity: " + res.getString(3)+"\n");
                  buffer.append("Duration: " + res.getString(4)+"\n");
                  buffer.append("StartTime: " + res.getString(5)+"\n");
                  buffer.append("EndTime: " + res.getString(6)+"\n\n");
              }
  
              //SHOW ALL DATA
              showMessage("Data", buffer.toString());
  
  
          }
      });
  }
  
  public void showMessage(String title,String Message){
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setCancelable(true);
      builder.setTitle(title);
      builder.setMessage(Message);
      builder.show();
  }
  </code></pre>
  
  <p>So, i now need to display the same information which is being stored from this activity into a new one.</p>
  
  <p>Anyone any thoughts?</p>
  
  <p>ERROR STACK TRACE:</p>
  
  <blockquote>
    <p>10-05 15:54:58.072 5958-5958/com.example.t_fdonnelly.pharmatest E/AndroidRuntime: 
    FATAL EXCEPTION: main
                                                                                      Process: com.example.t_fdonnelly.pharmatest, PID: 5958
                                                                                      android.database.CursorIndexOutOfBoundsException: Index 13 requested, with a size of 13
                                                                                          at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                                                                                          at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                                                                                          at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                                                                                          at com.example.t_fdonnelly.tracker.screen2$4.onClick(screen2.java:196)
                                                                                          at android.view.View.performClick(View.java:5653)
                                                                                          at android.view.View$PerformClick.run(View.java:22509)
                                                                                          at android.os.Handler.handleCallback(Handler.java:751)
                                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                                          at android.app.ActivityThread.main(ActivityThread.java:6144)
                                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)</p>
  </blockquote>
  
  <p>UPDATED CODE: </p>
  
  <pre><code> Cursor res = myDb.getAllData();
              if (res.getCount() == 0) {
                  //show messages
                  showMessage("Error", "Nothing Found");
                  return;
              }
  
              StringBuffer buffer = new StringBuffer();
  
              String getid = res.getString(0);
              String getlt = res.getString(1);
              String getpt = res.getString(2);
              String getqty = res.getString(3);
              String getdur = res.getString(4);
              String getst = res.getString(5);
              String getet = res.getString(6);
  
  
              if ( res != null &amp;&amp; res.moveToFirst()) {
                  do {
                      buffer.append(getid);
                      buffer.append(getlt);
                      buffer.append(getpt);
                      buffer.append(getqty);
                      buffer.append(getdur);
                      buffer.append(getst);
                      buffer.append(getet);
                  } while (res.moveToNext());
              }
  
  
              Intent TransferData = new Intent(getBaseContext(), screen4.class);
              TransferData.putExtra("ID", getid);
              TransferData.putExtra("LineType", getlt);
              TransferData.putExtra("PackageType", getpt);
              TransferData.putExtra("Quantity", getqty);
              TransferData.putExtra("Duration", getdur);
              TransferData.putExtra("Starttime",getst);
              TransferData.putExtra("endtime", getet);
  
              startActivity(TransferData);
          }
      });
  }
  </code></pre>