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

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-03-16 15:36:44

网络上的那个东西似乎都不准,以下是我做的此方法为可行。
首先,main.xml
Java代码 复制代码 收藏代码
  1. "1.0" encoding="utf-8"?>
  2. ""
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. android:id="@+id/listView1"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content" >


然后list.xml
Java代码 复制代码 收藏代码
  1. "1.0" encoding="utf-8"?>
  2. ""
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. android:id="@+id/_id"
  7. android:visibility="gone"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_alignParentLeft="true"
  11. android:text="TextView" />
  12. android:id="@+id/checkText"
  13. android:layout_width="fill_parent"
  14. android:layout_height="?android:attr/listPreferredItemHeight"
  15. android:textAppearance="?android:attr/textAppearanceLarge"
  16. android:gravity="center_vertical"
  17. android:checkMark="?android:attr/listChoiceIndicatorMultiple"
  18. android:paddingLeft="6dip"
  19. android:paddingRight="6dip"
  20. />


以下是自定义组件。
Java代码 复制代码 收藏代码
  1. public final class Compan {
  2. public TextView id;
  3. public CheckedTextView ctv;
  4. }
public final class Compan { public TextView id; public CheckedTextView ctv; }

自定义adapter方法
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. public String s[];
  19. private List> data;
  20. private LayoutInflater layoutInflater;
  21. private List delNumber = new ArrayList();
  22. @SuppressWarnings("unused")
  23. private Context context;
  24. public MyAdapter(Context context, int layout, Cursor c,
  25. String[] from, int[] to) {
  26. super(context, layout, c, from, to);
  27. this.context = context;
  28. this.data = getData();
  29. this.layoutInflater = LayoutInflater.from(context);
  30. }
  31. @Override
  32. public int getCount() {
  33. // TODO Auto-generated method stub
  34. return super.getCount();
  35. }
  36. @Override
  37. public Object getItem(int position) {
  38. // TODO Auto-generated method stub
  39. return super.getItem(position);
  40. }
  41. @Override
  42. public long getItemId(int position) {
  43. // TODO Auto-generated method stub
  44. return super.getItemId(position);
  45. }
  46. @SuppressWarnings("unused")
  47. @Override
  48. public View getView(final int position, View convertView, ViewGroup parent) {
  49. // TODO Auto-generated method stub
  50. Compan cp = null;
  51. if(cp == null){
  52. cp = new Compan();
  53. convertView = layoutInflater.inflate(R.layout.list, null);
  54. cp.id = (TextView) convertView.findViewById(R.id._id);
  55. cp.ctv = (CheckedTextView) convertView.findViewById(R.id.checkText);
  56. convertView.setTag(cp);
  57. }else{
  58. cp = (Compan) convertView.getTag();
  59. }
  60. cp.id.setText((CharSequence) data.get(position).get("_id"));
  61. cp.ctv.setText((CharSequence) data.get(position).get("Number"));
  62. return convertView;
  63. }
  64. public List> getData() {
  65. List> list = new ArrayList>();
  66. while (getCursor().moveToNext()) {
  67. Map map = new HashMap();
  68. // System.out.println("ID: "+getCursor().getString(0));
  69. map.put("_id", getCursor().getString(0));
  70. // System.out.println("NAME: "+getCursor().getString(1));
  71. map.put("Number", getCursor().getString(1));
  72. list.add(map);
  73. }
  74. return list;
  75. }
  76. }
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(); 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); } @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")); 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; } }

下面是Acitivity
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. setListAdapter(adapter);
  26. }
  27. @Override
  28. protected void onListItemClick(ListView l, View v, int position, long id) {
  29. // TODO Auto-generated method stub
  30. System.out.println(position);
  31. // [b]下面的才是最重要的,网络上写的为:multiple = (CheckedTextView)l.getChildAt(position).findViewById(R.id.checkText);此方法很有问题,比如说listview进行翻页之后就会出问题。[/b]
  32. multiple = (CheckedTextView)v.findViewById(R.id.checkText);
  33. if(multiple != null){
  34. System.out.println("not null");
  35. }else{
  36. System.out.println("null");
  37. }
  38. if(multiple.isChecked()){
  39. multiple.setChecked(false);
  40. }else{
  41. multiple.setChecked(true);
  42. }
  43. super.onListItemClick(l, v, position, id);
  44. }
  45. }
阅读(799) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~