Android SimpleCursorAdapter 到 CursorAdapter

标签 android simplecursoradapter android-cursorloader

我正在使用 SimpleCursorAdapter 来管理我对数据库的查询,然后在 ListView 中显示结果。

这是我的代码:

database.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor c = database.getData();
startManagingCursor(c);


String[] from = new String[]{Database._GROUP_NAME};
int[] to = new int[]{android.R.id.text1};

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, query, from, to);

listContent.setAdapter(adapter);

database.close();

虽然乍一看,它按预期工作,但我认为这不应该是继续进行的方式,因为有 CursorAdapter

我知道这是一个菜鸟问题,但由于现在才开始在 Android 中编程,所以我仍然不太了解。

如何将它从使用 SimpleCursorAdapter 传递给 CursorAdapter? 已经在互联网上搜索过,但无法理解如何做到这一点。

不问代码只是一些方向。

谢谢

蚕 bean

根据主要评论更新

database.open();
ListView listContent = (ListView)findViewById(android.R.id.list);
Cursor c = database.getData();
MyAdapt cursorAdapter = new MyAdapt(this, query);


listContent.setAdapter(adapter);

database.close();

MyAdapt 类:

public class MyAdapt extends CursorAdapter {

    private final LayoutInflater mInflater;

    public MyAdapt(Context context, Cursor c) {
        super(context, c);
        mInflater = LayoutInflater.from(context);
        mContext = context;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView groupName = (TextView) view.findViewById(android.R.id.text1);
        groupName.setText(cursor.getString(cursor
                .getColumnIndex(Database._GROUP_NAME)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        final View view = mInflater.inflate(
                android.R.layout.simple_list_item_1, parent, false);
        return view;
    }
}

这是正确的方法吗?

蚕 bean

最佳答案

SimpleCursorAdapter 是可用于自定义适配器的最简单的适配器形式。 当您不需要任何自定义时,您应该只使用 SimpleCursorAdapter。

关于Android SimpleCursorAdapter 到 CursorAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11720100/

相关文章:

java - 获取数组的索引

java - 无法将数据写入android中保存的文件中

android - 如何向 SimpleCursorAdapter 添加项目?

android - rxJava 和改造而不是 IntentService + CursorLoader?

android - 如何使用 CursorAdapter 和 LoaderCallbacks 在 fragment 中实现 ContentObserver?

android - 如何从 CursorLoader 刷新光标?

java - 短信接收器不起作用

android - 未收到 MixPanel 推送通知

android listview项目高度

android - 我如何使用 onListItem 来启动具有 ListView 值的新 Activity ?