Android 自定义SimpleCursorAdapter的问题

Android 自定义SimpleCursorAdapter的问题,android,gridview,simplecursoradapter,Android,Gridview,Simplecursoradapter,我正在尝试使用自定义SimpleCursorAdapter使用db游标中的数据填充网格视图。 我的游标有数据(我选中了),但GridView中没有显示任何内容,甚至没有调用getView()方法 有人能帮忙吗?为什么不调用getView() 谢谢 活动 适配器 因为CursorAdapter没有getView方法。它具有newView方法。 因此,扩展SimpleCursorAdapter并覆盖newWiev和其他方法,如 public class ContactListCursorAdapte

我正在尝试使用自定义SimpleCursorAdapter使用db游标中的数据填充网格视图。 我的游标有数据(我选中了),但GridView中没有显示任何内容,甚至没有调用getView()方法

有人能帮忙吗?为什么不调用getView()

谢谢

活动

适配器


因为CursorAdapter没有getView方法。它具有newView方法。 因此,扩展SimpleCursorAdapter并覆盖newWiev和其他方法,如

public class ContactListCursorAdapter extends SimpleCursorAdapter implements Filterable {

    private Context context;

    private int layout;

    public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.context = context;
        this.layout = layout;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        Cursor c = getCursor();

        final LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(layout, parent, false);

        int nameCol = c.getColumnIndex(People.NAME);

        String name = c.getString(nameCol);

        /**
         * Next set the name of the entry.
         */     
        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(name);
        }

        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {

        int nameCol = c.getColumnIndex(People.NAME);

        String name = c.getString(nameCol);

        /**
         * Next set the name of the entry.
         */     
        TextView name_text = (TextView) v.findViewById(R.id.name_entry);
        if (name_text != null) {
            name_text.setText(name);
        }
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
        if (getFilterQueryProvider() != null) { return getFilterQueryProvider().runQuery(constraint); }

        StringBuilder buffer = null;
        String[] args = null;
        if (constraint != null) {
            buffer = new StringBuilder();
            buffer.append("UPPER(");
            buffer.append(People.NAME);
            buffer.append(") GLOB ?");
            args = new String[] { constraint.toString().toUpperCase() + "*" };
        }

        return context.getContentResolver().query(People.CONTENT_URI, null,
                buffer == null ? null : buffer.toString(), args, People.NAME + " ASC");
    }
}

列名数组和视图数组为空。因此,列名到视图的映射将永远不会发生。如果我删除,这可能就是您无法在getView上获得回调的原因

c.close();
dbAdapter.close();
它是有效的


那么我应该在哪里关闭这个光标呢?…

我也有同样的问题。我用的是ArrayAdapter,后来我改成了SimpleCorsOrAdapter,但它在屏幕上什么都不显示

我搬走了

c.close();
dbAdapter.close();

现在它工作正常。

不要手动添加多余的方法,这只会导致问题。在回答这个问题时,Romain Guy写道“覆盖getView()是完全有效的”。无论如何,我会尝试使用newView()和bindView()。谢谢
c.close();
dbAdapter.close();
c.close();
dbAdapter.close();