指定したキーの値が存在するかどうか確認

広告

「ContentValues」クラスのオブジェクトに対して指定したキーを持つ値が存在しているかどうかを調べる方法を確認します。「ContentValues」クラスで用意されている「containsKey」メソッドを使います。

Returns true if this object has the named value.

Parameters:
  key  the value to check for
Returns:
  true if the value is present, false otherwise

1番目の引数に指定したキーを持つ値が存在するかどうかを調べます。存在した場合には戻り値に「true」を、存在しない場合には「false」を返します。

具体的には次のように記述します。

ContentValues cv = new ContentValues();
cv.put("name", "orange");
cv.put("price", 100);

if (cp.containsKey("name")){
    cv.remove("name);
}

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。