Back to project page Todoist-for-Android.
The source code is released under:
GNU General Public License
If you think the Android project Todoist-for-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/* This file is part of Todoist for Android?. /*from ww w . j av a 2 s .c o m*/ Todoist for Android? is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Todoist for Android? is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Todoist for Android?. If not, see <http://www.gnu.org/licenses/>. This file incorporates work covered by the following copyright and permission notice: Copyright [2010] pskink <pskink@gmail.com> Copyright [2010] ys1382 <ys1382@gmail.com> Copyright [2010] JonTheNiceGuy <JonTheNiceGuy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package com.android.applications.todoist.containers; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.Adapter; import android.widget.BaseAdapter; import android.widget.EditText; public class SeparatedListAdapter extends BaseAdapter { public final Map<String,Adapter> sections = new LinkedHashMap<String,Adapter>(); public final ArrayList<View> heads; public final static int TYPE_SECTION_HEADER = 0; public Context context; public SeparatedListAdapter(Context context) { this.context = context; heads = new ArrayList<View>(); } public void addSection(String section, Adapter adapter) { EditText newText = new EditText(this.context); newText.setText(section); this.heads.add(newText); this.sections.put(section, adapter); } public Object getItem(int position) { for(Object section : this.sections.keySet()) { Adapter adapter = sections.get(section); int size = adapter.getCount() + 1; // check if position inside this section if(position == 0) return section; if(position < size) return adapter.getItem(position - 1); // otherwise jump into next section position -= size; } return null; } public int getCount() { // total together all sections, plus one for each section header int total = 0; for(Adapter adapter : this.sections.values()) total += adapter.getCount() + 1; return total; } public int getViewTypeCount() { // assume that headers count as one, then total all sections int total = 1; for(Adapter adapter : this.sections.values()) total += adapter.getViewTypeCount(); return total; } public int getItemViewType(int position) { int type = 1; for(Object section : this.sections.keySet()) { Adapter adapter = sections.get(section); int size = adapter.getCount() + 1; // check if position inside this section if(position == 0) return TYPE_SECTION_HEADER; if(position < size) return type + adapter.getItemViewType(position - 1); // otherwise jump into next section position -= size; type += adapter.getViewTypeCount(); } return -1; } public boolean areAllItemsSelectable() { return false; } public boolean isEnabled(int position) { return (getItemViewType(position) != TYPE_SECTION_HEADER); } @Override public View getView(int position, View convertView, ViewGroup parent) { int sectionnum = 0; for(Object section : this.sections.keySet()) { Adapter adapter = sections.get(section); int size = adapter.getCount() + 1; // check if position inside this section if(position == 0) { return heads.get(sectionnum); } if(position < size) return adapter.getView(position - 1, convertView, parent); // otherwise jump into next section position -= size; sectionnum++; } return null; } @Override public long getItemId(int position) { return position; } }