HOME > Android > ListView(BaseAdapter)

Android -ListView(BaseAdapter)-

ListView(BaseAdapter)

BaseAdapterクラスを継承したクラスには、ArrayAdapter・CursorAdapter・SimpleAdapterクラスがあります。

BaseAdapterクラスを拡張し、独自のAdapterクラスを定義、getViewメソッドをオーバーライドすることで、リスト項目の レイアウトを自由にカスタマイズすることができます。

ポイントは、getViewでリスト項目を1列表示する度に、毎回新しいウィジェットを作成せず、初回に作成した後は、再利用します。

getView の 最適化の参考サイト:Y.A.M の 雑記帳

ListView(BaseAdapter)サンプル

res/layout/main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/layout_id"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#ffffff"
  7. android:orientation="vertical" >
  8. <ListView
  9. android:id="@+id/lsit_view"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content" />
  12. </LinearLayout>

res/layout/list_row.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:background="#ffffff"
  6. android:orientation="horizontal" >
  7. <ImageView
  8. android:id="@+id/flag_image"
  9. android:layout_width="40dp"
  10. android:layout_height="30dp"
  11. android:layout_marginBottom="5dp"
  12. android:layout_marginLeft="5dp"
  13. android:layout_marginRight="20dp"
  14. android:layout_marginTop="5dp"
  15. android:src="@drawable/ic_launcher" />
  16. <LinearLayout
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:orientation="vertical" >
  20. <TextView
  21. android:id="@+id/flag_code"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:textColor="#000000" />
  25. <TextView
  26. android:id="@+id/flag_name"
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_marginLeft="10dp"
  30. android:textColor="#000000" />
  31. </LinearLayout>
  32. </LinearLayout>

ListViewEx11.java

  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.os.Bundle;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.BaseAdapter;
  12. import android.widget.ImageView;
  13. import android.widget.ListView;
  14. import android.widget.TextView;
  15. public class ListViewEx11 extends Activity {
  16. //国番号
  17. String[] flagCode = new String[] { "+81", "+82", "+86", "+49", "+44",
  18. "+91", "+57", "+65", "+34", "+45", "+20", "+30", "+41", "+46",
  19. "+84" };
  20. //国名
  21. String[] flagNames = new String[] { "日本", "韓国", "中国", "ドイツ", "イギリス", "インド",
  22. "コロンビア", "シンガポール", "スペイン", "デンマーク", "エジプト", "ギリシャ", "スイス",
  23. "スウェーデン", "ベトナム" };
  24. //国旗のリソース
  25. int[] nationlFlag = { R.drawable.japan, R.drawable.korea,
  26. R.drawable.china, R.drawable.germany, R.drawable.uk,
  27. R.drawable.india, R.drawable.colombia, R.drawable.singapore,
  28. R.drawable.spain, R.drawable.denmark, R.drawable.egypt,
  29. R.drawable.greece, R.drawable.swiss, R.drawable.swedish,
  30. R.drawable.vietnam
  31. };
  32. //アダプタの要素情報を保持するクラス
  33. class NationlFlag {
  34. private Bitmap image;
  35. private String code;
  36. private String name;
  37. public NationlFlag(Bitmap image, String code, String name) {
  38. this.image = image;
  39. this.code = code;
  40. this.name = name;
  41. }
  42. public Bitmap getImage() {
  43. return image;
  44. }
  45. public String getCode() {
  46. return code;
  47. }
  48. public String getName() {
  49. return name;
  50. }
  51. }
  52. //毎回viewをinflateせず、再利用する為のクラス
  53. private static class ViewHolder {
  54. ImageView image;
  55. TextView tvCode;
  56. TextView tvName;
  57. }
  58. class ListFlagAdapter extends BaseAdapter {
  59. private Context context;
  60. private List<NationlFlag> list;
  61. public ListFlagAdapter(Context context) {
  62. super();
  63. this.context = context;
  64. list = new ArrayList<ListViewEx11.NationlFlag>();
  65. for (int i = 0; i < flagNames.length; i++) {
  66. Bitmap bmp = BitmapFactory.decodeResource(
  67. context.getResources(), nationlFlag[i]);
  68. list.add(new NationlFlag(bmp, flagCode[i], flagNames[i]));
  69. }
  70. }
  71. //数の取得
  72. @Override
  73. public int getCount() {
  74. return list.size();
  75. }
  76. //要素の取得
  77. @Override
  78. public Object getItem(int position) {
  79. return list.get(position);
  80. }
  81. //要素のID取得
  82. @Override
  83. public long getItemId(int position) {
  84. return position;
  85. }
  86. //Viewの生成
  87. @Override
  88. public View getView(int position, View convertView, ViewGroup parent) {
  89. NationlFlag nFlag = (NationlFlag) getItem(position);
  90. ViewHolder holder;
  91. //Viewオブジェクトが生成されていない場合
  92. if (convertView == null) {
  93. LayoutInflater inflater = LayoutInflater.from(context);
  94. convertView = inflater.inflate(R.layout.list_row, null);
  95. holder = new ViewHolder();
  96. holder.image = (ImageView) convertView
  97. .findViewById(R.id.flag_image);
  98. holder.tvCode = (TextView) convertView
  99. .findViewById(R.id.flag_code);
  100. holder.tvName = (TextView) convertView
  101. .findViewById(R.id.flag_name);
  102. convertView.setTag(holder);
  103. //すでにViewオブジェクトが生成されている場合
  104. } else {
  105. holder = (ViewHolder) convertView.getTag();
  106. }
  107. holder.image.setImageBitmap(nFlag.getImage());
  108. holder.tvCode.setText(nFlag.getCode());
  109. holder.tvName.setText(nFlag.getName());
  110. return convertView;
  111. }
  112. }
  113. @Override
  114. public void onCreate(Bundle savedInstanceState) {
  115. super.onCreate(savedInstanceState);
  116. setContentView(R.layout.main);
  117. ListView listView = (ListView) findViewById(R.id.lsit_view);
  118. listView.setAdapter(new ListFlagAdapter(this));
  119. }
  120. }
Copyright © 2018   MitoRoid  All Rights Reserved.