1<?xml version="1.0" encoding="utf-8"?> 2 3 <TextView xmlns:android="http://schemas.android.com/apk/res/android" 4 android:id="@+id/tv" 5 android:layout_width="match_parent" 6 android:layout_height="wrap_content" 7 /> 8 9<?xml version="1.0" encoding="utf-8"?> 10<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 11 android:layout_width="match_parent" 12 android:layout_height="match_parent" 13 android:orientation="horizontal" > 14 15 <ImageView 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:src="@drawable/ic_launcher" /> 19 20 <TextView 21 android:id="@+id/item_tv" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" /> 24 25</LinearLayout> 26 27<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 28 xmlns:tools="http://schemas.android.com/tools" 29 android:layout_width="match_parent" 30 android:layout_height="match_parent" 31 android:paddingBottom="@dimen/activity_vertical_margin" 32 android:paddingLeft="@dimen/activity_horizontal_margin" 33 android:paddingRight="@dimen/activity_horizontal_margin" 34 android:paddingTop="@dimen/activity_vertical_margin" 35 tools:context=".MainActivity" > 36 37 <ListView 38 android:id="@+id/arr_lv" 39 android:layout_width="match_parent" 40 android:layout_height="match_parent"></ListView> 41 42</RelativeLayout>
Activity:
1package com.example.arrayadapter; 2 3import java.util.ArrayList; 4 5import android.os.Bundle; 6import android.app.Activity; 7import android.view.Menu; 8import android.widget.ArrayAdapter; 9import android.widget.ListView; 10 11public class MainActivity extends Activity 12{ 13 14 private String [] names={"功能1","功能2","功能3","功能4","功能5"}; 15 private ArrayList<String> list=new ArrayList<String>(); 16 @Override 17 protected void onCreate(Bundle savedInstanceState) 18 { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 22 ListView lv=(ListView)findViewById(R.id.arr_lv); 23 //4个参数的构造方法 24 //lv.setAdapter(new ArrayAdapter<String>(this,R.layout.listitem,R.id.item_tv,names)); 25 //3个参数的构造方法 要求布局文件只能有一个textview 26 lv.setAdapter(new ArrayAdapter<String>(this,R.layout.listitem_tv,getData())); 27 } 28 private ArrayList<String> getData() 29 { 30 list.add("180平米的房子"); 31 list.add("一个勤劳漂亮的老婆"); 32 list.add("一辆宝马"); 33 list.add("一个强壮且永不生病的身体"); 34 list.add("一个喜欢的事业"); 35 return list; 36 } 37 @Override 38 public boolean onCreateOptionsMenu(Menu menu) 39 { 40 // Inflate the menu; this adds items to the action bar if it is present. 41 getMenuInflater().inflate(R.menu.main, menu); 42 return true; 43 } 44 45}