假设用户在不同的行上提供输入,并且应该以不同的方式处理每一行。例如,如果我想计算给定输入的每个用户的字符数。比如,如果用户在不同的行上输入:
this is first line
this is second line
this is third line
如何将每一行作为新输入并将其存储在某处,然后对每个输入执行一些操作(计算字符数)。
答案 0 :(得分:0)
public class MatchingDatabaseAdapter {
...
...
...
public static void addKeypoints(MatOfKeyPoint keyPoints, Context context) {
float[] data = new float[(int)keyPoints.total() * keyPoints.channels()]; // make a spot to save the data
keyPoints.get(0,0,data); // load the data;
ByteBuffer buffer = ByteBuffer.allocate(data.length * 4);
for (int i = 0; i < data.length; i++){
buffer.putFloat(data[i]);
}
byte[] byteArray = buffer.array();
addBlob(byteArray, keyPoints.rows(), keyPoints.cols(), keyPoints.type(), context);
}
public static void addBlob(byte[] blob, int rows, int columns, int mattype, Context context) throws SQLException {
if (mDb == null) mDb = openDb(context);
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.BLOB_FIELD_NAME, blob);
contentValues.put(DatabaseHelper.ROWS_FIELD_NAME, rows);
contentValues.put(DatabaseHelper.COLUMNS_FIELD_NAME, columns);
contentValues.put(DatabaseHelper.MATTYPE_FIELD_NAME, mattype);
long x = mDb.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
Log.v(TAG, "insert said " + x + " and blob was " + blob.length + " long.");
closeDb();
}
public static Cursor getAllRecordsCursor(Context context) {
if (mDb == null || !mDb.isOpen()) mDb = openDb(context);
mCursor = mDb.query(DatabaseHelper.TABLE_NAME, null, null, null, null,null, null);
boolean hasRecords = mCursor.moveToFirst();
Log.v(TAG, "MatchingDatabaseAdapter.getAllRecordsCursor() cursor created. " + mCursor + " and " + (hasRecords?"has records":"has NO RECORDS"));
return mCursor;
}
public static MatOfKeyPoint getKeypointsFromNextCursorPosition() {
MatOfKeyPoint keyPoints = null;
if (mCursor != null) {
Log.v(TAG, "mCursor has " + mCursor.getCount());
mCursor.moveToFirst();
int rows = mCursor.getInt(DatabaseHelper.ROWS_FIELD_POSITION);
int columns = mCursor.getInt(DatabaseHelper.COLUMNS_FIELD_POSITION);
int mattype = mCursor.getInt(DatabaseHelper.MATTYPE_FIELD_POSITION);
keyPoints = new MatOfKeyPoint();
keyPoints.create(rows, columns, mattype);
byte[] blob = mCursor.getBlob(DatabaseHelper.BLOB_FIELD_POSITION);
ByteBuffer buffer = ByteBuffer.wrap(blob);
FloatBuffer floatBuffer = buffer.asFloatBuffer();
float[] floatArray = new float[floatBuffer.limit()];
floatBuffer.get(floatArray);
keyPoints.put(0, 0, floatArray);
}
return keyPoints;
}
// INNER CLASS DatabaseHelper
static class DatabaseHelper extends SQLiteOpenHelper {
private static DatabaseHelper mDatabaseHelper;
private static final String DB_NAME = "blobDb";
private static final String TABLE_NAME = "blobTable";
private static final String ROWS_FIELD_NAME = "rowsField";
public static final int ROWS_FIELD_POSITION = 1;
private static final String COLUMNS_FIELD_NAME = "columnsField";
public static final int COLUMNS_FIELD_POSITION = 2;
private static final String MATTYPE_FIELD_NAME = "mattypeField";
public static final int MATTYPE_FIELD_POSITION = 3;
private static final String BLOB_FIELD_NAME = "blobField";
private static final int BLOB_FIELD_POSITION = 4;
private static final java.lang.String CREATE_TABLE_SQL =
"CREATE TABLE " + TABLE_NAME + " ("
+ "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ ROWS_FIELD_NAME + " INTEGER, "
+ COLUMNS_FIELD_NAME + " INTEGER, "
+ MATTYPE_FIELD_NAME + " INTEGER, "
+ BLOB_FIELD_NAME + " BLOB"
+ ");";
private DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
mDatabaseHelper = this;
}
static synchronized DatabaseHelper getInstance(Context context) {
if (mDatabaseHelper == null) {
mDatabaseHelper = new DatabaseHelper(context.getApplicationContext());
}
return mDatabaseHelper;
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v(TAG, "Creating table: " + CREATE_TABLE_SQL);
db.execSQL(CREATE_TABLE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.v(TAG, "onUpgrade() from " + oldVersion + " to " + newVersion);
Log.v(TAG, "ALL DATA BEING REMOVED FROM THE DATABASE!!");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
onCreate(db);
}
}
}
尝试上面的代码段,希望这有帮助