Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将第一个列表和最后一个列表中的特定值保存到SQLite_Java_Android_Sqlite_Listview_Android Listview - Fatal编程技术网

Java 将第一个列表和最后一个列表中的特定值保存到SQLite

Java 将第一个列表和最后一个列表中的特定值保存到SQLite,java,android,sqlite,listview,android-listview,Java,Android,Sqlite,Listview,Android Listview,我有一个列表视图,如下图所示 单击提交按钮时,我希望第一行的时间,第二行的超时保存到时间表。有可能实现吗 时间表 ------------------------ | PK |Time In |Time Out| | -= |---------|--------| | 1 | 9:0 | 18:0 | | | | | 您可以将时间转换为int,然后保存到sqlite,例如: Store : 14:30 ===> 14 * 60 +

我有一个列表视图,如下图所示

单击提交
按钮
时,我希望第一行的
时间
,第二行的
超时
保存到时间表。有可能实现吗

时间表

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

您可以将时间转换为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){

        }
    }
}

}

您存储的时间格式是什么DB@Clairvoyant我正在使用
timePick对话框
伙计们,请在这里留下您的评论或答案,而不是只给出一个downvots:)将其作为字符串存储在sqlite中,只需将列表中的值作为字符串保存在数据库中我如何知道哪个值在第一个列表或最后一个列表中?因为所有的变量都是相同的,这不是我想要的答案。我想在中保存时间(从第一个列表)和超时(从最后一个列表)。进入数据库。在同一个数据库中有row@John您的问题不清楚。单击“提交”按钮时,您能阅读“时间”吗?我还没有尝试。我想知道如何在我的一张桌子上节省时间你误解了我的问题。listView中的值实际上是从活动B中获取的:)