文件夹页面

6.listview显示不用条目-LMLPHP
布局只有一个listview,可以继承listactivity,这样少写一个布局文件
tool:

  1. * @param index 箱子的索引值
  2. * @return
  3. * 对应的uri 或null
  4. */
  5. public static Uri getUriFromIndex(int index){
  6. switch (index) {
  7. case 0:
  8. return MyConstants.URI_INBOX;
  9. case 1:
  10. return MyConstants.URI_OUTBOX;
  11. case 2:
  12. return MyConstants.URI_DRAFT;
  13. case 3:
  14. return MyConstants.URI_SENT;
  15. }
  16. return null;
  17. }
FolderUI :可以用%来改变条目的颜色
  1. public class FolderUI extends ListActivity implements OnItemClickListener{
  2. private ListView listView;
  3. private String [] names={"收件箱","发件箱","草稿箱","已发送"};
  4. private int[] iconIds={R.drawable.a_f_inbox,R.drawable.a_f_outbox,R.drawable.a_f_draft,R.drawable.a_f_sent};
  5. private int [] counts=new int[4];
  6. public Context ctx;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. ctx = this;
  11. listView = getListView();
  12. adapter = new FolderListAdapter();
  13. listView.setAdapter(adapter);
  14. prepareData();
  15. listView.setOnItemClickListener(this);
  16. }
  17. private void prepareData() {
  18. MyQueryHandler myQueryHandler = new MyQueryHandler(getContentResolver());
  19. for (int i = 0; i <4; i++) {
  20. myQueryHandler.startQuery(i, null, Tools.getUriFromIndex(i), new String[]{" count(*) "}, null, null, null);
  21. }
  22. myQueryHandler.setOnCursorChangedListener(new MyQueryHandler.IOnCursorChangedListener() {
  23. @Override
  24. public void onCursorChanged(int token, Object cookie, Cursor cursor) {
  25. //移动至第一行
  26. cursor.moveToFirst();
  27. // 获得短信的个数
  28. int count = cursor.getInt(0); // 仅查询 短信的条数,仅返回一列
  29. // 以 token 为counts的下标,存短信个数
  30. counts[token] = count;
  31. //刷新listView
  32. adapter.notifyDataSetChanged();
  33. }
  34. });
  35. }
  36. private FolderListAdapter adapter;
  37. class FolderListAdapter extends BaseAdapter{
  38. @Override
  39. public int getCount() {
  40. return names.length;
  41. }
  42. @Override
  43. public Object getItem(int position) {
  44. return null;
  45. }
  46. @Override
  47. public long getItemId(int position) {
  48. return 0;
  49. }
  50. @Override
  51. public View getView(int position, View convertView, ViewGroup parent) {
  52. View view;
  53. if(convertView == null){
  54. view = View.inflate(ctx, R.layout.list_item_folder, null);
  55. }else{
  56. view = convertView;
  57. }
  58. ImageView icon = (ImageView) view.findViewById(R.id.iv_icon_folder);
  59. TextView name = (TextView) view.findViewById(R.id.tv_name_folder);
  60. TextView count = (TextView) view.findViewById(R.id.tv_count_folder);
  61. icon.setBackgroundResource(iconIds[position]);
  62. name.setText(names[position]);
  63. count.setText(""+counts[position]);
  64. // 改变item的背景
  65. if(position%2 == 0){
  66. view.setBackgroundColor(Color.WHITE);
  67. }else{
  68. view.setBackgroundColor(Color.GRAY);
  69. }
  70. return view;
  71. }
  72. }
  73. @Override
  74. /**
  75. * 响应listview 条目点击事件
  76. */
  77. public void onItemClick(AdapterView<?> parent, View view, int position,
  78. long id) {
  79. Intent intent = new Intent(this,FolderDetail.class);
  80. intent.putExtra("position", position);
  81. startActivity(intent);
  82. }
  83. }
文件夹详情页:listview的条目都带着上面的一条的,解决方法
  1. 每个条目判断和上一个条目是否是同一天,不是显示,是隐藏,但是会不停的判读
  2. 在获取日期信息的时候保存到集合,不是同一天就保存,用第二种,缺点一次性全部取出效率不高
6.listview显示不用条目-LMLPHP
  1. public class FolderDetail extends Activity implements OnClickListener{
  2. private ListView listView;
  3. /**
  4. * 在文件夹页面,点击listView的位置
  5. */
  6. private int position;
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. position = getIntent().getIntExtra("position", 0);
  11. setContentView(R.layout.activity_folder_detail);
  12. findViewById(R.id.btn_send).setOnClickListener(this);
  13. listView = (ListView) findViewById(R.id.lv_folder_detail);
  14. adapter = new FolderDetailListAdapter(this, null);
  15. listView.setAdapter(adapter);
  16. showPositionSet = new HashSet<Integer>();
  17. prepareData();
  18. }
  19. /**
  20. * 要查询的列
  21. */
  22. private String[] projection={
  23. "body","_id","address","date"
  24. };
  25. /**
  26. * 短信内容所在列的索引值 为 0
  27. */
  28. private final int INDEX_BODY = 0;
  29. /**
  30. * 短信联系人电话所在列的索引值 为 3
  31. */
  32. private final int INDEX_ADDRESS = 2;
  33. /**
  34. * 短信日期所在列的索引值 为 4
  35. */
  36. private final int INDEX_DATE = 3;
  37. private void prepareData() {
  38. MyQueryHandler myQueryHandler =new MyQueryHandler(getContentResolver());
  39. myQueryHandler.startQuery(99, adapter, Tools.getUriFromIndex(position),
  40. projection, null, null, " date desc");
  41. myQueryHandler.setOnCursorChangedListener(new MyQueryHandler.IOnCursorChangedListener() {
  42. @Override
  43. public void onCursorChanged(int token, Object cookie, Cursor cursor) {
  44. //遍历curosr 将需要显示标题的条目的位置,保存在 showPositionSet
  45. cursor.moveToPosition(-1);// 将cursor 移动到-1 的位置,方便遍历cursor
  46. showPositionSet.clear(); // 清空集合
  47. long lastDay=0;
  48. long thisDay=0;
  49. while(cursor.moveToNext()){
  50. thisDay=cursor.getLong(INDEX_DATE);
  51. if(!isSameToday(lastDay, thisDay)){ // 如果二个时间表示的不是同一天
  52. // 将当前cursor 的行数,保存至集合
  53. showPositionSet.add(cursor.getPosition());
  54. }
  55. lastDay = thisDay;
  56. }
  57. // 刷新listView
  58. adapter.notifyDataSetChanged();
  59. }
  60. });
  61. }
  62. /**
  63. * @return true 如果 二人长型数字,表示的是同一天
  64. */
  65. public boolean isSameToday(long lastDay,long thisDay) {
  66. Time time = new Time();
  67. time.set(lastDay);
  68. int thenYear = time.year;
  69. int thenMonth = time.month;
  70. int thenMonthDay = time.monthDay;
  71. time.set(thisDay);
  72. return (thenYear == time.year)
  73. && (thenMonth == time.month)
  74. && (thenMonthDay == time.monthDay);
  75. }
  76. /**
  77. * 应该显示标题的位置的集合
  78. */
  79. private HashSet<Integer> showPositionSet;
  80. private FolderDetailListAdapter adapter ;
  81. class FolderDetailListAdapter extends CursorAdapter{
  82. public FolderDetailListAdapter(Context context, Cursor c) {
  83. super(context, c);
  84. }
  85. @Override
  86. public View newView(Context context, Cursor cursor, ViewGroup parent) {
  87. View view = View.inflate(context, R.layout.list_item_folder_detail, null);
  88. FolderDetailViewHolder vh = new FolderDetailViewHolder();
  89. vh.title = (TextView) view.findViewById(R.id.tv_title_list_item);
  90. vh.face = (ImageView) view.findViewById(R.id.iv_face_list_item);
  91. vh.address = (TextView) view.findViewById(R.id.tv_address_list_item);
  92. vh.body = (TextView) view.findViewById(R.id.tv_body_list_item);
  93. vh.date = (TextView) view.findViewById(R.id.tv_date_list_item);
  94. view.setTag(vh);
  95. return view;
  96. }
  97. @Override
  98. public void bindView(View view, Context context, Cursor cursor) {
  99. FolderDetailViewHolder vh = (FolderDetailViewHolder) view.getTag();
  100. //TODO
  101. //设置短信内容
  102. vh.body.setText(cursor.getString(INDEX_BODY));
  103. //设置时间
  104. long when = cursor.getLong(INDEX_DATE);
  105. String dateStr;
  106. if(DateUtils.isToday(when)){
  107. dateStr = DateFormat.getTimeFormat(context).format(when);
  108. }else{
  109. dateStr = DateFormat.getDateFormat(context).format(when);
  110. }
  111. vh.date.setText(dateStr);
  112. //设置联系人的名
  113. String number = cursor.getString(INDEX_ADDRESS);
  114. String name = Tools.findNameByNumber(context, number);
  115. if(name == null){//无此联系人
  116. vh.address.setText(number);
  117. }else{
  118. vh.address.setText(name);
  119. }
  120. // 设置头像
  121. int contactId = Tools.findIDByNumber(context, number);
  122. if(contactId == -1){ // 无此联系人
  123. vh.face.setBackgroundResource(R.drawable.ic_unknow_contact_picture);
  124. }else{
  125. Bitmap bitmap = Tools.getFaceById(context, ""+contactId);
  126. if(bitmap ==null){//联系人,无头像
  127. vh.face.setBackgroundResource(R.drawable.ic_contact_picture);
  128. }else{
  129. vh.face.setBackgroundDrawable(new BitmapDrawable(bitmap));
  130. }
  131. }
  132. // 设置标题
  133. if(showPositionSet.contains(cursor.getPosition())){ // 如果集合中包含此行,那么,就显示标题 ,
  134. vh.title.setText(DateFormat.getDateFormat(context).format(when));
  135. vh.title.setVisibility(View.VISIBLE);
  136. }else{
  137. // 否则,就隐藏标题
  138. vh.title.setVisibility(View.GONE);
  139. }
  140. }
  141. }
  142. class FolderDetailViewHolder{
  143. public TextView title;
  144. public ImageView face;
  145. public TextView address;
  146. public TextView body;
  147. public TextView date;
  148. }
  149. @Override
  150. /**
  151. * 响应新建信息的点击事件
  152. */
  153. public void onClick(View v) {
  154. Intent intent = new Intent(this,NewMessageUI.class);
  155. startActivity(intent);
  156. }
  157. }

05-08 08:42