Reads a String out of a column in a Cursor and writes it to a ContentValues. - Android Database

Android examples for Database:Cursor Column

Description

Reads a String out of a column in a Cursor and writes it to a ContentValues.

Demo Code

/*/*from   ww w.  j a  va  2s.com*/
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.content.ContentValues;
import android.database.Cursor;

public class Main {
  /**
   * Reads a String out of a column in a Cursor and writes it to a
   * ContentValues. Adds nothing to the ContentValues if the column isn't
   * present or if its value is null.
   *
   * @param cursor
   *          The cursor to read from
   * @param column
   *          The column to read
   * @param values
   *          The {@link ContentValues} to put the value into
   */
  public static void cursorStringToContentValuesIfPresent(Cursor cursor,
      ContentValues values, String column) {
    final int index = cursor.getColumnIndex(column);
    if (index != -1 && !cursor.isNull(index)) {
      values.put(column, cursor.getString(index));
    }
  }
}

Related Tutorials