【问题标题】:Android CursorLoaded with custom BaseAdapter带有自定义 BaseAdapter 的 Android CursorLoaded
【发布时间】:2012-07-09 12:53:46
【问题描述】:

我一直在寻找如何做到这一点,但我的发现还很短。

我在我的应用程序中实现了一个 ContentProvider。我的片段正在使用 CursorLoader 回调来查询 ContentProvider。

SimpleCursorAdapter 通常用于呈现结果,但是,我想创建一个自定义基本适配器,以便在显示时使用节标题修改这些结果。

我的总体问题是如何将光标的结果传递到自定义 BaseAdapter 中?

谢谢。

【问题讨论】:

    标签: android android-contentprovider android-cursoradapter


    【解决方案1】:

    我要做的是创建一个从 CursorAdapter 扩展的 CustomCursorAdapter。

    这是一个你可以使用的例子:)

    public class ContactListCustomCursorAdapter extends CursorAdapter {
    
    private static final String TAG = ContactListCustomCursorAdapter.class.getName();
    private LayoutInflater mInflater;
    
    public ContactListCustomCursorAdapter(Context context) {
        super(context, null, false);
        mInflater = LayoutInflater.from(context);
    }
    

    在构造函数中,您可以获取 layoutInflater 以便稍后为每个视图充气。

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        contactName = cursor.getString(cursor
                .getColumnIndex(Contacts.DISPLAY_NAME));
        currentId = cursor.getLong(cursor.getColumnIndex(Contacts._ID));
        currentChar = contactName.substring(0, 1);
        ViewHolder mHolder = (ViewHolder) view.getTag();
    
        if (cursor.isFirst()) {
            mHolder.header.setVisibility(View.VISIBLE);
            mHolder.charHeader.setText(currentChar);
            mHolder.fistContactBackground.setVisibility(View.VISIBLE);
            mHolder.normalBackground.setVisibility(View.GONE);
        } else {
            cursor.moveToPrevious();
            previousChar = cursor.getString(
                    cursor.getColumnIndex(Contacts.DISPLAY_NAME)).substring(0,
                    1);
            if (collator.compare(currentChar, previousChar) != 0) {
                mHolder.header.setVisibility(View.VISIBLE);
                mHolder.charHeader.setText(currentChar);
                mHolder.fistContactBackground.setVisibility(View.VISIBLE);
                mHolder.normalBackground.setVisibility(View.GONE);
            } else {
                mHolder.header.setVisibility(View.GONE);
                mHolder.fistContactBackground.setVisibility(View.GONE);
                mHolder.normalBackground.setVisibility(View.VISIBLE);
            }
            cursor.moveToNext();
        }
    }
    

    在绑定视图中,我这样做是为了知道一行是否与之前的名称不同,以及是否显示分隔标题。

    @Override
    public Object getItem(int position) {
    }
    

    在 getItem 上,您可以返回一个对象或外部操作所需的内容,例如列表元素上的 clic。

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = mInflater.inflate(R.layout.contact_row, parent, false);
        ViewHolder refHolder = new ViewHolder();
        refHolder.name = (TextView) view
                .findViewById(R.id.contact_row_name_textView);
        refHolder.setContactPhoto((ImageView) view
                .findViewById(R.id.contact_row_photo_ImageView));
        refHolder.header = (LinearLayout) view
                .findViewById(R.id.contact_row_separator);
        refHolder.charHeader = (TextView) view
                .findViewById(R.id.contact_separator_char_textview);
        refHolder.normalBackground = view.findViewById(R.id.normal_background);
        refHolder.fistContactBackground = view
                .findViewById(R.id.first_contact_background);
        view.setTag(refHolder);
        return view;
    }
    
    public static class ViewHolder implements ViewHolderInterface {
        private LinearLayout header;
        private TextView name;
        private ImageView contactPhoto;
        private TextView charHeader;
        private View normalBackground;
        private View fistContactBackground; 
    }
    

    viewHolder 是为了加快视图搜索,在 newView 上设置组件并将它们存储在 Tag Object 中。

    在您的活动或片段上,只需在加载时设置光标。

        @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor currentCursor) {
        contactsAdapter.changeCursor(currentCursor);
    }
    

    现在你可以过滤或任何你想要的,光标将由加载器处理:)

    问候。

    【讨论】:

    • 谢谢。我会看看你发布的内容。但是,据我所知,您使用空游标构造了新的 CursorAdapter。如何引用片段中存在的光标?它是通过上下文假设的吗?
    • 没关系,我在 onLoadFinished() 中错过了上面的部分。我认为那是我缺少的部分。
    • 谢谢。这正是我想要的。
    • 这很好用,但我认为可能存在问题。我每个部分的计数似乎减少了 2。仅供参考。我会更深入地研究它
    • 计数部分是什么意思?你能进一步解释一下吗? :)
    【解决方案2】:

    创建一个扩展 BaseAdapter 的新适配器。然后在您的适配器中声明一个字段 private Cursor c 并通过类构造函数将您的光标传递给此适配器。在您的适配器中实现必要的方法,仅此而已。 Here,例如,您可以阅读如何实现自定义 BaseAdapter。此外,您可以看看 AOSP 中 SimpleCursorAdapter 的实现。

    【讨论】:

    • 谢谢。我将看一下 SimpleCursorAdapter 源代码。
    猜你喜欢
    • 2013-12-01
    • 2015-09-28
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    相关资源
    最近更新 更多