1 package com.example.adaptertest; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.view.Menu; 9 import android.view.View;10 import android.view.View.OnClickListener;11 import android.widget.Button;12 import android.widget.CheckBox;13 import android.widget.CompoundButton;14 import android.widget.CompoundButton.OnCheckedChangeListener;15 import android.widget.ListView;16 17 public class MainActivity extends Activity {18 @Override19 protected void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);21 setContentView(R.layout.activity_main);22 // 找到listview23 ListView listView = (ListView) findViewById(R.id.list);24 // 加载数据25 String[] strings = getResources().getStringArray(R.array.list);26 // arrayadapter适配器27 28 // ArrayAdapteradapter = new ArrayAdapter (29 // MainActivity.this, R.layout.list_item_1, strings);30 // listView.setAdapter(adapter);31 //32 33 // simpleadapter适配器34 /*35 * List
1 package com.example.adaptertest; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter;10 import android.widget.CheckBox;11 import android.widget.CompoundButton;12 import android.widget.TextView;13 import android.widget.CompoundButton.OnCheckedChangeListener;14 15 public class MyAdapter extends BaseAdapter {16 private Listlist = null;17 private LayoutInflater inflater = null;18 private View view = null;19 20 public MyAdapter(Context context, List list) {21 this.list = list;22 inflater = LayoutInflater.from(context);23 }24 25 // 返回listView数据的条数26 @Override27 public int getCount() {28 // TODO Auto-generated method stub29 return list.size();30 }31 32 @Override33 public Object getItem(int position) {34 // TODO Auto-generated method stub35 return list.get(position);36 }37 38 @Override39 public long getItemId(int position) {40 // TODO Auto-generated method stub41 return position;42 }43 44 @Override45 public View getView(int position, View convertView, ViewGroup parent) {46 // TODO Auto-generated method stub47 // 获取布局48 view = inflater.inflate(R.layout.list_item_2, null);49 TextView id = (TextView) view.findViewById(R.id.id);50 TextView name = (TextView) view.findViewById(R.id.name);51 TextView sexTextView = (TextView) view.findViewById(R.id.sex);52 final CheckBox box = (CheckBox) view.findViewById(R.id.check);53 final Student student = list.get(position);54 box.setOnCheckedChangeListener(new OnCheckedChangeListener() {55 @Override56 public void onCheckedChanged(CompoundButton buttonView,57 boolean isChecked) {58 student.setState(isChecked);59 }60 });61 // 控件和数据的匹配62 id.setText(student.getId());63 name.setText(student.getName());64 sexTextView.setText(student.getSexString());65 box.setChecked(student.isState());66 return view;67 }68 69 }
1 package com.example.adaptertest; 2 3 public class Student { 4 private String id; 5 private String name; 6 private String sexString; 7 private boolean state; 8 9 public Student(String id, String name, String sexString, boolean state) {10 super();11 this.id = id;12 this.name = name;13 this.sexString = sexString;14 this.state = state;15 }16 17 public String getId() {18 return id;19 }20 21 public void setId(String id) {22 this.id = id;23 }24 25 public String getName() {26 return name;27 }28 29 public void setName(String name) {30 this.name = name;31 }32 33 public String getSexString() {34 return sexString;35 }36 37 public void setSexString(String sexString) {38 this.sexString = sexString;39 }40 41 public boolean isState() {42 return state;43 }44 45 public void setState(boolean state) {46 this.state = state;47 }48 49 }
activity_main.xml
110 11 15 16 20 21 22 28 29 35 36 37
list_item_2.xml
1 26 7 11 12 17 18 23 24 30 31