@SmallTest public void testCardFromCursor() { MatrixCursor cursor = new MatrixCursor(CardsQuery.PROJECTION); cursor.moveToFirst(); Object[] values = new Object[] { 123456789L, "question", "answer", "ftag1 ftag2", "mtag", "model", 2, 13000.0, 25.0, 1.25, 12950.0, }; cursor.addRow(values); cursor.moveToFirst(); HashMap<String, String> card = CardsQuery.newCardFromCursor(cursor); assertEquals("123456789", card.get("id")); assertEquals("question", card.get("question")); assertEquals("answer", card.get("answer")); assertEquals("00", card.get("flags")); assertEquals("ftag1 ftag2 mtag model", card.get("tags")); assertEquals("13000.0", card.get("due")); assertEquals("25.0", card.get("interval")); assertEquals("1.25", card.get("factor")); assertEquals("12950.0", card.get("created")); }
@Override public void onEdit(Exercise exercise, int position) { if (position == -1) { for (Exercise e : workout.getExercises()) { String name = e.getName(); if (name.equals(exercise.getName())) { Toast toast = Toast.makeText(this, "Exercise already exists!", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); return; } } } ArrayList<Integer> positions = adapter.getCursorPositions(); MatrixCursor newcursor = new MatrixCursor(new String[] {"_id", "name"}); // int lastCursorPosition = cursor.getPosition(); // copy over old cursor items to the new one. if (cursor != null) { cursor.moveToFirst(); for (int i = 0; i < positions.size(); i++) { int cursorPosition = positions.get(i); int listPosition = adapter.getListPosition(positions.get(i)); // dont add to new cursor if item has been removed. if (listPosition == DragSortCursorAdapter.REMOVED) continue; cursor.moveToPosition(cursorPosition); String c = cursor.getString(1); // if its not a new exercise, check to see if the current cursor list position mapping // matches the one we are working with. If yes, set c to be the new exercise. if (position >= 0) { if (position == listPosition) { c = exercise.getName(); } } newcursor.newRow().add(listPosition).add(c); } } // add the new row if (position < 0 && exercise != null) { newcursor.newRow().add(newcursor.getCount()).add(exercise.getName()); } ArrayList<Exercise> newExercises = new ArrayList<Exercise>(); newcursor.moveToFirst(); for (int i = 0; i < newcursor.getCount(); i++) { String ename = newcursor.getString(1); Exercise e = workout.getExercise(ename); if (e != null) newExercises.add(e); else if (i == newcursor.getCount() - 1) newExercises.add(exercise); if (!newcursor.moveToNext()) break; } adapter.changeCursor(newcursor); cursor = newcursor; // modify workout to reflect the change workout.setExercises(newExercises); }
/** * If values are passed in, replaces any cached cursor with one containing new values, and then * closes the previously cached one (if any, and if not in use) If values are not passed in, * removes the row from cache If the row was locked, unlock it * * @param id the id of the row * @param values new ContentValues for the row (or null if row should simply be removed) * @param wasLocked whether or not the row was locked; if so, the lock will be removed */ private void unlockImpl(String id, ContentValues values, boolean wasLocked) { Cursor c = get(id); if (c != null) { if (MailActivityEmail.DEBUG && DEBUG_CACHE) { LogUtils.d(mLogTag, "=========== Unlocking cache for: " + id); } if (values != null && !sLockCache) { MatrixCursor cursor = getMatrixCursor(id, mBaseProjection, values); if (cursor != null) { if (MailActivityEmail.DEBUG && DEBUG_CACHE) { LogUtils.d(mLogTag, "=========== Recaching with new values: " + id); } cursor.moveToFirst(); mLruCache.put(id, cursor); } else { mLruCache.remove(id); } } else { mLruCache.remove(id); } // If there are no cursors using the old cached cursor, close it if (!sActiveCursors.contains(c)) { c.close(); } } if (wasLocked) { mLockMap.subtract(id); } }
private void populateChallengesList() { Log.d(TAG, "populating challenger list"); challenges.moveToFirst(); Log.d( TAG, "first challenger: " + challenges.getString(challenges.getColumnIndex(CHALLENGE_SOURCE_HANDLE_COL))); String[] from = {CHALLENGE_SOURCE_HANDLE_COL}; int[] to = {R.id.challenger_text_view}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.challenger_text_view, challenges, from, to); ListView lv = (ListView) findViewById(R.id.view_challenges); lv.setAdapter(adapter); final CursorWrapper c = new CursorWrapper(challenges); lv.setOnItemClickListener( new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { // Move the cursor to the selected item c.moveToPosition(pos); int cid = c.getInt(c.getColumnIndex("_id")); int sourceId = c.getInt(c.getColumnIndex(CHALLENGE_SOURCE_ID_COL)); String sourceHandle = c.getString(c.getColumnIndex(CHALLENGE_SOURCE_HANDLE_COL)); int targetId = c.getInt(c.getColumnIndex(CHALLENGE_TARGET_COL)); challengeDialog(cid, sourceId, sourceHandle, targetId); } }); }
// checks if any exercises have been removed and updates the global list accordingly // also rewrites the workouts file on the device private void checkRemoved() { ArrayList<Integer> positions = adapter.getCursorPositions(); ArrayList<Exercise> newExercises = new ArrayList<Exercise>(); if (cursor != null) { cursor.moveToFirst(); for (int i = 0; i < positions.size(); i++) { int cursorPosition = positions.get(i); cursor.moveToPosition(cursorPosition); String c = cursor.getString(1); newExercises.add(workout.getExercise(c)); } workout.setExercises(newExercises); } }
private static MatrixCursor createCursor(final String[] columns) { final MatrixCursor cursor = new MatrixCursor(columns); cursor.addRow(new String[] {"test"}); cursor.moveToFirst(); return cursor; }