嘿,我试图构建一个应用程序,在其中将值插入数据库,并尝试使用4种方法检索记录。
方法:movetoFirst(), moveToLast(), moveToNext(), moveToPrevious()
。
方法1和2正确执行。但是我正面临下一个和上一个方法的问题。以下是我为以上查询附加的代码。
宣言。
package com.Rohit.taskforsqllite;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText e1, e2, e3;
Cursor c;
SQLiteDatabase db;
int count;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText1);
e2 = (EditText) findViewById(R.id.editText2);
e3 = (EditText) findViewById(R.id.editText3);
text = (TextView) findViewById(R.id.textviewcount);
db = openOrCreateDatabase("Listname", MODE_PRIVATE, null);
db.execSQL("create table if not exists list1(name varchar,location varchar, phone number)");
// Insert values into table
db.execSQL("insert into list1 values('Rohit','London',12)");
db.execSQL("insert into list1 values('Raj','Hyd',23)");
db.execSQL("insert into list1 values('PRak','Canada',34)");
db.execSQL("insert into list1 values('Sanj','Mad',45)");
db.execSQL("insert into list1 values('Anna','Polland',56)");
c = db.rawQuery("select * from list1", null);
count = c.getCount();`import android.app.Activity;
方法1;
Button b1 = (Button) findViewById(R.id.startbutton);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
c.moveToFirst();
String name = c.getString(c.getColumnIndex("name"));
String location = c.getString((c.getColumnIndex("location")));
String phone = c.getString((c.getColumnIndex("phone")));
e1.setText(name);
e2.setText(location);
e3.setText(phone);
Log.d("Record from db", name + "," + location + "," + phone);
}
});
方法2:
Button b2 = (Button) findViewById(R.id.finishbuton);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// c = db.rawQuery("select * from list1", null);
c.moveToLast();
String name = c.getString(0);
String location = c.getString(1);
String phone = c.getString(2);
e1.setText(name);
e2.setText(location);
e3.setText(phone);
Log.d("Record from db", name + "," + location + "," + phone);
}
});
方法3.如果我尝试使用while循环执行,这就是我面临的问题。它要执行到循环结束。但我不想这样做,我想在按上一个按钮时一次显示一条记录。
Button b4 = (Button) findViewById(R.id.previousbutton);
b4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (c.moveToFirstt()) {
do {
String name = c.getString(c.getColumnIndex("name"));
String location = c.getString(c
.getColumnIndex("location"));
String phone = c.getString(c.getColumnIndex("phone"));
e1.setText(name);
e2.setText(location);
e3.setText(phone);
Log.d("Record from db", name + "," + location + ","
+ phone);
} while (c.moveToNext());
}
}
});
最佳答案
如果您使用moveToLast(),则必须移至上一行并使用c.moveToPrevious,否c.moveToNext()