Last active
August 29, 2015 14:14
-
-
Save andreiverdes/c8229a68dc8ab33dec2e to your computer and use it in GitHub Desktop.
Faster alternative for OrmLiteCursorAdapter with OrmLite 4.48
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.ormlite.cursor; | |
| /** | |
| * Created by andrei on 27/01/15. | |
| */ | |
| import android.content.Context; | |
| import android.database.Cursor; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.CursorAdapter; | |
| import com.okit.app.android.widget.bindable.IBindable; | |
| import java.util.HashMap; | |
| public abstract class AwesomeCursorAdapter<T, ViewType extends View & IBindable<T>> extends CursorAdapter{ | |
| public static final int MODEL_TAG_ID = "MODEL_TAG_ID".hashCode(); | |
| private HashMap<Integer, T> mPositionsMap; | |
| public AwesomeCursorAdapter(Context context) { | |
| super(context, null, false); | |
| this.mPositionsMap = new HashMap<>(); | |
| } | |
| @SuppressWarnings("unchecked") | |
| @Override public View getView(int position, View convertView, ViewGroup parent) { | |
| View view = super.getView(position, convertView, parent); | |
| this.mPositionsMap.put(position % getCursor().getCount(), ((T) view.getTag(MODEL_TAG_ID))); | |
| return view; | |
| } | |
| @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { | |
| ViewType view = getNewView(context, cursor, parent); | |
| view.setTag(MODEL_TAG_ID, getNewModel()); | |
| return view; | |
| } | |
| @SuppressWarnings("unchecked") | |
| @Override public final void bindView(View itemView, Context context, Cursor cursor) { | |
| ViewType itemViewType = (ViewType) itemView; | |
| T t = ((T) itemViewType.getTag(MODEL_TAG_ID)); | |
| this.mapCursorToModel(cursor, t); | |
| itemViewType.bindView(context, t); | |
| } | |
| @Override public T getItem(int position) { | |
| return mPositionsMap.get(position % getCursor().getCount()); | |
| } | |
| /** | |
| * Here is where you map the cursor values to your model | |
| * @return The built and initialized model | |
| */ | |
| protected abstract void mapCursorToModel(Cursor pCursor, T pObject); | |
| /** | |
| * Should return a new View to be bond with the Model | |
| * @return | |
| */ | |
| protected abstract ViewType getNewView(Context context, Cursor cursor, ViewGroup parent); | |
| /** | |
| * Should return a new Model | |
| * @return a new model | |
| */ | |
| protected abstract T getNewModel(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.ormlite.cursor; | |
| import android.content.Context; | |
| import android.util.AttributeSet; | |
| import android.widget.LinearLayout; | |
| import android.widget.TextView; | |
| import com.okit.app.android.model.account.DeviceModel; | |
| import com.okit.app.android.widget.bindable.IBindable; | |
| /** | |
| * Created by andrei on 30/01/15. | |
| */ | |
| public class DeviceItemView extends LinearLayout implements IBindable<DeviceModel> { | |
| private TextView mNameTextView; | |
| private TextView mIdTextView; | |
| public DeviceItemView(Context context) { | |
| super(context); | |
| } | |
| public DeviceItemView(Context context, AttributeSet attrs) { | |
| super(context, attrs); | |
| } | |
| public DeviceItemView(Context context, AttributeSet attrs, int defStyleAttr) { | |
| super(context, attrs, defStyleAttr); | |
| } | |
| public void init(Context pContext){ | |
| //inflate your view here and find the children views; | |
| inflate(pContext, R.layout.your_awesome_device_item_view_layout, null); | |
| this.mNameTextView = (TextView)this.findViewById(R.id.your_awesome_device_name_text_view); | |
| this.mIdTextView = (TextView)this.findViewById(R.id.your_awesome_device_id_text_view); | |
| } | |
| @Override public void bindView(Context pContext, DeviceModel pModel) { | |
| //here map your model to the view and do the actual binding... | |
| this.mNameTextView.setText(pModel.getName()); | |
| this.mIdTextView.setTag(pModel.getIid()); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.ormlite.cursor; | |
| import android.content.Context; | |
| import android.database.Cursor; | |
| import android.view.ViewGroup; | |
| import com.okit.app.android.model.account.DeviceModel; | |
| import com.okit.app.android.orm.AwesomeCursorAdapter; | |
| /** | |
| * Created by andrei on 30/01/15. | |
| */ | |
| public class DeviceCursorAdapter extends AwesomeCursorAdapter<DeviceModel, DeviceItemView> { | |
| public DeviceCursorAdapter(Context context) { | |
| super(context); | |
| } | |
| @Override protected void mapCursorToModel(Cursor pCursor, DeviceModel pDeviceModel) { | |
| pDeviceModel.setName(pCursor.getString(pCursor.getColumnIndex("name"))); | |
| pDeviceModel.setId(pCursor.getLong(pCursor.getColumnIndex("id"))); | |
| //...here you can map your cursor to the model | |
| } | |
| @Override public DeviceItemView getNewView(Context context, Cursor cursor, ViewGroup parent) { | |
| return new DeviceItemView(context); | |
| } | |
| @Override public DeviceModel getNewModel() { | |
| return new DeviceModel(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.ormlite.cursor; | |
| import android.content.Context; | |
| /** | |
| * Created by andrei on 26/01/15. | |
| * Copyright (c) Greenchili BV 2014. All rights reserved. | |
| */ | |
| public interface IBindable<T> { | |
| public void bindView(Context pContext, T pModel); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment