BaseAdapter方式
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android1:id="@+id/listView1"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
android1:layout_weight="1" >
</ListView>
</LinearLayout>
list_item.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="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:id="@+id/waibubuju"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bianhao"
android:text="编号"
android:textColor="#88ff0000"
android:gravity="center"
android:textSize="18sp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/neibubuju"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:id="@+id/username"
/>
MainActivity.java
package com.hyl.listViewpack;
import java.util.ArrayList;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
private ListView listView1;
ArrayList<ArrayList<String>> arr ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1=(ListView) findViewById(R.id.listView1);
arr=new ArrayList<ArrayList<String>>();
for(int i=0;i<=50;i++){
ArrayList<String> a=new ArrayList<String>();
a.add("编号:"+i);
a.add("姓名:"+i);
a.add("电话:"+i);
arr.add(a);
}
listView1.setAdapter(new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
//父窗体 挂载
View view=View.inflate(MainActivity.this, R.layout.list_item, null);
Log.e(TAG, "测试创建对象位置:"+position);
ArrayList<String> a=arr.get(position);
TextView tvbianhao=(TextView) view.findViewById(R.id.bianhao);
tvbianhao.setText( a.get(0));
TextView tvUserName=(TextView) view.findViewById(R.id.username);
tvUserName.setText( a.get(1));
TextView tvTel=(TextView) view.findViewById(R.id.tel);
tvTel.setText( a.get(2));
return view;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public int getCount() {
return arr.size();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话"
android:id="@+id/tel"
/>
</LinearLayout>
</LinearLayout>
ArrayAdapter方式
//上下文对象 布局列表对象 显示的TextView的ID 数组对象
listView1.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, R.id.bianhao, new String[]{"选项一","选项二","选项三","选项四","选项五"}));
SimpleAdapter方式
listView1=(ListView) findViewById(R.id.listView1);
ArrayList<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
Map<String, Object> map1=new HashMap<String, Object>();
map1.put("icon", R.drawable.ic1);
map1.put("name", "功能一");
list.add(map1);
Map<String, Object> map2=new HashMap<String, Object>();
map2.put("icon", R.drawable.ic2);
map2.put("name", "功能二");
list.add(map2);
Map<String, Object> map3=new HashMap<String, Object>();
map3.put("icon", R.drawable.ic3);
map3.put("name", "功能三");
list.add(map3);
Map<String, Object> map4=new HashMap<String, Object>();
map4.put("icon", R.drawable.ic1);
map4.put("name", "功能四");
list.add(map4);
Map<String, Object> map5=new HashMap<String, Object>();
map4.put("icon", R.drawable.ic5);
map4.put("name", "功能五");
list.add(map5);
listView1.setAdapter(new SimpleAdapter(this, list, R.layout.list_item, new String[]{"icon","name"},new int[]{R.id.tubiao,R.id.gongneng} ));