simpleitem.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" > <TextView android:id="@+id/id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="2" /> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left" android:layout_weight="10"/> <TextView android:id="@+id/age" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="3"/> </LinearLayout>
BaseAdapterDemo.java
package com.example.phonedemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.example.phonedemo.util.Utils; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; public class BaseAdapterDemo extends Activity { private ListView listView = null; private LinearLayout layout = null; private MyBaseAdapter adapter = null; /** * 功能:静态类,优化list * * @author qinxijuan * */ static class ViewHolder { TextView id = null; TextView name = null; TextView age = null; } class MyBaseAdapter extends BaseAdapter { public List<Map<String, Object>> list = null; private LayoutInflater lif = null; public MyBaseAdapter(List<Map<String, Object>> l, Context context) { this.list = l; this.lif = LayoutInflater.from(context); } @Override public int getCount() { // TODO Auto-generated method stub return this.list.size(); } @Override public Object getItem(int arg0) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int arg0) { // TODO Auto-generated method stub return 0; } @Override public View getView(int postion, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = lif.inflate(R.layout.simpleitem, null); holder.id = (TextView) convertView.findViewById(R.id.id); holder.name = (TextView) convertView.findViewById(R.id.name); holder.age = (TextView) convertView.findViewById(R.id.age); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.id.setText(this.list.get(postion).get("id").toString()); holder.name.setText(this.list.get(postion).get("name").toString()); holder.age.setText(this.list.get(postion).get("age").toString()); return convertView; } } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.layout = new LinearLayout(this); this.layout.setOrientation(LinearLayout.VERTICAL); this.listView = new ListView(this); this.adapter = new MyBaseAdapter(initList(), this); this.listView.setAdapter(this.adapter); this.listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub Toast.makeText( BaseAdapterDemo.this, "您选择了:" + BaseAdapterDemo.this.adapter.list .get(position).get("id").toString(), Toast.LENGTH_SHORT).show(); } }); this.layout.addView(this.listView, Utils.match); super.addContentView(this.layout, Utils.match); } private List<Map<String, Object>> initList() { List<Map<String, Object>> temp = new ArrayList<Map<String, Object>>(); Map<String, Object> map = null; for (int i = 0; i < 20; i++) { map = new HashMap<String, Object>(); map.put("id", i); map.put("name", "张三" + i + "号"); map.put("age", 28); map.put("email", "zhangsan@163.com"); temp.add(map); } return temp; } }