If you think the Android project example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/*
* ******************************************************************************
* Copyright (c) 2013 Gabriele Mariotti.
*/*fromwww.java2s.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 it.gmariotti.cardslib.library.internal.base;
import android.content.Context;
import android.database.Cursor;
import android.widget.CursorAdapter;
import it.gmariotti.cardslib.library.R;
import it.gmariotti.cardslib.library.internal.Card;
/**
* Base Cursor Adapter
*
* @author Gabriele Mariotti (gabri.mariotti@gmail.com)
*/publicabstractclass BaseCardCursorAdapter extends CursorAdapter {
/**
* Current context
*/protected Context mContext;
/**
* Default layout used for each row
*/protectedint mRowLayoutId = R.layout.list_card_layout;
/**
* Used to set the viewTypeCount
*/protectedint innerviewTypeCount=1;
// -------------------------------------------------------------
// Constructors
// -------------------------------------------------------------
/**
* Constructor
*
* @param context The current context.
*/protected BaseCardCursorAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
mContext = context;
}
protected BaseCardCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mContext = context;
}
// -------------------------------------------------------------
// Views
// -------------------------------------------------------------
@Override
publicint getViewTypeCount() {
return innerviewTypeCount;
}
@Override
publicint getItemViewType(int position) {
Card card = (Card) getItem(position);
return card.getType();
}
@Override
publicboolean isEnabled(int position) {
//Disable card if it is not clickable or longClickable
Card card = (Card) getItem(position);
if (card.isClickable() || card.isLongClickable())
return true;
elsereturn false;
}
@Override
public Card getItem(int position) {
Object obj = super.getItem(position);
if (obj instanceof Cursor)
return (Card) getCardFromCursor((Cursor) obj);
elsereturn null;
}
/**
* You should implement this method to
* @param cursor
* @return
*/protectedabstract Card getCardFromCursor(Cursor cursor);
// -------------------------------------------------------------
// Getters and Setters
// -------------------------------------------------------------
/**
* Returns current context
*
* @return current context
*/public Context getContext() {
return mContext;
}
/**
* Sets layout resource ID used by rows
*
* @param rowLayoutId layout resource id
*/publicvoid setRowLayoutId(int rowLayoutId) {
this.mRowLayoutId = rowLayoutId;
}
/**
* Sets the viewTypeCount inside the adapter.
* It is very important in a adapter with different inner layouts
*
* @param viewTypeCount
*/publicvoid setInnerViewTypeCount(int viewTypeCount) {
this.innerviewTypeCount = viewTypeCount;
}
}