Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1078971
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-16 15:35:22

前面的已经介绍了如何将check选中,但是很不尽如人意的是,每次listView都会调用getView方法来刷新页面,所以导致之前选中的check会在调用getView之后会被取消。
通过:http://blog.csdn.net/whucyl/article/details/7341133
上面的提示对部件进行了重写。
使用一个select的方法,监听选中是否,然后加入一个hashmap,用来保存每次用户选中的状态,每次getView的查看此状态即可。
一下是代码:
MyAdapter方法:
Java代码 复制代码 收藏代码
  1. package com.duduli.li;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.View.OnClickListener;
  11. import android.view.ViewGroup;
  12. import android.widget.AdapterView;
  13. import android.widget.CheckedTextView;
  14. import android.widget.SimpleCursorAdapter;
  15. import android.widget.TextView;
  16. public class MyAdapter extends SimpleCursorAdapter{
  17. private List list = new ArrayList();
  18. private HashMap isSelected = null;
  19. public String s[];
  20. private List> data;
  21. private LayoutInflater layoutInflater;
  22. private List delNumber = new ArrayList();
  23. @SuppressWarnings("unused")
  24. private Context context;
  25. public MyAdapter(Context context, int layout, Cursor c,
  26. String[] from, int[] to) {
  27. super(context, layout, c, from, to);
  28. this.context = context;
  29. this.data = getData();
  30. this.layoutInflater = LayoutInflater.from(context);
  31. isSelected = new HashMap();
  32. }
  33. @Override
  34. public int getCount() {
  35. // TODO Auto-generated method stub
  36. return super.getCount();
  37. }
  38. @Override
  39. public Object getItem(int position) {
  40. // TODO Auto-generated method stub
  41. return super.getItem(position);
  42. }
  43. @Override
  44. public long getItemId(int position) {
  45. // TODO Auto-generated method stub
  46. return super.getItemId(position);
  47. }
  48. @SuppressWarnings("unused")
  49. @Override
  50. public View getView(final int position, View convertView, ViewGroup parent) {
  51. // TODO Auto-generated method stub
  52. Compan cp = null;
  53. if(cp == null){
  54. cp = new Compan();
  55. convertView = layoutInflater.inflate(R.layout.list, null);
  56. cp.id = (TextView) convertView.findViewById(R.id._id);
  57. cp.ctv = (CheckedTextView) convertView.findViewById(R.id.checkText);
  58. convertView.setTag(cp);
  59. }else{
  60. cp = (Compan) convertView.getTag();
  61. }
  62. cp.id.setText((CharSequence) data.get(position).get("_id"));
  63. cp.ctv.setText((CharSequence) data.get(position).get("Number"));
  64. // [b]此处即为状态保存的方法![/b]
  65. Boolean b = isSelected.get(position);
  66. if(null == b){
  67. b = false;
  68. }
  69. if (convertView instanceof ViewGroup) {
  70. ViewGroup g = (ViewGroup) convertView;
  71. for (int i = 0; i < g.getChildCount(); i++) {
  72. View v = g.getChildAt(i);
  73. if (v instanceof CheckedTextView) {
  74. ((CheckedTextView) v).setChecked(b);
  75. break;
  76. }
  77. }
  78. }
  79. return convertView;
  80. }
  81. public List> getData() {
  82. List> list = new ArrayList>();
  83. while (getCursor().moveToNext()) {
  84. Map map = new HashMap();
  85. // System.out.println("ID: "+getCursor().getString(0));
  86. map.put("_id", getCursor().getString(0));
  87. // System.out.println("NAME: "+getCursor().getString(1));
  88. map.put("Number", getCursor().getString(1));
  89. list.add(map);
  90. }
  91. return list;
  92. }
  93. // 选中加入hashmap
  94. public void select(int postion, boolean isChecked) {
  95. isSelected.put(postion, isChecked);
  96. }
  97. }
package com.duduli.li; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.content.Context; import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.CheckedTextView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class MyAdapter extends SimpleCursorAdapter{ private List list = new ArrayList(); private HashMap isSelected = null; public String s[]; private List> data; private LayoutInflater layoutInflater; private List delNumber = new ArrayList(); @SuppressWarnings("unused") private Context context; public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.context = context; this.data = getData(); this.layoutInflater = LayoutInflater.from(context); isSelected = new HashMap(); } @Override public int getCount() { // TODO Auto-generated method stub return super.getCount(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return super.getItem(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return super.getItemId(position); } @SuppressWarnings("unused") @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub Compan cp = null; if(cp == null){ cp = new Compan(); convertView = layoutInflater.inflate(R.layout.list, null); cp.id = (TextView) convertView.findViewById(R.id._id); cp.ctv = (CheckedTextView) convertView.findViewById(R.id.checkText); convertView.setTag(cp); }else{ cp = (Compan) convertView.getTag(); } cp.id.setText((CharSequence) data.get(position).get("_id")); cp.ctv.setText((CharSequence) data.get(position).get("Number")); // [b]此处即为状态保存的方法![/b] Boolean b = isSelected.get(position); if(null == b){ b = false; } if (convertView instanceof ViewGroup) { ViewGroup g = (ViewGroup) convertView; for (int i = 0; i < g.getChildCount(); i++) { View v = g.getChildAt(i); if (v instanceof CheckedTextView) { ((CheckedTextView) v).setChecked(b); break; } } } return convertView; } public List> getData() { List> list = new ArrayList>(); while (getCursor().moveToNext()) { Map map = new HashMap(); // System.out.println("ID: "+getCursor().getString(0)); map.put("_id", getCursor().getString(0)); // System.out.println("NAME: "+getCursor().getString(1)); map.put("Number", getCursor().getString(1)); list.add(map); } return list; } // 选中加入hashmap public void select(int postion, boolean isChecked) { isSelected.put(postion, isChecked); } }

然后Activity:
Java代码 复制代码 收藏代码
  1. package com.duduli.li;
  2. import android.app.ListActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.AdapterView;
  6. import android.widget.AdapterView.OnItemClickListener;
  7. import android.widget.CheckedTextView;
  8. import android.widget.ListView;
  9. import com.duduli.db.AllowCallDAO;
  10. /**
  11. * This example shows how to use choice mode on a list. This list is
  12. * in CHOICE_MODE_MULTIPLE mode, which means the items behave like
  13. * checkboxes.
  14. */
  15. public class CallComingTest2Activity extends ListActivity {
  16. private CheckedTextView multiple;
  17. @Override
  18. public void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. AllowCallDAO dao = new AllowCallDAO(this);
  21. MyAdapter adapter = new MyAdapter(this, R.layout.list, dao.show()
  22. , new String[]{"_id","phonenr"}, new int[]{R.id._id,R.id.checkText});
  23. final ListView listView = getListView();
  24. listView.setItemsCanFocus(false);
  25. listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  26. setListAdapter(adapter);
  27. }
  28. @Override
  29. protected void onListItemClick(ListView l, View v, int position, long id) {
  30. // TODO Auto-generated method stub
  31. System.out.println(position);
  32. multiple = (CheckedTextView)v.findViewById(R.id.checkText);
  33. if (multiple != null) {
  34. multiple.toggle();
  35. ((MyAdapter) l.getAdapter()).select(position, multiple.isChecked());
  36. }
  37. if(multiple.isChecked()){
  38. // getListView().getItemAtPosition(position);
  39. System.out.println("check");
  40. }else{
  41. System.out.println("not check");
  42. }
  43. }
  44. }
package com.duduli.li; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.CheckedTextView; import android.widget.ListView; import com.duduli.db.AllowCallDAO; /** * This example shows how to use choice mode on a list. This list is * in CHOICE_MODE_MULTIPLE mode, which means the items behave like * checkboxes. */ public class CallComingTest2Activity extends ListActivity { private CheckedTextView multiple; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AllowCallDAO dao = new AllowCallDAO(this); MyAdapter adapter = new MyAdapter(this, R.layout.list, dao.show() , new String[]{"_id","phonenr"}, new int[]{R.id._id,R.id.checkText}); final ListView listView = getListView(); listView.setItemsCanFocus(false); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub System.out.println(position); multiple = (CheckedTextView)v.findViewById(R.id.checkText); if (multiple != null) { multiple.toggle(); ((MyAdapter) l.getAdapter()).select(position, multiple.isChecked()); } if(multiple.isChecked()){ // getListView().getItemAtPosition(position); System.out.println("check"); }else{ System.out.println("not check"); } } }
阅读(682) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~