キーと値を追加

広告
facebookボタン
googleplusボタン
twitterボタン
ダミーボタン
bloggerボタン

「ContentValues」クラスのオブジェクトには複数のキーと値のペアを追加する事が出来ます。カラムにはカラム名を指定しますが、カラムは様々なデータ型がありますので各データ型に対応したメソッドが用意されています。「ContentValues」クラスで用意されている「put」メソッドを使います。

Integer:

put
public void put(String key, Integer value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Short:

put
public void put(String key, Short value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Byte:

put
public void put(String key, Byte value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

String:

put
public void put(String key, String value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Long:

put
public void put(String key, Long value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Boolean:

put
public void put(String key, Boolean value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Float:

put
public void put(String key, Float value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

Double:

put
public void put(String key, Double value)
Adds a value to the set.

Parameters:
  key  the name of the value to put
  value  the data for the value to put

各メソッドの1番目の引数にはカラム名を表すStringクラスの値を指定します。そして2番目の引数にはカラムに設定されているデータ型に対応したクラスの値を指定します。

「ContentValues」クラスのオブジェクトには複数のペアを追加できます。例えばテーブルにカラムが2つ含まれていて、テーブルにデータを追加したい場合にはカラムの数だけ「put」メソッドを実行しておきます。

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

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

( Written by T.buzz.Ikura+ )

Facebook Page