下一个/后退按钮,用于检索Android中的下一个/上一个记录

时间:2014-05-08 06:59:46

标签: java android sqlite

您能告诉我如何从数据库中获取下一个/上一个数据然后在我的textview上显示它吗?我不知道该怎么做。

主要目标:创建一个下一个/后退按钮,它将从数据库中检索数据,然后在textview上显示它。

这是我的MainActivity。

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();
        }



        });*/

    //--------------------------------------------------





}}

这是我的数据库帮助

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
}

}

1 个答案:

答案 0 :(得分:1)

可以通过多种方式实现这一目标。其中一种方法是:

在表格中添加自动增量参加者ID列

示例

sql = "CREATE TABLE " + SMS_FAILED_TABLE + " ("
                + ID_COLUMN + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + SMS_FAILED_TABLE_PHONENOS_COLUMN + " TEXT , "
                + _SMS_FAILED_TABLE_MSG_COLUMN + " TEXT );";

我们现在可以使用它来实现next和prev功能

<强>步骤

1)  sql =从表中选择*;

现在使用返回的光标并移动cursor.movetofirst();

2)光标移动到第一个后,只需使用光标获取该行的ID_COLUMN值。

3)现在你有了ID,只需增加和减少下一个和上一个

的ID

现在使用此sql= select * from table where ID_COLUMN=value;