在ViewPager位置从SQLite Table设置EditText

时间:2017-01-30 18:37:27

标签: android database sqlite android-viewpager

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

使用保存按钮继承我的viewpager,该按钮将值添加到数据库以及我需要设置edittext和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();
    }

1 个答案:

答案 0 :(得分:1)

假设(同一)行同时包含 routeNotePosition routeNote ,则: -

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

注意!! 更改了第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列(就像你一样) 做了,除了你没有分配它丢弃第二个光标 变量。所以在有2个游标后( n x )。
  • 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行( position ),然后从offest x处的列中获取数据。它只有在x为0时才有效。如果position为1,那么它会失败,因为游标n中没有偏移1(它只有1列偏移0,所以n.getString(position)将失败{ {1}}仅指定1列。

SELECT

我的猜测是,你正在寻找第二个例子的内容。但是,将 routeNotePosition routeNote 结合起来可能会更有利。

请注意,提供的代码是动态写入的,因此可能会有错误。它更多的是作为指南。

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

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

其中Cursor n = db.rawQuery(" SELECT routeNotePosition FROM checked_routes WHERE routeNotePosition = " + Integer.toString(position),null); 是包含position位置的整数。同样的变化也可以应用于第一个例子。