/TextEnlarger/src/com/ideal/textenlarger/ExtendedCheckBoxListAdapter.java

http://eyes-free.googlecode.com/ · Java · 145 lines · 57 code · 20 blank · 68 comment · 5 complexity · c861f6474babd6c54489b02d14af3ae4 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 The IDEAL Group
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.ideal.textenlarger;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import android.content.Context;
  20. import android.view.View;
  21. import android.view.ViewGroup;
  22. import android.widget.BaseAdapter;
  23. /**
  24. * Class needed by ApplicationsListActivity.
  25. *
  26. * This was taken from a checkbox list tutorial at anddev.org:
  27. * http://www.anddev.org/extended_checkbox_list__extension_of_checkbox_text_list_tu-t5734.html
  28. */
  29. public class ExtendedCheckBoxListAdapter extends BaseAdapter {
  30. /** Remember our context so we can use it when constructing views. */
  31. private Context mContext;
  32. private List<ExtendedCheckBox> mItems = new ArrayList<ExtendedCheckBox>();
  33. /**
  34. *
  35. * @param context - Render context
  36. */
  37. public ExtendedCheckBoxListAdapter(Context context) {
  38. mContext = context;
  39. }
  40. /**
  41. * Add a new Item at the end of the exiting ones
  42. * @param it - New item to be added
  43. */
  44. public void addItem(ExtendedCheckBox it) {
  45. mItems.add(it);
  46. }
  47. /**
  48. * Will force to use a list of items
  49. * @param lit - List of items to be used
  50. */
  51. public void setListItems(List<ExtendedCheckBox> lit) {
  52. mItems = lit;
  53. }
  54. /**
  55. * @return The number of items this adapter offers
  56. */
  57. public int getCount() {
  58. return mItems.size();
  59. }
  60. /**
  61. * Return item at a specific position
  62. */
  63. public Object getItem(int position) {
  64. return mItems.get(position);
  65. }
  66. /**
  67. * Returns the position of an element
  68. */
  69. public int GetPosition( ExtendedCheckBox item ) {
  70. int count = getCount();
  71. for ( int i = 0; i < count; i++ )
  72. {
  73. if ( item.compareTo((ExtendedCheckBox)getItem(i)) == 0 )
  74. return i;
  75. }
  76. return -1;
  77. }
  78. /**
  79. * Set selection of an item
  80. * @param value - true or false
  81. * @param position - position
  82. */
  83. public void setChecked(boolean value, int position) {
  84. mItems.get(position).setChecked(value);
  85. }
  86. /**
  87. * Select all elements
  88. */
  89. public void selectAll() {
  90. for(ExtendedCheckBox cboxtxt: mItems)
  91. cboxtxt.setChecked(true);
  92. /* Things have changed, do a redraw. */
  93. this.notifyDataSetInvalidated();
  94. }
  95. /**
  96. * Deselect all elements
  97. */
  98. public void deselectAll() {
  99. for(ExtendedCheckBox cboxtxt: mItems)
  100. cboxtxt.setChecked(false);
  101. /* Things have changed, do a redraw. */
  102. this.notifyDataSetInvalidated();
  103. }
  104. /**
  105. * Decides if all items are selectable
  106. * @return - true or false
  107. */
  108. public boolean areAllItemsSelectable() {
  109. return false;
  110. }
  111. /**
  112. * Use the array index as a unique id
  113. */
  114. public long getItemId(int position) {
  115. return position;
  116. }
  117. /**
  118. * Do not recycle a view if one is already there, if not the data could get corrupted and
  119. * the checkbox state could be lost.
  120. * @param convertView The old view to overwrite
  121. * @returns a CheckBoxifiedTextView that holds wraps around an CheckBoxifiedText */
  122. public View getView(int position, View convertView, ViewGroup parent ){
  123. return new ExtendedCheckBoxListView(mContext, mItems.get(position));
  124. }
  125. }