【问题标题】:Save specific value in first list and last list to SQLite将第一个列表和最后一个列表中的特定值保存到 SQLite
【发布时间】:2016-03-05 00:28:40
【问题描述】:

我有一个如下图所示的 listView。

点击提交button时,我希望将第一行的Time In和第二行的Time Out保存到时间表中。有可能达到吗?

时间表

------------------------
| PK |Time In  |Time Out|
| -= |---------|--------|
|  1 |  9:0    | 18:0   |
|    |         |        |

【问题讨论】:

  • 您在数据库中存储的时间格式是什么
  • @Clairvoyant 我正在使用timePick dialog
  • 各位,请在此处留下您的评论或回答,而不是只投反对票 :)
  • 将它作为字符串存储在sqlite中,只需从列表中获取值作为字符串并将其保存在db中
  • 我如何知道哪个值在第一个列表中或最后一个列表中?由于所有变量都相同

标签: java android sqlite listview android-listview


【解决方案1】:

您可以将时间转换为int,然后保存到sqlite,例如:

Store : 14:30  ===> 14 * 60 + 30 ===> 870

Read : 870 ===> hour= 870 / 60; minute = 870 % 60; ===> String time=hour+":"+minute;

数据库:

public class Database {

private TimesHelper mHelper;
private SQLiteDatabase mDatabase;

public Database(Context context) {

    this.mHelper = new TimesHelper(context);
    this.mDatabase = this.mHelper.getWritableDatabase();
}

public void insertTimes(int timeIn, int timeOut){

    String sql = "INSERT INTO TimeTable VALUES (?,?,?);";

    SQLiteStatement statement = this.mDatabase.compileStatement(sql);
    this.mDatabase.beginTransaction();

    statement.clearBindings();

    statement.bindString(2, timeIn);
    statement.bindLong(3, timeOut);     

    statement.execute();        

    this.mDatabase.setTransactionSuccessful();
    this.mDatabase.endTransaction();
}

public String getAllTimeTable(){

    //get a list of columns to be retrieved, we need all of them
    String[] columns = {
            "ID",
            "TimeIn",
            "TimeOut"                
    };

    Cursor cursor = this.mDatabase.query("TimeTable", columns, null, null, null, null, null);

    String result = "";

    if(cursor != null && cursor.moveToFirst()){

        do{

            int timeIn = cursor.getInt(cursor.getColumnIndex("TimeIn"));  
            int timeOut = cursor.getInt(cursor.getColumnIndex("TimeOut"));

            result = (timeIn / 60) + ":" + (timeOut % 60);
        }
        while (cursor.moveToNext());
    }

    return result;
}

private static class TimesHelper extends SQLiteOpenHelper{

    private Context mContext;
    private static final String DB_NAME= "db";
    private static final int DB_VERSION= 1;        

    private static final String CREATE_TABLE_TIME_TABLE= "CREATE TABLE TimeTable (ID INTEGER PRIMARY KEY AUTOINCREMENT,TimeIn INTEGER,TimeOut INTEGER);";


    public TimesHelper(Context context) {

        super(context, DB_NAME, null, DB_VERSION);
        mContext = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        try {

            db.execSQL(CREATE_TABLE_TIME_TABLE);                
        }
        catch (SQLiteException exception){

        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        try {

            db.execSQL("DROP TABLE TimeTable IF EXISTS;");
            onCreate(db);
        }
        catch (SQLiteException exception){

        }
    }
}

}

【讨论】:

  • 这不是我想要的答案。我想保存Time In(来自第一个列表)和Time Out(from last list)。进入数据库。
  • 同一行有
  • @John 你的问题不清楚。点击提交按钮时你能读懂“Time In”吗?
  • 我还没试过。我想知道如何将 In 和 Time Out 的时间保存到我的一张桌子上
  • 你误解了我的问题。 listView 中的值实际上是从 Activity B 获取的 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 2011-12-08
  • 2018-06-02
相关资源
最近更新 更多