Android-list-view
Androidリストビュー
'_Android ListView は、複数のアイテムをグループ化し、垂直スクロール可能なリストに表示するビューです。 リスト項目は、配列やデータベースなどのソースからコンテンツを取得する Adapter を使用して、リストに自動的に挿入されます。_
リストビュー
アダプタは、UIコンポーネントと、データをUIコンポーネントに入力するデータソースとの間を実際に橋渡しします。 アダプターはデータを保持し、アダプタービューにデータを送信します。ビューはアダプタービューからデータを取得し、スピナー、リストビュー、グリッドビューなどのさまざまなビューにデータを表示できます。
*ListView* および *GridView* は *AdapterView* のサブクラスであり、それらを *Adapter* にバインドすることで移植できます。 *Adapter* は外部ソースからデータを取得し、各データエントリを表すビューを作成します。
Androidには、AdapterViewのさまざまな種類のデータの取得やビューの構築に役立つAdapterのサブクラスがいくつか用意されています(つまり、 ListViewまたはGridView)。 共通のアダプターは、 ArrayAdapter 、 Base Adapter 、 CursorAdapter 、 SimpleCursorAdapter 、 SpinnerAdapter 、および WrapperListAdapter です。 両方のアダプターについて別々の例を見ることになります。
リストビュー属性
以下は、GridViewに固有の重要な属性です-
Sr.No | Attribute & Description |
---|---|
1 |
android:id これは、レイアウトを一意に識別するIDです。 |
2 |
android:divider これは、リスト項目間を描画するための描画可能または色です。 |
3 |
android:dividerHeight 仕切りの高さを指定します。 これは、px、dp、sp、in、またはmmになります。 |
4 |
android:entries ListViewに入力する配列リソースへの参照を指定します。 |
5 |
android:footerDividersEnabled falseに設定すると、ListViewは各フッタービューの前に仕切りを描画しません。 デフォルト値はtrueです。 |
6 |
android:headerDividersEnabled falseに設定すると、ListViewは各ヘッダービューの後に区切り線を描画しません。 デフォルト値はtrueです。 |
ArrayAdapter
データソースがアレイの場合、このアダプターを使用できます。 デフォルトでは、ArrayAdapterは各項目でtoString()を呼び出し、その内容を TextView に配置することにより、各配列項目のビューを作成します。 あなたがリストビューに表示したい文字列の配列を持っていると考え、各文字列と文字列配列のレイアウトを指定するコンストラクタを使用して新しい ArrayAdapter を初期化します-
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);
このコンストラクタの引数は次のとおりです-
- 最初の引数 this はアプリケーションコンテキストです。 ほとんどの場合、 this のままにしてください。
- 2番目の引数は、XMLファイルで定義されたレイアウトであり、配列内の各文字列に対して TextView を持ちます。
- 最後の引数は、テキストビューに入力される文字列の配列です。
配列アダプタを作成したら、次のように ListView オブジェクトで* setAdapter()*を呼び出すだけです-
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
XMLファイルのres/layoutディレクトリの下にリストビューを定義します。 この例では、activity_main.xmlファイルを使用します。
例
以下は、ListViewを使用して独自のAndroidアプリケーションを作成する方法を示す簡単な手順を示す例です。 _Hello World Example_の章で作成したAndroidアプリケーションを変更するには、次の手順に従います-
Step | Description |
---|---|
1 | You will use Android Studio IDE to create an Android application and name it as ListDisplay under a package com.example.ListDisplay as explained in the Hello World Example chapter. |
2 | Modify the default content of res/layout/activity_main.xml file to include ListView content with the self explanatory attributes. |
3 | No need to change string.xml, Android studio takes care of default string constants. |
4 | Create a Text View file res/layout/activity_listview.xml. This file will have setting to display all the list items. So you can customize its fonts, padding, color etc. using this file. |
6 | Run the application to launch Android emulator and verify the result of the changes done in the application. |
以下は、変更されたメインアクティビティファイル src/com.example.ListDisplay/ListDisplay.java の内容です。 このファイルには、基本的なライフサイクルメソッドのそれぞれを含めることができます。
package com.example.ListDisplay;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListDisplay extends Activity {
//Array of strings...
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",
"WebOS","Ubuntu","Windows7","Max OS X"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
以下は res/layout/activity_main.xml ファイルの内容です-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ListActivity" >
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
以下は、2つの新しい定数を定義する res/values/strings.xml の内容です-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ListDisplay</string>
<string name="action_settings">Settings</string>
</resources>
以下は res/layout/activity_listview.xml ファイルの内容です-
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
変更したばかりの* Hello World!アプリケーションを実行してみましょう。 環境のセットアップ中に *AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像の実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウが表示されます-
SimpleCursorAdapter
データソースがデータベースカーソルの場合、このアダプターを使用できます。 _SimpleCursorAdapter_を使用する場合、 Cursor の各行に使用するレイアウト、およびカーソルのどの列をレイアウトのどのビューに挿入するかを指定する必要があります。
たとえば、人の名前と電話番号のリストを作成する場合、各人の行と名前と番号の列を含むカーソルを返すクエリを実行できます。 次に、各結果のレイアウトでカーソルからどの列を使用するかを指定する文字列配列と、各列を配置する必要がある対応するビューを指定する整数配列を作成します-
String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};
あなたがSimpleCursorAdapterをインスタンス化するとき、各結果、結果を含むカーソル、およびこれらの2つの配列に使用するレイアウトを渡します-
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);
次に、SimpleCursorAdapterは、提供されたレイアウトを使用して、それぞれのColumnsアイテムを対応する toViews ビューに挿入することにより、カーソルの各行のビューを作成します。