ViewPager和数据库

编程入门 行业动态 更新时间:2024-10-27 23:21:14
本文介绍了ViewPager和数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

该数据是在数据库中,所以我通常使用的CursorAdapter并将它与一个ListView CursorLoader工作,但是现在我需要的,而不是一个ListView一个ViewPager而这需要一个PagerAdapter,我只有它已经看到了它的工作与名单。

The data is in a Database, so I would normally use CursorAdapter and have it work with CursorLoader on a ListView, however now I need a ViewPager instead of a ListView and that takes a PagerAdapter and I only have it have seen it work with lists.

有没有使用游标或生病必须使它工作?土生土长的寻呼机适配器

Is there a native pager adapter that uses a cursor or ill have to make it work?

谢谢!

推荐答案

根据的CursorAdapter我有一个延伸FragmentPagerAdapter与您将使用从光标传来的信息,唯一的区别来实例化的片段。

Based on the internals of the CursorAdapter I have an implementation that extends FragmentPagerAdapter with the only difference that you will use the information coming from a Cursor to instantiate a Fragment.

package com.example; import android.content.Context; import android.database.Cursor; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.util.SparseIntArray; import android.view.ViewGroup; import java.util.HashMap; public abstract class CursorFragmentPagerAdapter extends FragmentPagerAdapter { protected boolean mDataValid; protected Cursor mCursor; protected Context mContext; protected SparseIntArray mItemPositions; protected HashMap<Object, Integer> mObjectMap; protected int mRowIDColumn; public CursorFragmentPagerAdapter(Context context, FragmentManager fm, Cursor cursor) { super(fm); init(context, cursor); } void init(Context context, Cursor c) { mObjectMap = new HashMap<Object, Integer>(); boolean cursorPresent = c != null; mCursor = c; mDataValid = cursorPresent; mContext = context; mRowIDColumn = cursorPresent ? c.getColumnIndexOrThrow("_id") : -1; } public Cursor getCursor() { return mCursor; } @Override public int getItemPosition(Object object) { Integer rowId = mObjectMap.get(object); if (rowId != null && mItemPositions != null) { return mItemPositions.get(rowId, POSITION_NONE); } return POSITION_NONE; } public void setItemPositions() { mItemPositions = null; if (mDataValid) { int count = mCursor.getCount(); mItemPositions = new SparseIntArray(count); mCursor.moveToPosition(-1); while (mCursor.moveToNext()) { int rowId = mCursor.getInt(mRowIDColumn); int cursorPos = mCursor.getPosition(); mItemPositions.append(rowId, cursorPos); } } } @Override public Fragment getItem(int position) { if (mDataValid) { mCursor.moveToPosition(position); return getItem(mContext, mCursor); } else { return null; } } @Override public void destroyItem(ViewGroup container, int position, Object object) { mObjectMap.remove(object); super.destroyItem(container, position, object); } @Override public Object instantiateItem(ViewGroup container, int position) { if (!mDataValid) { throw new IllegalStateException("this should only be called when the cursor is valid"); } if (!mCursor.moveToPosition(position)) { throw new IllegalStateException("couldn't move cursor to position " + position); } int rowId = mCursor.getInt(mRowIDColumn); Object obj = super.instantiateItem(container, position); mObjectMap.put(obj, Integer.valueOf(rowId)); return obj; } public abstract Fragment getItem(Context context, Cursor cursor); @Override public int getCount() { if (mDataValid) { return mCursor.getCount(); } else { return 0; } } public void changeCursor(Cursor cursor) { Cursor old = swapCursor(cursor); if (old != null) { old.close(); } } public Cursor swapCursor(Cursor newCursor) { if (newCursor == mCursor) { return null; } Cursor oldCursor = mCursor; mCursor = newCursor; if (newCursor != null) { mRowIDColumn = newCursor.getColumnIndexOrThrow("_id"); mDataValid = true; } else { mRowIDColumn = -1; mDataValid = false; } setItemPositions(); notifyDataSetChanged(); return oldCursor; } @Override public long getItemId(int position) { if (!mDataValid || !mCursor.moveToPosition(position)) { return super.getItemId(position); } int rowId = mCursor.getInt(mRowIDColumn); return rowId; } }

如果你需要比片段的别的东西,你可以很容易地改变code您的需求。

If you need something else than Fragment's you can easily alter the code to your needs.

更多推荐

ViewPager和数据库

本文发布于:2023-07-16 07:32:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   ViewPager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!