布局文件的代码:
xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" > android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/iv"/> android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:id="@+id/bigtv"/> android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10sp" android:id="@+id/smalltv"/> android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" android:id="@+id/btn"/> android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/cb"/>
新建一个类继承BaseAdapter并重写方法
public class MySimpleAdapter extends BaseAdapter { private LayoutInflater mInflater;//声明一个LayoutInflater对象用来实例化布局 private List> list;//声明一个list容器对象 private int layoutID;//声明布局ID private String[] flag;//声明ListView项中所有组件映射索引 private int[] ItemIDs;//声明ListView项中所有组件ID数组 Context context; public MySimpleAdapter(Context context,List>list,int layoutID,String flag[],int ItemIDs[]) { //利用构造函数来实例化成员变量对象 this.mInflater =LayoutInflater.from(context); this.list = list; this.layoutID = layoutID; this.flag = flag; this.ItemIDs = ItemIDs; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return 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 position, View converView, ViewGroup parent) //第一个参数:绘制的行数 //第二个参数:绘制的视图这里指的是ListView中每一行的布局 //第三个参数:view的合集,这里不需要 { // TODO Auto-generated method stub converView = mInflater.inflate(layoutID, null);//将布局通过mInflater对象实例化为一个view for(int i=0;i { if(converView.findViewById(ItemIDs[i]) instanceof ImageView)//遍历每一项的组件 { ImageView iv = (ImageView)converView.findViewById(ItemIDs[i]);//实例化组件的对象 iv.setBackgroundResource((Integer)list.get(position).get(flag[i]));//为其组建设置数据 } else if(converView.findViewById(ItemIDs[i]) instanceof TextView) { TextView tv = (TextView)converView.findViewById(ItemIDs[i]); tv.setText((String)list.get(position).get(flag[i])); } } ((Button)converView.findViewById(R.id.btn)).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //弹出对话框 new AlertDialog.Builder(context).setTitle("自定义").setMessage("按钮成功触发监听时间按").show(); } }); ((CheckBox)converView.findViewById(R.id.cb)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // TODO Auto-generated method stub new AlertDialog.Builder(context).setTitle("自定义").setMessage("checked成功触发监听时间按").show(); } }); return converView; } }
主类的代码为
public class MainActivity extends Activity {
private ArrayList> list;//声明一个list的容器 private ListView listView;//声明一个列表 private MySimpleAdapter sp;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); list = new ArrayList>(); listView = new ListView(this); Map map = new HashMap(); map.put("iteml_imageivew", R.drawable.ic_launcher); map.put("iteml_bigtv", "BIGTV"); map.put("iteml_smalltv", "SMALLTV"); list.add(map); sp = new MySimpleAdapter(this,list,R.layout.activity_main,new String[]{"iteml_imageivew","iteml_bigtv","iteml_smalltv"},new int[] {R.id.iv,R.id.bigtv,R.id.smalltv}); listView.setAdapter(sp); this.setContentView(listView);//将适配器数据映射ListView上 }
}