Search code examples
androidandroid-contentproviderandroid-gridview

How do I bind a GridView to a Custom ContentProvider


I'm trying to bind a customer ContentProvider to my activity that holds a GridView

    String[] projection = { SAppsDatabase.ID, SAppsDatabase.COL_APP_TITLE};
    String[] uiBindFrom = { SAppsDatabase.COL_APP_TITLE };
    int[] uiBindTo = { R.id.title };

    Cursor apps =  managedQuery(
           MyProvider.CONTENT_URI, projection, null, null, null);

    GridView gridview = (GridView) findViewById(R.id.gridview);

    CursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.grid_app_list, apps,
            uiBindFrom, uiBindTo);

    gridview.setAdapter(adapter);

This is not working.

I manage to bind it to a ListActivity by doing the same as above, and setting: setListAdapter(adapter);


Solution

  • This will not work for a GridView as the name indicates it is a grid of views

    GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. 
    The grid items are automatically inserted to the layout using a ListAdapter.
    

    but you are providing it a cursor adapter which doesn't make much sense.

    Take a look at this sample

    If you want to provide a custom adapter then you will have extend adapter and return the view content in the adapter.