ANDROID笔记:ListView和CheckBox的简单使用

ANDROID笔记:ListView和CheckBox的简单使用

 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     @Override

19     protected void onCreate(Bundle savedInstanceState) {

20         super.onCreate(savedInstanceState);

21         setContentView(R.layout.activity_main);

22         // 找到listview

23         ListView listView = (ListView) findViewById(R.id.list);

24         // 加载数据

25         String[] strings = getResources().getStringArray(R.array.list);

26         // arrayadapter适配器

27 

28         // ArrayAdapter<String> adapter = new ArrayAdapter<String>(

29         // MainActivity.this, R.layout.list_item_1, strings);

30         // listView.setAdapter(adapter);

31         //

32 

33         // simpleadapter适配器

34         /*

35          * List<Map<String, String>> objList = new ArrayList<Map<String,

36          * String>>(); Map<String, String> map = new HashMap<String, String>();

37          * map.put("id", "1"); map.put("name", "zhansan"); map.put("sex", "男");

38          * 

39          * Map<String, String> map1 = new HashMap<String, String>();

40          * map1.put("id", "2"); map1.put("name", "hua"); map1.put("sex", "女");

41          * 

42          * objList.add(map);

43          * 

44          * objList.add(map1);

45          * 

46          * SimpleAdapter simpleAdapter = new SimpleAdapter(MainActivity.this,

47          * objList, R.layout.list_item_2, new String[] { "id", "name", "sex" },

48          * new int[] { R.id.id, R.id.name, R.id.sex });

49          * listView.setAdapter(simpleAdapter);

50          */

51         // 自定义Adapter

52         final CheckBox box = (CheckBox) findViewById(R.id.idcheck);

53         final List<Student> students = new ArrayList<Student>();

54         final MyAdapter adapter2 = new MyAdapter(MainActivity.this, students);

55         students.add(new Student("1", "aa", "男", false));

56         students.add(new Student("2", "bb", "男", false));

57         students.add(new Student("3", "cc", "女", false));

58         Button addButton = (Button) findViewById(R.id.btn_add);

59         addButton.setOnClickListener(new OnClickListener() {

60 

61             @Override

62             public void onClick(View v) {

63                 students.add(new Student((students.size() + 1) + "", "aa", "男",

64                         false));

65                 // 通知适配器更新数据

66                 adapter2.notifyDataSetChanged();

67             }

68         });

69         listView.setAdapter(adapter2);

70         listView.setSelection(students.size() - 1);

71         box.setOnCheckedChangeListener(new OnCheckedChangeListener() {

72             @Override

73             public void onCheckedChanged(CompoundButton buttonView,

74                     boolean isChecked) {

75                 // 全选

76                 for (Student student : students) {

77                     student.setState(isChecked);

78                 }

79                 // 通知适配器更新数据

80                 adapter2.notifyDataSetChanged();

81 

82             }

83         });

84 

85     }

86 

87     @Override

88     public boolean onCreateOptionsMenu(Menu menu) {

89         // Inflate the menu; this adds items to the action bar if it is present.

90         getMenuInflater().inflate(R.menu.main, menu);

91         return true;

92     }

93 

94 }
 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 List<Student> list = null;

17     private LayoutInflater inflater = null;

18     private View view = null;

19 

20     public MyAdapter(Context context, List<Student> list) {

21         this.list = list;

22         inflater = LayoutInflater.from(context);

23     }

24 

25     // 返回listView数据的条数

26     @Override

27     public int getCount() {

28         // TODO Auto-generated method stub

29         return list.size();

30     }

31 

32     @Override

33     public Object getItem(int position) {

34         // TODO Auto-generated method stub

35         return list.get(position);

36     }

37 

38     @Override

39     public long getItemId(int position) {

40         // TODO Auto-generated method stub

41         return position;

42     }

43 

44     @Override

45     public View getView(int position, View convertView, ViewGroup parent) {

46         // TODO Auto-generated method stub

47         // 获取布局

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             @Override

56             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

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 2     xmlns:tools="http://schemas.android.com/tools"

 3     android:layout_width="match_parent"

 4     android:layout_height="match_parent"

 5     android:paddingBottom="@dimen/activity_vertical_margin"

 6     android:paddingLeft="@dimen/activity_horizontal_margin"

 7     android:paddingRight="@dimen/activity_horizontal_margin"

 8     android:paddingTop="@dimen/activity_vertical_margin"

 9     tools:context=".MainActivity" >

10 

11     <LinearLayout

12         android:id="@+id/linear"

13         android:layout_width="fill_parent"

14         android:layout_height="wrap_content" >

15 

16         <CheckBox

17             android:id="@+id/idcheck"

18             android:layout_width="wrap_content"

19             android:layout_height="wrap_content" />

20     </LinearLayout>

21 

22     <Button

23         android:id="@+id/btn_add"

24         android:layout_width="wrap_content"

25         android:layout_height="wrap_content"

26         android:layout_alignParentBottom="true"

27         android:text="添加" />

28 

29     <ListView

30         android:id="@+id/list"

31         android:layout_width="fill_parent"

32         android:layout_height="wrap_content"

33         android:layout_above="@id/btn_add"

34         android:layout_below="@id/linear" >

35     </ListView>

36 

37 </RelativeLayout>

list_item_2.xml

 1 <?xml version="1.0" encoding="utf-8"?>

 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 3     android:layout_width="fill_parent"

 4     android:layout_height="50dp"

 5     android:orientation="horizontal" >

 6 

 7     <CheckBox

 8         android:id="@+id/check"

 9         android:layout_width="wrap_content"

10         android:layout_height="wrap_content" />

11 

12     <TextView

13         android:id="@+id/id"

14         android:layout_width="wrap_content"

15         android:layout_height="wrap_content"

16         android:layout_weight="1" />

17 

18     <TextView

19         android:id="@+id/name"

20         android:layout_width="wrap_content"

21         android:layout_height="wrap_content"

22         android:layout_weight="1" />

23 

24     <TextView

25         android:id="@+id/sex"

26         android:layout_width="wrap_content"

27         android:layout_height="wrap_content"

28         android:layout_gravity="right"

29         android:layout_weight="1" />

30 

31 </LinearLayout>

 

你可能感兴趣的:(ListView)