title: next/back button to retrieve next/previous record in Android
body: <p>Can you give me an idea on how I would be able to fetch next/previous data from database then show it on my textview? I have no idea with what to do.</p>
  
  <p>Main objective: Create a next/back button that will retrieve data from database then show it on textview.</p>
  
  <p>This is my MainActivity.</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
  private DatabaseHelper dbHelper = new DatabaseHelper(MainActivity.this,"MyDatabase2",2);
  
  TextView text;
  
  TextView num;
  TextView user;
  TextView password;
  EditText txt1;
  EditText txt2;
  EditText txt3;
  EditText txt4;
  EditText txt5;
  Button btnAdd;
  Button btnClose;
  Button btnUpdate;
  Button btnDelete;
  Button btnLess;
  Button btnGreat;
  Button btnLesser;
  Button btnGreater;
  
  
  
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
  
      text = (TextView) findViewById(R.id.textView1);
      num = (TextView) findViewById(R.id.textView2);
      user = (TextView) findViewById(R.id.textView3);
      password = (TextView) findViewById(R.id.textView4);
      txt1 = (EditText) findViewById(R.id.editText1);
      txt2 = (EditText) findViewById(R.id.editText2);
      txt3 = (EditText) findViewById(R.id.editText3);
      txt4 = (EditText) findViewById(R.id.editText4);
      txt5 = (EditText) findViewById(R.id.editText5);
      btnAdd = (Button) findViewById(R.id.button1);
      btnClose = (Button) findViewById(R.id.button2);
      btnUpdate = (Button) findViewById(R.id.button3);
      btnDelete = (Button) findViewById(R.id.button4);
      btnLess = (Button) findViewById(R.id.button5);
      btnGreat = (Button) findViewById(R.id.button6);
      btnLesser = (Button) findViewById(R.id.button7);
      btnGreater = (Button) findViewById(R.id.button8);
  
  
  
      SQLiteDatabase db = dbHelper.getWritableDatabase();
      try
      {
  
          db.execSQL("create table User(ID integer, fname varchar(90), lname varchar(90),mname varchar(90),course varchar(90));");
          text.setText("Successful in creating table.");
      }
      catch(Exception e)
      {    
          text.setText("Error in creating table.");
          e.printStackTrace();
      }
  
      btnAdd.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
  
  
              if("".equals(txt2.getText().toString()) || "".equals(txt3.getText().toString()))
                {
                    Toast toast = Toast.makeText(MainActivity.this, 
                            "Something needs to be typed in.", Toast.LENGTH_LONG); 
                  toast.show();
                }
                else
                {
                    long flag = 0;
                    int id = 1;
                    SQLiteDatabase db = dbHelper.getWritableDatabase();
                    Cursor cursor = db.query("User", new String[]{"count(*) ID"}, null, null, null, null, null);
                  while(cursor.moveToNext())
                  {
                      int idFromDatabase = cursor.getInt(cursor.getColumnIndex("ID"));
                      if(idFromDatabase != 0)
                      {
                          id = 1 + idFromDatabase;
                      }
                  }
  
                    ContentValues values = new ContentValues();
                    values.put("ID", Integer.parseInt(txt1.getText().toString().trim()));
                    values.put("lname", txt2.getText().toString().trim());
                    values.put("fname", txt3.getText().toString().trim());
                    values.put("mname", txt4.getText().toString().trim());
                    values.put("course", txt5.getText().toString().trim());
                    flag = db.insert("User", null, values);
                    if(flag != -1)
                    {
                        Toast toast = Toast.makeText(MainActivity.this, "A new record has been added! "
                                , Toast.LENGTH_LONG); 
                      toast.show();
                      db.close();
                      return;
                    }
                    else
                    {
                        Toast toast = Toast.makeText(MainActivity.this, "Something went wrong please check.", 
                                Toast.LENGTH_LONG); 
                      toast.show();
                      db.close();
                      return;
                    }
  
          }
          }
  
      });
  
  
  
  
  
  
      btnUpdate.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
  
              long flag = 0;
              SQLiteDatabase db = dbHelper.getWritableDatabase();
              ContentValues values = new ContentValues();
                 values.put("ID", txt1.getText().toString());
                values.put("lname", txt2.getText().toString());
                values.put("fname", txt3.getText().toString());
                values.put("mname", txt4.getText().toString());
                values.put("course", txt5.getText().toString());
                flag = db.update("User",values,"ID=?",new String[]{txt1.getText().toString()});
  
  
                if(flag != -1)
                {
                    Toast toast = Toast.makeText(MainActivity.this, "Update Successful! ",
                            Toast.LENGTH_LONG); 
                  toast.show();
                  db.close();
                  return;
                }
                else
                {
                    Toast toast = Toast.makeText(MainActivity.this, "Update Failed!",
                            Toast.LENGTH_LONG); 
                  toast.show();
                  db.close();
                  return;
                }    
  
          }
          });
      //--------------------------------------------------
  
      btnDelete.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
  
              int flag = 0;
                 SQLiteDatabase db = dbHelper.getWritableDatabase();
                flag = db.delete("User", "ID=?", new String[]{""+txt1.getText().toString().trim()});
                if(flag != 0)
                {
                    Toast toast = Toast.makeText(MainActivity.this, "The record has been deleted! ",
                            Toast.LENGTH_LONG); 
                  toast.show();
                  db.close();
                  return;
                }
                else
                {
                    Toast toast = Toast.makeText(MainActivity.this, "Failed deleting the record! ",
                            Toast.LENGTH_LONG); 
                  toast.show();
                  db.close();
                  return;
                }
             } });
  
      //--------------------------------------------------
      btnClose.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
  
              finish();
              System.exit(0);
             } });
  
      btnLess.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
              dbHelper info = new dbHelper(this);
              info.open();
              String data = info.getBankBal();
              info.close();
              tv.setText(data);
  
             } });
  
    /*  btnShow.setOnClickListener(new View.OnClickListener()
      {
          public void onClick(View v)
          {
  
              String display = "";
              SQLiteDatabase db = dbHelper.getWritableDatabase();
  
              Cursor cursor = db.query("User", new String[]{"ID","name","pass"},
                      null, null, null, null, null);
  
              if (!cursor.moveToNext())
              {
                  text.setText("No records in the table");
                  db.close();
                  return;
              }
  
  
  
              cursor.moveToPrevious();
  
              while(cursor.moveToNext())
              {
                  int ID = cursor.getInt(cursor.getColumnIndex("ID"));
                  String name = cursor.getString(cursor.getColumnIndex("name"));
                  String password = cursor.getString(cursor.getColumnIndex("pass"));
                  display = display + "\n"+"No."+ID+", Name: "+name+", Password: "+password+"\n";
              }
  
              text.setText(display);
              db.close();
          }
  
  
  
          });*/
  
      //--------------------------------------------------
  
  
  
  
  
  }}
  </code></pre>
  
  <p>And this is my databasehelper</p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper 
  {
  public DatabaseHelper(Context context, String name, CursorFactory factory,
          int version) {
      super(context, name, factory, version);
      // TODO Auto-generated constructor stub
  }
  
  private static final int VERSION = 1;
  
  public DatabaseHelper(Context context,String name)
  {
      this(context,name,VERSION);
  }
  
  public DatabaseHelper(Context context,String name,int version)
  {
      this(context, name,null,version);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) 
  {
      //do nothing
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
  {
      // do nothing
  }
  </code></pre>
  
  <p>}</p>