package com.example.androidtest;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.provider.Contacts.People;
import android.widget.GridView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gv = (GridView)findViewById(R.id.gridview);
Cursor c = managedQuery(People.CONTENT_URI, null, null, null, People.NAME);
String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, cols, views);
gv.setAdapter(adapter);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/gridview.xml -->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10px"
android:verticalSpacing="10px"
android:horizontalSpacing="10px"
android:numColumns="auto_fit"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"
/>