自定义SimpleCursorAdapter的问题

时间:2012-01-15 13:25:48

标签: android gridview simplecursoradapter

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

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

由于

活动

dbAdapter = new DBAdapter(this);    
dbAdapter.open();

Cursor c;
c = dbAdapter.fetchPCList();
startManagingCursor(c);     

String[] from = new String[] {};
int[] to = new int[] {};

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new PCIconAdapter(this, R.layout.pc_icon, c, from, to));

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

适配器

public class PCIconAdapter extends SimpleCursorAdapter {
    private final Context mContext;
    private final int mLayout;
    private final Cursor mCursor;
    private final int mPCIDIndex;
    private final int mClassNameIndex;
    private final LayoutInflater mLayoutInflater;

    private final class ViewHolder {
        public TextView pc_id_view;
        public TextView clas_name_view;
    }

    public PCIconAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);

        this.mContext = context;
        this.mLayout = layout;
        this.mCursor = c;
        this.mPCIDIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_LM_ID);
        this.mClassNameIndex = mCursor.getColumnIndex(DBAdapter.KEY_PC_CLAS_NAME);
        this.mLayoutInflater = LayoutInflater.from(mContext);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (mCursor.moveToPosition(position)) {
            ViewHolder viewHolder;

            if (convertView == null) {
                convertView = mLayoutInflater.inflate(mLayout, null);

                viewHolder = new ViewHolder();
                viewHolder.pc_id_view = (TextView) convertView.findViewById(R.id.pc_id);
                viewHolder.clas_name_view = (TextView) convertView.findViewById(R.id.clas_name);

                convertView.setTag(viewHolder);
            }
            else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            String pc_id = mCursor.getString(mPCIDIndex);
            String clas_name = mCursor.getString(mClassNameIndex);

            viewHolder.pc_id_view.setText(pc_id);
            viewHolder.clas_name_view.setText(clas_name);
        }

        return convertView;
    }
}

4 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。我使用的是ArrayAdapter然后我改为SimpleCursorAdapter,但它没有在屏幕上显示任何内容。

我删除了

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

现在工作正常。

答案 1 :(得分:0)

因为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");
    }
}

答案 2 :(得分:0)

您的列名数组和Views数组为空。因此,视图映射的列名永远不会发生。这可能是你没有得到getView回调

的原因

答案 3 :(得分:0)

如果我删除

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

它有效......

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