AlphabetIndexer with Custom Adapter managed by LoaderManager
So I finally got this to work. Here's how i did it:
I added:
ListView lv = getListView();
lv.setFastScrollEnabled(true);
lv.setScrollingCacheEnabled(true);
to the onLoadFinished()
method after the new cursor was swapped in like so
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in. (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
ListView lv = getListView();
lv.setFastScrollEnabled(true);
lv.setScrollingCacheEnabled(true);
}
consequently these three statements were removed from the onActivityCreated()
method of my custom ListFragment
.
Getting NullPointerException with AlphabetIndexer
You should probably post more of your code to confirm this, but I'm guessing you cursos has changed, or for some other reason cursor[columnIndex] returns null, by the time getSectionForPosition
is evaluated.
According to this outstanding issue, this situation will trigger a very similar NPE to what you are experiencing
Again, the real problem is somewhere in the code that you didn't post. The Alphabetindexer is just not handling that situation all too well.
Styling the sectionIndexer
Found the answer here
You have to create a theme and apply it to an activity
Android: how to use SectionIndexer
You can try to write a custom ArrayAdapter
and basically return a "section header" view in the getView(...)
method for the positions where headers should appear.
You'll also have to overwrite getViewTypeCount ()
to return the number of new types of views (in this case 2) and getItemViewType (int position)
to return the type of view for the current position.
Also, the onItemClickListener
should check to see if the item you clicked on is a section header.
This is my custom array adapter:
public class ItemListAdapter extends ArrayAdapter<ModelItem>
{
private static final int TYPE_SECTION_HEADER = 0;
private static final int TYPE_LIST_ITEM = 1;
int mDefaultRowLayoutResID;
Context mContext;
LayoutInflater mInflater;
ArrayList<ModelItem> lItems;
public ItemListAdapter(Context context, int resource, ArrayList<ModelItem> items)
{
super(context, resource, items);
mContext = context;
mResource = resource;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
lItems = items;
}
@Override
public ModelItem getItem(int position)
{
return lItems.get(position);
}
@Override
public int getCount()
{
return lItems.size();
}
@Override
public int getViewTypeCount()
{
return 2;
}
@Override
public int getItemViewType(int position)
{
ModelItem item = lItems.get(position);
if (item.isHeader())
{
return TYPE_SECTION_HEADER;
}
else
{
return TYPE_LIST_ITEM;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
ModelItem item = getItem(position);
if (convertView == null)
{
if (item.isHeader())
{
convertView = mInflater.inflate(R.layout.row_item_section_header, null);
holder = new ViewHolder();
holder.title = (TextView)convertView.findViewById(R.id.list_header_title);
holder.subtitle = null;
convertView.setTag(holder);
}
else
{
convertView = mInflater.inflate(R.layout.row_item_default, null);
holder = new ViewHolder();
holder.title = (TextView)convertView.findViewById(R.id.row_item_title);
holder.subtitle = (TextView)convertView.findViewById(R.id.row_item_subtitle);
convertView.setTag(holder);
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(item.getTitle());
if (holder.subtitle != null)
{
holder.subtitle.setText(item.getSubtitle());
}
return convertView;
}
private class ViewHolder
{
public TextView title;
public TextView subtitle;
public ImageView leftIcon;
public View rightControl;
}
}
This is the row_item_default.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/row_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/row_item_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/row_item_title"
/>
</RelativeLayout>
and this is the row_item_section_header.xml:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_header_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/listSeparatorTextViewStyle"
/>
The ModelItem class is a simple container for title, subtitle and a boolean to tell if it's a section header or not.
This is not the only way to write this adapter but I hope this helps.
AlphabetIndexer setCursor doesn't update its cache
I've solved this by calling this in sequence after I set the adapter:
listView.setFastScrollEnabled(false);
listView.setFastScrollEnabled(true);
That worked for me.
Related Topics
Phonegap - Add a Splash Screen for Android App
Windowsoftinputmode="Adjustresize" Not Working with Translucent Action/Navbar
How to Draw Free Hand Polygon in Google Map V2 in Android
How to Keep/Exclude a Particular Package Path When Using Proguard
HTML List Tag Not Working in Android Textview. What How to Do
How to Send an Email with a File Attachment in Android
Switching Application-Wide Theme Programmatically
"Native Typeface Cannot Be Made" Only for Some People
Searchview in Listview Having a Custom Adapter
How to Start Service-Only Android App
How to Set Edittext to Only Accept Numeric Values in Android
Change Activity's Theme Programmatically
Multi-Line Edittext with Done Action Button
How to Convert Dp, Px, Sp Among Each Other, Especially Dp and Sp
Spannablestring with Image Example
How to Enable Tls 1.2 Support in an Android Application (Running on Android 4.1 Jb)