MainActivity.java
package com.example.arrayadapter; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends ActionBarActivity { private ListView lv; private String[] names = {"第一关","第二关","第三关","第四关","第五关"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView)findViewById(R.id.lv); lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_arrayadapter, R.id.tv, names)); } }res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context="com.example.arrayadapter.MainActivity" > <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </RelativeLayout>创建一个xml文件list_arrayadapter.xml res/layout/list_arrayadapter.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#88000000" android:orientation="horizontal" > <ImageView android:layout_width="25dip" android:layout_height="25dip" android:src="@drawable/icon_3" > </ImageView> <TextView android:layout_marginLeft="10dip" android:layout_marginTop="2dip" android:textSize="20dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv"> </TextView> </LinearLayout>