创建GridView的布局文件
自定义TextView用来显示GridView里的数据
然后是程序代码
public class CategoryActivity extends Activity { private ArrayList> data; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.category_layout); GridView categoryGrid = (GridView) findViewById(R.id.book_category_gridview_layout); data = BookUtil.getCategory(); String[] from = new String[]{"name"}; int[] to = {R.id.text}; SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.category_grid_item, from, to); categoryGrid.setAdapter(adapter); categoryGrid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View view, int postion, long id) { Toast.makeText(CategoryActivity.this, data.get(postion).get("name"), Toast.LENGTH_SHORT).show(); } }); }}
以下是运行截图