android - 在 ViewPager 位置从 SQLite 表设置 EditText

标签 android database sqlite android-viewpager

所以我有一个带有保存按钮的编辑文本。当您输入文本并按保存时,文本保存在 COLUMN_ROUTENOTE 中,位置保存在 COLUMN_ROUTENOTEPOSITION 中,现在我希望当您关闭应用程序并返回到 viewpager 中的某个位置时,edittext 会填充您的路线注释以前保存在那个位置。我是编程新手,不确定如何完成此操作。 `

这是我的 viewpager,带有将值添加到数据库的保存按钮,我需要在其中设置编辑文本和我的 viewpager 位置。

      saveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            RouteNote routeNote = new RouteNote();
            routeNote.set_routeNote(noteEditText.getText().toString());
            dbHandler.addRouteNote(routeNote);

            RouteNote routeNotePosition = new RouteNote();
            routeNotePosition.set_routeNotePosition(position);
            dbHandler.addRouteNotePosition(routeNotePosition);
        }});

    Cursor n = db.rawQuery("SELECT routeNotePosition FROM checked_routes", null);
    db.rawQuery("SELECT routeNote FROM checked_routes", null);

      n.moveToPosition(position);
      n.getString(position);

    noteEditText.setText(???);

这是我的数据库处理程序,我在其中添加了路由注释和位置

/////Add route note row to table
public void addRouteNote(RouteNote routeNote){
    ContentValues values = new ContentValues();
    values.put(COLUMN_NOTE, routeNote.get_routeNote());
    SQLiteDatabase db = getWritableDatabase();
    db.insertWithOnConflict(TABLE_CHECKED_ROUTES , null, values, SQLiteDatabase.CONFLICT_IGNORE);
    db.close();
}
/////Add route note position to table
public void addRouteNotePosition(RouteNote routeNotePosition){
    ContentValues values = new ContentValues();
    values.put(COLUMN_ROUTENOTEPOSITION, routeNotePosition.get_routeNotePosition());
    SQLiteDatabase db = getWritableDatabase();
    db.insertWithOnConflict(TABLE_CHECKED_ROUTES , null, values, SQLiteDatabase.CONFLICT_IGNORE);
    db.close();
}

这是我的 routeNote 类

public class RouteNote {

private String routeNote;
private int routeNotePosition;

public void set_routeNote(String routeNote){ this.routeNote = routeNote;
}

public String get_routeNote(){
    return routeNote;
}

 public void set_routeNotePosition(int routeNotePosition){ this.routeNotePosition = routeNotePosition;
}

 public int get_routeNotePosition(){
    return routeNotePosition;
}
}

谢谢你的帮助

编辑:仍在寻求帮助谢谢!

工作:

        Cursor n = db.rawQuery(" SELECT routeNote FROM route_note WHERE routeNotePosition = " + Integer.toString(position),null);

    if (n.getCount() >= 1) {
        n.moveToFirst();
        String note = n.getString(n.getColumnIndex("routeNote"));
        noteEditText.setText(note);
        n.close();
    }

最佳答案

假设(同一)行包含 routeNotePositionrouteNote 那么:-

Cursor n = db.rawQuery(" SELECT routeNotePosition, routeNote FROM checked_routes", null);

String rnp = n.getString(0); // get the routenoteposition
noteEditText.setText(n.getString(1)); //Set the EditText

如果 routeNotePosition 持有 RouteNote 的位置并且它是一个整数并且还假设所需的 roueNotePosition 在第一行(否则在设置 rnp)

之前为光标 n 添加 moveToPosition

Note!! 将第 3 行代码更改为使用 SELECT routeNote .... 而不是 SELECT roueNote(即缺少 t)根据相关评论。


Cursor n = db.rawQuery(" SELECT routeNotePosition FROM checked_routes",null);
Cursor x = db.rawQuery(" SELECT routeNote FROM checked_routes",null);

int rnp = n.getInt(n.getColumnIndex("routeNotePosition"),-1); // get routeNotesPosition

if ((x.getCount() > rnp) || rnp < 0) {
    x.moveToPosition(rnp);
    noteEditText.setText(x.getString(getColumnIndex(routeNote)));
} else {
    // handle invalid position i.e. greater than rows in cursor
}

注意 getColumnIndex(column_name_as_string) 根据列名获取列在游标中的位置。使用特定位置可能会导致问题和/或需要更多维护。

这是在做什么:-

  • a) 创建 2 个游标,每个游标只有 1 列(就像你 做了,除了你通过不分配它来丢弃第二个游标 到一个变量。所以在 a 之后有 2 个游标(nx)。
  • b) 从第一个游标中提取 routeNotesPosition 到一个 称为 rnp 的整数变量(我假设这是位置,即 包含关联 roueNotes 的行)(int rnp = ......)。 -1 getInt 是默认值,在无法返回 Int 时使用(因此下一步也会检查这种情况)。
  • c) 检查 rnp 以确保第二个游标至少有 那么多行(注意位置是一个偏移量,所以第一行是 0,而 count 返回行数,因此如果有任何行,则从 1 开始 存在)。 (if (x.getCount() > rnp)。
  • d) 光标 x 移动到根据 rnp 的位置。
  • e) EditText 根据保存在 routeNotes 列。

我看不出您使用的以下代码是否有效。您是说将光标移动到第 x 行(位置),然后从最偏移 x 的列中获取数据。它只有在 x 为 0 时才有效。如果位置为 1,那么它将失败,因为游标 n 中没有偏移量 1(它只有 1 列,偏移量为 0,所以 n.getString(position) 将失败,因为 SELECT 仅指定 1 列)。

  n.moveToPosition(position);
  n.getString(position);

我的猜测是,您正在寻找与第二个示例类似的内容。但是,也许将 routeNotePositionrouteNote 结合起来会更有优势。

请注意,所提供的代码是即时编写的,因此其中可能存在错误。它更像是一个指南。


根据评论添加。 对第二个示例的以下更改应该处理获取正确的 roueNotePosition 行:-

Cursor n = db.rawQuery(" SELECT routeNotePosition FROM checked_routes WHERE routeNotePosition = " + Integer.toString(position),null);

其中 position 是包含 ViewPager 位置的整数。相同的更改也可以应用于第一个示例。

关于android - 在 ViewPager 位置从 SQLite 表设置 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41943108/

相关文章:

android - Activity 完成()中的问题以及在android中按下后退按钮

Android View 比较两个 View 是否相等

database - 无法使用 Play Framework 2.2.2 连接到远程 Heroku Postgres 数据库

c# - 使用 C# 从 SQL Server 数据库显示 PDF

java - 相同类型的嵌套关系

android - 表格行扩展到 View 之外,因为图像无法缩放

java - 选择随机数以选择数组索引时应用程序崩溃

database - 如果我超过了 Heroku 上的数据库大小限额,会发生什么情况?

android - 按项目过滤 SQLite 数据库 "popularity"- Android

java - Android SQLite 并发(尝试重新打开已关闭的连接)