PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/portalis-client-androlis/client-androlis/src/com/androlis/list/List_MyCustomAdapter.java

https://bitbucket.org/vincentpiet/portalis
Java | 279 lines | 193 code | 60 blank | 26 comment | 15 complexity | 8a68f28560b822ce85b84e4b0b0ce2e0 MD5 | raw file
  1. package com.androlis.list;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.Locale;
  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.CompoundButton.OnCheckedChangeListener;
  13. import android.widget.Filter;
  14. import android.widget.Filterable;
  15. import android.widget.TextView;
  16. import com.androlis.R;
  17. public class List_MyCustomAdapter extends BaseAdapter implements Filterable {
  18. private static final int TYPE_ITEM = 0;
  19. private static final int TYPE_SEPARATOR = 1;
  20. private static final int TYPE_CHECKABLE_ITEM = 2;
  21. private static final int TYPE_MAX_COUNT = 3;
  22. private Filter customFilter;
  23. //Contain separator position to acces their view in the ListView create from this current adapter
  24. private List<Integer> _separatorPosition = new ArrayList<Integer>();
  25. //Contain all items of the list
  26. private List<List_Item> _originalData = new ArrayList<List_Item>();
  27. //Contain only visible items
  28. private List<List_Item> _filteredData = new ArrayList<List_Item>();
  29. //Indicate if the checkboxs of all data are checked
  30. private List<Boolean> _originalCheckBox = new ArrayList<Boolean>();
  31. //Indicate if a visible checkbox is checked
  32. private List<Boolean> _filteredCheckBox = new ArrayList<Boolean>();
  33. private LayoutInflater _inflater;
  34. private Context _ctx;
  35. private CheckBox checkbox = null;
  36. private int nbItems=0;
  37. public List_MyCustomAdapter(Context ctx) {
  38. _inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  39. _ctx=ctx;
  40. }
  41. public void addItem(String item) {
  42. List_Item itemList = new List_Item(item, TYPE_ITEM, nbItems);
  43. nbItems++;
  44. _originalCheckBox.add(false);
  45. _filteredCheckBox.add(false);
  46. _originalData.add(itemList);
  47. _filteredData.add(itemList);
  48. notifyDataSetChanged();
  49. }
  50. public void addSeparatorItem(String item) {
  51. _separatorPosition.add(_originalData.size());
  52. List_Item itemList = new List_Item(item, TYPE_SEPARATOR,nbItems);
  53. nbItems++;
  54. _originalCheckBox.add(false);
  55. _filteredCheckBox.add(false);
  56. _originalData.add(itemList);
  57. _filteredData.add(itemList);
  58. notifyDataSetChanged();
  59. }
  60. public void addCheckableItem(String item) {
  61. List_Item itemList = new List_Item(item, TYPE_CHECKABLE_ITEM, nbItems);
  62. nbItems++;
  63. _originalCheckBox.add(false);
  64. _filteredCheckBox.add(false);
  65. _originalData.add(itemList);
  66. _filteredData.add(itemList);
  67. notifyDataSetChanged();
  68. }
  69. /** Return names of checked items */
  70. public ArrayList<Integer> getChecked() {
  71. ArrayList<Integer> objectsSelected = new ArrayList<Integer>();
  72. for(int i=0; i<_originalCheckBox.size(); i++)
  73. if(_originalCheckBox.get(i))
  74. objectsSelected.add(i);
  75. return objectsSelected;
  76. }
  77. /** Uncheck all items */
  78. public void unCheck() {
  79. for(int i=0; i<_originalCheckBox.size(); i++)
  80. if(_originalCheckBox.get(i)) {
  81. checkbox = (CheckBox) getView(i, checkbox, null).findViewById(R.id.list_checkable_item_checkbox);
  82. checkbox.performClick();
  83. }
  84. //Notify changes to update the view otherwise checkboxs remain checked
  85. notifyDataSetChanged();
  86. }
  87. public void clean() {
  88. _separatorPosition = new ArrayList<Integer>();
  89. _originalData = new ArrayList<List_Item>();
  90. _filteredData = new ArrayList<List_Item>();
  91. _originalCheckBox = new ArrayList<Boolean>();
  92. _filteredCheckBox = new ArrayList<Boolean>();
  93. nbItems=0;
  94. }
  95. @Override
  96. public int getItemViewType(int position) {
  97. return _originalData.get(position).getItemType();
  98. }
  99. @Override
  100. public int getViewTypeCount() {
  101. return TYPE_MAX_COUNT;
  102. }
  103. public int getCount() {
  104. return _filteredData.size();
  105. }
  106. public long getItemId(int position) {
  107. return position;
  108. }
  109. public List_Item getItem(int position) {
  110. return _filteredData.get(position);
  111. }
  112. public List<Integer> getSeparatorPosition() {
  113. return _separatorPosition;
  114. }
  115. /** Manage the visibility of each item by implicit call */
  116. public View getView(final int position, View convertView, ViewGroup parent) {
  117. final List_Item item = _filteredData.get(position);
  118. int type = item.getItemType();
  119. //Will contain information about how to display item according to his type
  120. TextView textView = new TextView(_ctx);
  121. //Select the right view according to item type
  122. switch (type) {
  123. case TYPE_ITEM:
  124. convertView = _inflater.inflate(R.layout.list_item, null);
  125. textView = (TextView)convertView.findViewById(R.id.list_item_text);
  126. break;
  127. case TYPE_SEPARATOR:
  128. convertView = _inflater.inflate(R.layout.list_separator, null);
  129. textView = (TextView)convertView.findViewById(R.id.textSeparator);
  130. break;
  131. case TYPE_CHECKABLE_ITEM:
  132. convertView = _inflater.inflate(R.layout.list_checkable_item, null);
  133. textView = (TextView)convertView.findViewById(R.id.list_checkable_item_text);
  134. //getView is called when the item becomes visible (after a scroll on the list for example)
  135. //we have to display the CheckBox in the right state
  136. checkbox = (CheckBox) convertView.findViewById(R.id.list_checkable_item_checkbox);
  137. checkbox.setChecked(_filteredCheckBox.get(position));
  138. //Is called when the state of the CheckBox changes (We don't need to implement OnClick...)
  139. checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  140. Boolean checked = _filteredCheckBox.get(position);
  141. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  142. _filteredCheckBox.set(position, (!checked));
  143. int originalposition =_filteredData.get(position).getId();
  144. _originalCheckBox.set(originalposition, (!checked));
  145. }
  146. });
  147. break;
  148. }
  149. convertView.setTag(textView);
  150. textView.setText(item.getItem());
  151. return convertView;
  152. }
  153. /** Add the possibility to filter items from list items */
  154. public Filter getFilter()
  155. {
  156. if(customFilter == null)
  157. customFilter=new CustomFilter();
  158. return customFilter;
  159. }
  160. private class CustomFilter extends Filter
  161. {
  162. /** Select items which contains the charSequence filter */
  163. @Override
  164. protected FilterResults performFiltering(CharSequence charSequence)
  165. {
  166. FilterResults results = new FilterResults();
  167. //If there is nothing to filter on, return the original data for your list
  168. if(charSequence == null || charSequence.length() == 0)
  169. {
  170. results.values =new Doublet(_originalData, _originalCheckBox);
  171. results.count = 2;
  172. }
  173. //Else we have to calculate the new list displayed and the checkbox list associated with it
  174. else
  175. {
  176. List<List_Item> filterResultsData = new ArrayList<List_Item>();
  177. List<Boolean> filterResultsCheckBox = new ArrayList<Boolean>();
  178. //Test each item from the original list with the charSequence filter
  179. for(int i=0; i<_originalData.size();i++){
  180. List_Item item =_originalData.get(i);
  181. if(filterConditions(item.getItem(), charSequence))
  182. {
  183. filterResultsData.add(item);
  184. filterResultsCheckBox.add(_originalCheckBox.get(item.getId()));
  185. }
  186. }
  187. results.values = new Doublet(filterResultsData, filterResultsCheckBox);
  188. results.count = 2;
  189. }
  190. return results;
  191. }
  192. /** Update the filtered list data and checkbox, and notify it for displaying it thanks to getView method */
  193. @SuppressWarnings("unchecked")
  194. @Override
  195. protected void publishResults(CharSequence charSequence, FilterResults filterResults)
  196. {
  197. Doublet doublet = (Doublet) filterResults.values;
  198. _filteredData=(List<List_Item>)doublet.arg0;
  199. _filteredCheckBox=(List<Boolean>)doublet.arg1;
  200. notifyDataSetChanged();
  201. }
  202. /**
  203. * Non case-sensitive filter which return true if item contains
  204. * a word starting by the charSequence filter
  205. * */
  206. private boolean filterConditions(String item, CharSequence filter) {
  207. String delims = "[ ]+";
  208. //Turns listItem to lower-case in the local alphabet and parses it to filter each word from it
  209. String[] tokens = item.toLowerCase(Locale.getDefault()).split(delims);
  210. //Turns filter to lower-case in the local alphabet
  211. String sFilter = filter.toString().toLowerCase(Locale.getDefault());
  212. for(String token : tokens){
  213. if(token.startsWith(sFilter))
  214. return true;
  215. }
  216. return false;
  217. }
  218. };
  219. }