事前準備

次のプロジェクトをEclipseにインポートしてください。
プロジェクト名PhotoViewerPart2.zip

概要

リストの一行のなかに、画像とテキストが含まれたレイアウトを実現するなど
複雑なレイアウトに対応する場合は、ArrayAdapterを継承した独自のAdapterを実装する必要があります。

カスタマイズ手順

一行のレイアウトを作成

res/layout/custom_list_item.xmlを次の内容に書き換えてください。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="10dip"
  >
  <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff"
    android:paddingTop="8dip"
    android:paddingBottom="8dip"
    android:paddingLeft="14dip"
    android:paddingRight="14dip"
    >
    <ImageView
      android:id="@+id/imageview"
      android:layout_width="85dip"
      android:layout_height="85dip"
      android:scaleType="centerCrop"
      android:padding="8dip"
      />
    <TextView
      android:id="@+id/textview"
      android:layout_width="wrap_content"
      android:layout_height="fill_parent"
      android:layout_weight="1"
      android:textColor="@color/dark_blue"
      android:background="#ffffff"
      android:layout_marginLeft="16dip"
      android:layout_marginTop="8dip"
      />
  </LinearLayout>
</FrameLayout>

一行のデータを表すクラス生成

src/jp.co.example.listview/BindData.javaを、次の内容に書き換えてください。
public class BindData {
// ------------ ↓ここから ------------------
	private String text;
	private int imageResourceId;

	public BindData(String text, int id) {
		this.text = text;
		this.imageResourceId = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public int getImageResourceId() {
		return imageResourceId;
	}

	public void setImageResourceId(int imageResourceId) {
		this.imageResourceId = imageResourceId;
	}
// ------------ ↑ここまで ------------------
}

独自Adapterを作成

src/jp.co.example.listview/ImageAdapter.javaを、次の内容に書き換えてください。
public class ImageAdapter extends ArrayAdapter<BindData> {
// ------------ ↓ここから ------------------
	private LayoutInflater inflater;

	public ImageAdapter(Context context, List<BindData> objects) {
		super(context, 0, objects);
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			// 行を表すビューオブジェクトを取得
			convertView = inflater.inflate(R.layout.custom_list_item, parent,
					false);
		}
		// 画像名と日付の表示エリア
		TextView textView = (TextView) convertView.findViewById(R.id.textview);
		// 画像の表示エリア
		ImageView imageView = (ImageView) convertView
				.findViewById(R.id.imageview);
		// 該当ポジションのデータを取得
		BindData data = getItem(position);
		// テキストビューとイメージビューにセット
		textView.setText(data.getText());
		imageView.setImageResource(data.getImageResourceId());

		return convertView;
	}
// ------------ ↑ここまで ------------------
}

Activityを生成

src/jp.co.example.listview/PhotoViewerActivity.javaを、次の内容に書き換えてください。
public class PhotoViewerActivity extends ListActivity {
// ------------ ↓ここから ------------------
	private Integer[] mThumbIds = {
			R.drawable.image1,
			R.drawable.image2,
			R.drawable.image3,
			R.drawable.image4,
			R.drawable.image5,
			R.drawable.image6 };

	private String[] mTitles = {
			"image1\n\n2010/11/20",
			"image2\n\n2010/11/21",
			"image3\n\n2010/11/22",
			"image4\n\n2010/11/23",
			"image5\n\n2010/11/24",
			"image6\n\n2010/11/25" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.custom_list);
	
		// BindDataのリストを生成し、アダプターにセット
		List<BindData> objects = new ArrayList<BindData>();
		for(int i = 0; i < mTitles.length; i++) {
			BindData data =
				new BindData(mTitles[i], mThumbIds[i]);
			objects.add(data);
		}
		setListAdapter(new ImageAdapter(this, objects));
	}
// ------------ ↑ここまで ------------------
}

実行

次のように表示されましたか?

このページへのコメント

u3sFM3 <a href="http://whixyixurfkc.com/">whixyixurfkc</a>, [url=http://smujtxesmqnk.com/]smujtxesmqnk[/url], [link=http://ohdtvabbnwjj.com/]ohdtvabbnwjj[/link], http://kvdiqpvuwdqq.com/

0
Posted by yjjwyllrna 2013年11月14日(木) 14:18:43 返信

goodhousekeeping moles forth

0
Posted by http://download.fasttrack.schedule.8.0.8.softwarenvm.com 2013年10月03日(木) 05:27:44
http://download.fasttrack.schedule.8.0.8.softwaren...
返信

dkEqdA <a href="http://xbuosoozzbns.com/">xbuosoozzbns</a>, [url=http://lyecikyhvbnc.com/]lyecikyhvbnc[/url], [link=http://dxnqczoqzgbf.com/]dxnqczoqzgbf[/link], http://yulogakcawca.com/

0
Posted by mwavauyc 2013年09月22日(日) 00:51:57
http://hfycanvxoabp.com/
返信

qy5U1b <a href="http://tltflixwaqwe.com/">tltflixwaqwe</a>, [url=http://ynmlddcnokgj.com/]ynmlddcnokgj[/url], [link=http://loiknmuokqzg.com/]loiknmuokqzg[/link], http://avmmiugzwrbu.com/

0
Posted by azkqnsv 2013年07月07日(日) 11:04:35
http://zczgqplpzspw.com/
返信

b6OTXi <a href="http://odralpufzvcs.com/">odralpufzvcs</a>, [url=http://rwldxexrcjcs.com/]rwldxexrcjcs[/url], [link=http://xttgadkmgkpw.com/]xttgadkmgkpw[/link], http://lskcmvcrhyqu.com/

0
Posted by azpyxg 2013年06月17日(月) 15:47:54
http://woqvqpnlzuiy.com/
返信

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

管理人/副管理人のみ編集できます