• Stars
    star
    110
  • Rank 316,770 (Top 7 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 10 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SectionCursorAdapter Β Β  Build Status for master

SectionCursorAdapter adds sections and fast scroll to CursorAdapter as an easily implementable feature. A blog post on the implementation rational can be found on toastdroid.com

SectionCursorAdapter 2.0

2.0 adds two new adapters and handles all recycling for you. For reasons which are specified in the 2.0 readme this version will not take over as master but none the less 1.0 is deprecated and it is highly recommended that you use 2.0. Follow this link to go to the 2.0 Master branch.

sections dialog

Download

Sample
Android app on Google Play

If you are using maven add to your pom file:

<dependency>
    <groupId>com.twotoasters.SectionCursorAdapter</groupId>
    <artifactId>library</artifactId>
    <version>1.0.1</version>
</dependency>

or if you are using Gradle:

dependencies {
    compile 'com.twotoasters.SectionCursorAdapter:library:1.0.+'
}

Basics

SectionCursorAdapter is implemented in a similar way to Android's CursorAdapter which it extends. Instead of having newView and bindView the SectionCursorAdpater uses newSectionView and bindSectionView plus newItemView and bindItemView.

There is one additional abstract method Object getSectionFromCursor(Cursor cursor). This is the method tells the adapter how to remap the cursor positions to allow for sections. The object which is returned is then passed through to newSectionView and bindSectionView. You can make an alphabitical adapter with the following method. Noob tip: You will have to sort alphabitically when querying your database for your cursor.

@Override
protected Object getSectionFromCursor(Cursor cursor) {
    int columnIndex = cursor.getColumnIndex(StoreModel.NAME);
    String name = cursor.getString(columnIndex);
    return name.toUpperCase().substring(0, 1);
}

If you prefer to use the onItemClickListenter instead of using click listeners when binding your views you'll more then likely need to convert your position to a cursorPosition.

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MyAdapter adapter = (MyAdapter) parent.getAdapter();
        Object sectionObject = adapter.getItem(position);
        int cursorPosition = adapter.getCursorPositionWithoutSections(position);

        if (adapter.isSection(position) && sectionObject != null) {
            // Handle the section being clicked on.
            Log.i("SCA", "Section: " + sectionObject);
        } else if (cursorPosition != SectionCursorAdapter.NO_CURSOR_POSITION) {
            // Handle the cursor item being clicked on.
            Log.i("SCA", "CursorPosition: " + cursorPosition);
        }
    }
});

Advanced

To build sections in a more advanced way you can override buildSections. The following is an example for how to build a simple alphabitical map.

@Override
protected SortedMap<Integer, Object> buildSections(Cursor cursor) {
    TreeMap<Integer, Object> sections = new TreeMap<>();
    int columnIndex = cursor.getColumnIndex(StoreModel.NAME);
    int cursorPosition = 0;
        
    while (cursor.moveToNext()) {
        String name = cursor.getString(columnIndex);
        String section = name.toUpperCase().substring(0, 1);
        if (!sections.containsValue(section)) {
            sections.put(cursorPosition + sections.size(), section);
        }
        cursorPosition++;
    }
    return sections;
}

You can give a custom object as a value in the map instead of a number or string. To use the fast scroll with this object override toString. This will allow you to control what is displayed in the fast scroll dialog. Note that in versions of Android before KitKat this dialog does not resize to fit content. SectionCursorAdapter by default only allows a maximum of 3 characters in this dialog on these older version of Android, but by overriding getMaxIndexerLength() the length can be whatever you choose.

License

Copyright 2014 Two Toasters

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.

More Repositories

1

JazzyListView

Java
935
star
2

TWTSideMenuViewController

Side Menus for iOS 7
Objective-C
851
star
3

TTSwitch

Fully customizable switch for iOS using images
Objective-C
432
star
4

clusterkraf

A clustering library for the Google Maps Android API v2
Java
255
star
5

multi-column-list-adapter

Java
100
star
6

TTAlertView

Objective-C
82
star
7

watchface-template

Java
79
star
8

Toast

Tools and Utilities for Cocoa Development
Objective-C
67
star
9

RecyclerViewLib

An example implementation of Android's new RecyclerView.
Java
63
star
10

HorizontalImageScroller-Android

A horizontal image scroller widget for Android
Java
50
star
11

MaterialRangeSlider

Material inspired range slider
Java
42
star
12

Wear-MessageApiDemo

Java
36
star
13

zbar

git clone of zbar sdk
Objective-C
35
star
14

AnimatedPath

Objective-C
33
star
15

AndroidStyleGuidelines

31
star
16

AndrOAuth

Java
29
star
17

hoot

A powerful, flexible, lightweight Android library for dealing with RESTful endpoints.
Java
26
star
18

watchface-gears

Java
20
star
19

UISpec

Git SVN Clone of UISpec
Objective-C
19
star
20

UISpecRunner

Flexible CLI test runner for UISpec
Ruby
16
star
21

FramerateDemo

Objective-C
11
star
22

toastdroid

Java
9
star
23

HelloAndroidStudio

Java
8
star
24

objective_stack

A Rails template used by the fine folks at Two Toasters
Ruby
7
star
25

TWTCommon

Misc. downright useful classes & additions for iphone development (with three 20)
Objective-C
7
star
26

project-example

Example repository demonstrating the twotoasters development process
6
star
27

Android-Master-Class-ListenerAwareAsyncTask

Java
6
star
28

fbconnect-iphone

Facebook Connect for iPhone
Objective-C
5
star
29

AnalyticsTest

Java
5
star
30

chron

Java
2
star
31

resource_controller_extensions

Provides reasonable default handlers for XML, JSON, and FBML to ResourceController
Ruby
2
star
32

iPhone-Build-Scripts

client build and release build scripts for iphone projects
Ruby
1
star
33

facebook-ios-sdk-framework

*Unofficial* build of the Facebook iOS SDK Framework
Objective-C
1
star
34

RKCatalog-Server

Simple Sinatra backend for the RestKit RKCatalog Sample app
Ruby
1
star
35

brominet

Objective-C
1
star
36

Xcode-Templates

Objective-C
1
star
37

Volley

Java
1
star