Android Tutorial  »  GridView  »  UI

PowenKo, Android Tutorial, GridView 20-21

November 15, 2011   ·   0 Comments

package com.powenko.GridView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewUsage extends Activity {

	int[] image = { R.drawable.hana, R.drawable.cc, R.drawable.cc,
			R.drawable.zs, R.drawable.lr, R.drawable.sm, R.drawable.dz,
			R.drawable.fs, R.drawable.xd, R.drawable.qs, R.drawable.ms,
			R.drawable.ss, };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		GridView gv = (GridView) findViewById(R.id.gride);

		ImageList adapter = new ImageList(this);

		gv.setAdapter(adapter);
	}

	class ImageList extends BaseAdapter {
		Activity activity;

		public ImageList(Activity a) {
			activity = a;
		}

		@Override
		public int getCount() {
			return image.length;
		}

		@Override
		public Object getItem(int position) {
			return image[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView iv = new ImageView(activity);
			iv.setImageResource(image[position]);
			iv.setBackgroundColor(R.drawable.white);
			return iv;
		}
	}
}

layout\main.xml


<!--?xml version="1.0" encoding="utf-8"?-->

sample code:
GridViewUsage

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Android7

Amazon

adsbygoogle

Recent Posts

Categories

Amazon