Search code examples
androidandroid-sqliteandroid-adapterandroid-gridviewandroid-cursor

Need help figuring out an Adapter "tutorial" on Google's Android Developer Site


I am currently trying to display database information in my Android app.
I want the data displayed in a tabular form:

+----------+----------+-----+----------+
| column 1 | column 2 | ... | column n |
+----------+----------+-----+----------+
| xxxx     | xxxx     | ... | nnnn     |
| yyyy     | yyyy     | ... | nnnn     |
| ....     | ....     | ... | nnnn     |
| zzzz     | zzzz     | ... | nnnn     |
+----------+----------+-----+----------+

In looking at the information HERE, it looks like I need to use a GridView with a SimpleCursorAdapter.

So, I created a DatabaseHelper class that extends SQLiteOpenHelper. In that class, I have a method that returns a Cursor for a query:

public static final String DATABASE_NAME = "myDatabaseName";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "myDatabaseTableName";

public DatabaseHelper(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public static final String MY_QUERY = "SELECT * FROM " + TABLE_NAME;

public Cursor getDataCursor(){
    SQLiteDatabase sqliteDB = this.getReadableDatabase();
    return sqliteDB.rawQuery(MY_QUERY, null);
}

One section of the "tutorial" on that Google Dev page states:

For example, if you want to create a list of people's names and phone numbers, you can perform a query that returns a Cursor containing a row for each person and columns for the names and numbers. You then create a string array specifying which columns from the Cursor you want in the layout for each result and an integer array specifying the corresponding views that each column should be placed

Then has this code:

String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};

So, in my MainActivity.java I create a new instance of my DatabaseHelper, and get a Cursor:

DatabaseHelper myDBHelper = new DatabaseHelper(this);
Cursor myDataCursor = myDBHelper.getDataCursor();

What I cannot figure out is, what is "ContactsContract" in Google's snippet above? I thought it might be their Cursor, but my cursor does not have "Data" or "CommonDataKinds" methods available to it. I did some poking around Google's API guides pages to see where they defined those methods, but cannot seem to locate them. I also cannot figure out where they got "R.layout.person_name_and_number" in that same example.

Thanks for taking the time to read.

EDIT:

I have added this code to my code to MainActivity.java:

DatabaseHelper myDBHelper = new DatabaseHelper(this);
Cursor myDataCursor = myDBHelper.getDataCursor();
String[] fromColumns = {"_id", "columnA","columnB","...","columnN"};
int[] toViews = {R.id.id_textview, R.id.columnA_textview, R.id.columnB_textview, ... , R.id.columnN_textview};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.my_new_layout, myDataCursor, fromColumns, toViews, 0);
ListView myListView = (ListView) findViewById(R.id.row_of_data);
myListView.setAdapter(sca);

And I created a new view called my_new_layout.xml:

<ListView android:id="@+id/row_of_data">
    <TextView android:id="@+id/id_textview" />
    <TextView android:id="@+id/columnA_textview" />
    <TextView android:id="@+id/columnB_textview" />
    ...
    <TextView android:id="@+id/columnN_textview" />
</ListView>

And then I added my new view to activity_main.xml

<include layout="@layout/"my_new_layout />

However, now when I try to run the application, I get the error:

java.lang.unsupportedoperationexception addview(view, layoutparams) is not supported in adapterview

And I get this error, strangely enough, where I include my new layout into activity_main.xml.


Solution

  • Looking at the documentation of the SimpleCursorAdapter and it's constructor you can see

    public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to, int flags)
    

    Parameters

    • context: The context where the ListView associated with this SimpleListItemFactory is running
    • layout: resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"
    • c: The database cursor. Can be null if the cursor is not available yet.
    • from: A list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.
    • to: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.
    • flags: Flags used to determine the behavior of the adapter, as per CursorAdapter(Context, Cursor, int).

    fromColumns are the names of the columns of your table, which you would like to get data from

    toViews are the ids of your TextViews that should be inside the XML layout for your row (second parameter in constructor - int layout).

    Number of items in both arrays should be the same.