![](/img/trans.png)
[英]java.lang.IllegalStateException: couldn't move cursor to position n
[英]Move cursor position on every click of button Java
我在 Android 工作室上创建了一个学生管理应用程序,我想在每次单击按钮时将学生的分数输入数据库。
这是我为 cursor 编写的代码,因此 cursor 位于表格的第一行,我希望它在每次单击按钮时移动到下一个 position。
Cursor cursor = DB.DisplayEtudiant(matricule);
cursor.moveToFirst();
while(cursor.moveToNext()){
editMatricule.setText(cursor.getString(0));
editNote.setText(" ");
}
这里的while循环不起作用,我不明白为什么。
您将移至 cursor 的第一行,然后移至下一行。 如果只检索到 1 行,那么您跳过该行并且什么都不做。
如果提取的 cursor 中有多行,那么您实际上是移至最后一行并将 editMatricule 的文本设置为“”。 因此,无论 cursor 中当前 position 的概念如何,您都将始终移动到最后一个而不是下一个。
不可能为您的问题提供解决方案,因为有很多事情需要猜测。 但是,也许考虑以下允许遍历(即不仅是 Next,而且还有 First、Previous 和 Last):-
首先是一个带有 TextView 的简单布局,它显示了学生(Etudiant)和一个 EditText 作为标记。
学生详细信息下方是 4 个按钮 First、Prev、Next 和 Last:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!">
</TextView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/etudiant_name"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="match_parent">
</TextView>
<EditText
android:id="@+id/etudiant_mark"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/first_etudiant"
android:layout_margin="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="FIRST"
>
</Button>
<Button
android:id="@+id/prev_etudiant"
android:layout_margin="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="PREV"
>
</Button>
<Button
android:id="@+id/next_etudiant"
android:layout_margin="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="NEXT"
>
</Button>
<Button
android:id="@+id/last_etudiant"
android:layout_margin="1dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="LAST"
>
</Button>
</LinearLayout>
</LinearLayout>
接下来是 DatabaseHelper DBHelper :-
class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "thedatabase.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME_MATRICULE = "matricule";
public static final String MATRICULE_COL_ID = BaseColumns._ID;
public static final String MATRICULE_COL_MARK = "matricule_MARK";
public static final String MATRICULE_COL_ETUDIANT = "matricule_etudiant";
DBHelper(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_MATRICULE + " ("
+ MATRICULE_COL_ID + " INTEGER PRIMARY KEY,"
+ MATRICULE_COL_ETUDIANT + " TEXT, "
+ MATRICULE_COL_MARK + " INTEGER " +
");"
);
db.execSQL("INSERT OR IGNORE INTO " + TABLE_NAME_MATRICULE
+ "(" + MATRICULE_COL_ETUDIANT + "," + MATRICULE_COL_MARK + ")" +
" VALUES ('Etudiant1',0),('Etudiant2',0),('Etudiant3',0),('Etudiant4',0)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
}
public Cursor DisplayEtudiant() {
return this.getWritableDatabase().query(TABLE_NAME_MATRICULE,null,null,null,null,null,null);
}
}
DisplayEtudiant
再次只是为了演示。最后一个活动MainActivity 。 这使用上面的布局,因此包括 onClick 监听器,用于遍历 cursor 的 4 个按钮,相应地更改显示:-
@SuppressLint("Range")
public class MainActivity extends AppCompatActivity {
DBHelper DB;
Cursor cursor;
int current_matricule_position = 0;
int matricule_count = 0;
TextView etduiant_name;
EditText etudiant_mark;
Button first,prev,next,last;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etduiant_name = this.findViewById(R.id.etudiant_name);
etudiant_mark = this.findViewById(R.id.etudiant_mark);
first = this.findViewById(R.id.first_etudiant);
prev = this.findViewById(R.id.prev_etudiant);
next = this.findViewById(R.id.next_etudiant);
last = this.findViewById(R.id.last_etudiant);
DB = new DBHelper(this);
setOrRefreshCursor();
displayCurrent();
setupButtons();
}
private void setupButtons() {
first.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (matricule_count > 0) {
cursor.moveToFirst();
current_matricule_position = 0;
displayCurrent();
}
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (matricule_count > 0) {
if (current_matricule_position > 0) {
current_matricule_position--;
cursor.moveToPrevious();
displayCurrent();
}
}
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (matricule_count > 0) {
if (current_matricule_position < (matricule_count - 1)) {
current_matricule_position++;
cursor.moveToNext();
displayCurrent();
}
}
}
});
last.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (matricule_count > 0) {
cursor.moveToLast();
current_matricule_position = cursor.getPosition();
displayCurrent();
}
}
});
}
private void displayCurrent() {
etduiant_name.setText(cursor.getString(cursor.getColumnIndex(DBHelper.MATRICULE_COL_ETUDIANT)));
etudiant_mark.setText(cursor.getString(cursor.getColumnIndex(DBHelper.MATRICULE_COL_MARK)));
}
private void setOrRefreshCursor() {
cursor = DB.DisplayEtudiant();
if (current_matricule_position < 0) {
cursor.moveToFirst();
} else {
cursor.moveToPosition(current_matricule_position);
}
current_matricule_position = cursor.getPosition();
matricule_count = cursor.getCount();
}
@Override
protected void onDestroy() {
super.onDestroy();
cursor.close();
}
}
应用程序运行时显示:-
如果应该/可以进行移动,则单击按钮将移动到相应的学习者(例如,当第一个学习者 First 和 Prev 什么都不做时,同样,当最后一个学习者 Last 和 Next 什么都不做时)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.