Question

I'm trying to fill a grid view using data from a db cursor using a custom SimpleCursorAdapter. My cursor has data (I checked), but nothing is shown in the GridView, and the getView() method is not even called.

Anybody can help? Why is getView() not called?

Thanks

Activity

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();

Adapter

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;
    }
}
Was it helpful?

Solution 4

If I remove

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

it works...

So where am I supposed to close this cursor?...

OTHER TIPS

I had the same problem. I was using an ArrayAdapter and then I changed to a SimpleCursorAdapter, but it doesn't show anything on screen.

I removed

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

and now its working fine.

because CursorAdapter does not have getView method . it have newView method . so extend SimpleCursorAdapter and overRide newWiev and other methods like

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

Your column names array and Views array are empty. So column name to views mapping will never occur. That maybe the reason you are not getting callback on getView

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top