【问题标题】:Using Custom Adapter with CursorLoader将自定义适配器与 CursorLoader 一起使用
【发布时间】:2015-05-04 20:17:21
【问题描述】:

我正在尝试使用CursorLoader 从 UI 线程的ContentProvider 中获取数据。然后我用它来填充我的列表视图。我之前使用过SimpleCursorAdapter,一切正常。但是现在我想根据数据对列表视图行有不同的视图。

所以我写了一个自定义适配器扩展了基础适配器

public class CustomAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mInflater;

    public CustomAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Log.d("CustomAdapter", "Check" + i + 1);
        if (view == null) {
            view = mInflater.inflate(R.layout.listview_text_layout, viewGroup, false);
            //if(text) load text view
            //else load image view
        }

        return view;
    }
}

但是要显示任何内容,getCount() 方法应该返回一个大于0 的值。

如何获取CursorLoader 加载的项目数以便显示所有元素?目前,我只是返回10 使其工作,但这显然不是正确的方法。

这是我的Fragment 类,它实现了CursorLoader

public class MessageFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {

    private AbsListView mListView;
    private CustomAdapter mAdapter;
    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);

        getLoaderManager().initLoader(MESSAGES_LOADER, null, this);

        // Set the adapter
        mListView = (AbsListView) view.findViewById(android.R.id.list);
        mListView.setAdapter(mAdapter);

        return view;
    }


    @Override
    public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
        String[] projection = {MessageEntry._ID, MessageEntry.MESSAGE_DATA, MessageEntry.MESSAGE_TIMESTAMP};

        switch (i) {
            case MESSAGES_LOADER:
                return new CursorLoader(
                        getActivity(),
                        Uri.parse("content://com.rubberduck.dummy.provider/messages"),
                        projection,
                        null,
                        null,
                        null
                );
            default:
                return null;
        }
    }
}

另外,在我的getView() 方法中,我需要访问数据以便选择要膨胀的布局。我知道我们可以将数据列表传递给自定义适配器,但是当数据实际由CursorLoader 加载时,我们该怎么做呢?

【问题讨论】:

    标签: android baseadapter custom-adapter android-cursor android-cursorloader


    【解决方案1】:

    您可以扩展CursorAdapter,而不是扩展BaseAdapter

    onLoadFinished 被调用时,您只需调用swapCursor。您不需要覆盖getCount()super 已经负责返回正确的值。

    【讨论】:

    • 扩展 CursorAdapter 的类在实例化时需要一个 Cursor 对象。那我应该在哪里实例化我的 CustomCursorAdapter 类?我应该在 onLoadFinished() 函数中这样做吗?
    • 实例化适配器 onCreateView 就像你现在正在做的那样,将 null 传递给光标。将引用存储在类成员中。当 onLoadFinished 被调用时,调用mAdapter.swapCursor(cursor)
    【解决方案2】:

    SimpleCursorAdapterViewBinder 设置或CursorAdapter 足以满足我的要求,你不需要扩展BaseAdapter

    一旦你通过LoaderManager初始化加载器,你将通过onLoadFinished()方法得到结果。

    在适配器上调用swapCursor()方法刷新数据

    另请注意,ActivityonStart() 中启动所有已注册的加载程序,因此启动加载程序的替代位置将在onActivityCreated()

    本教程描述了@Blackbelt 和我所描述的方式:

    http://code.tutsplus.com/tutorials/android-fundamentals-properly-loading-data--mobile-5673

    一旦您深入了解加载器,我建议您阅读这篇文章:

    http://chalup.github.io/blog/2014/06/12/android-loaders/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      相关资源
      最近更新 更多