博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ANDROID笔记:ListView和CheckBox的简单使用
阅读量:5162 次
发布时间:2019-06-13

本文共 6627 字,大约阅读时间需要 22 分钟。

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         // ArrayAdapter
adapter = new ArrayAdapter
(29 // MainActivity.this, R.layout.list_item_1, strings);30 // listView.setAdapter(adapter);31 //32 33 // simpleadapter适配器34 /*35 * List
> objList = new ArrayList
>(); Map
map = new HashMap
();37 * map.put("id", "1"); map.put("name", "zhansan"); map.put("sex", "男");38 * 39 * Map
map1 = new HashMap
();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 // 自定义Adapter52 final CheckBox box = (CheckBox) findViewById(R.id.idcheck);53 final List
students = new ArrayList
();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 @Override62 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 @Override73 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 @Override88 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
list = 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

1 
10 11
15 16
20 21 22
28 29
35 36 37

list_item_2.xml

1 
2
6 7
11 12
17 18
23 24
30 31

 

转载于:https://www.cnblogs.com/afluy/p/3373488.html

你可能感兴趣的文章
Java8内存模型—永久代(PermGen)和元空间(Metaspace)(转)
查看>>
ObjectiveC基础教程(第2版)
查看>>
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
求出斐波那契数组
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>
.NET CLR基本术语
查看>>
ubuntu的home目录下,Desktop等目录消失不见
查看>>
建立,查询二叉树 hdu 5444
查看>>
[Spring框架]Spring 事务管理基础入门总结.
查看>>
2017.3.24上午
查看>>
Python-常用模块及简单的案列
查看>>
LeetCode 159. Longest Substring with At Most Two Distinct Characters
查看>>
LeetCode Ones and Zeroes
查看>>
基本算法概论
查看>>
jquery动态移除/增加onclick属性详解
查看>>
JavaScript---Promise
查看>>