gpt4 book ai didi

java - Android 中的下一个/后退按钮可检索下一个/上一个记录

转载 作者:行者123 更新时间:2023-12-01 13:00:41 24 4
gpt4 key购买 nike

你能给我一个关于如何从数据库获取下一个/上一个数据然后将其显示在我的 TextView 上的想法吗?我不知道该怎么办。

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

这是我的主要 Activity 。

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
}

}

最佳答案

有很多方法可以实现这一目标。这样做的方法之一是:

在表格中添加自动增量 interget 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 );";

我们现在可以使用它来实现下一个和上一个功能

步骤

1) sql=从表中选择*;

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

2) 游标移至第一行后,只需使用游标获取该行的 ID_COLUMN 值。

3) 现在您已经有了 ID,只需递增和递减下一个和上一个的 ID

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

关于java - Android 中的下一个/后退按钮可检索下一个/上一个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23534908/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com