グリッドGrvidViewビューを実現するクリックジャンプActivity

5079 ワード

package cn.hwttnet.com.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.hwttnet.com.adapter.GridAdapter;
import cn.hwttnet.com.bean.GridInfo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MainActivity extends Activity {
	private GridView gridview;
	private GridAdapter adapter;
	Context context = MainActivity.this;
	Class<?> mActivities[] = { RegistrationActivity.class, AnnouncementActivity.class,
			WorkflowActivity.class
			,WorkLogActivity.class, };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);

	}

	void init() {

		gridview = (GridView) findViewById(R.id.gridview);
		adapter = new GridAdapter(getList(), context);
		gridview.setAdapter(adapter);
		gridview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				onListItemClick(arg2);
			}

		});

	}

	void onListItemClick(int index) {
		if (index < 0 || index >= mActivities.length)
			return;
		Intent intent = new Intent(context, mActivities[index]);
		this.startActivity(intent);
	}

	protected void onStart() {
		super.onStart();
		init();
	}

	protected void onResume() {
		super.onResume();
	}

	ArrayList<GridInfo> getList() {
		ArrayList<GridInfo> list = new ArrayList<GridInfo>();
		GridInfo gif1 = new GridInfo();
		gif1.setImages(R.drawable.ic_launcher);
		gif1.setName(" ");
		GridInfo gif2 = new GridInfo();
		gif2.setImages(R.drawable.ic_launcher);
		gif2.setName(" ");
		GridInfo gif3 = new GridInfo();
		gif3.setImages(R.drawable.ic_launcher);
		gif3.setName(" ");
		GridInfo gif4 = new GridInfo();
		gif4.setImages(R.drawable.ic_launcher);
		gif4.setName(" ");
		 
		list.add(gif1);
		list.add(gif2);
		list.add(gif3);
		list.add(gif4);
	 
		return list;

	}
}

 
package cn.hwttnet.com.adapter;

import java.util.ArrayList;
import java.util.List;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.hwttnet.com.bean.GridInfo;
import cn.hwttnet.com.ui.R;

public class GridAdapter extends BaseAdapter {
	ArrayList<GridInfo> list;
	Context context;
	 
	public GridAdapter(ArrayList<GridInfo> list,Context context) {
		this.list=list;
		this.context=context;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return  list.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView==null)
		{
			LayoutInflater layout=LayoutInflater.from(context);
			convertView=layout.inflate(R.layout.grid_item, null);
		}
		ImageView itemImg=(ImageView)convertView.findViewById(R.id.itemImage);
		TextView itemName=(TextView)convertView.findViewById(R.id.itemText);
		itemImg.setImageResource(list.get(position).getImages());
		itemName.setText(list.get(position).getName());
		return convertView;
	}

}

 
package cn.hwttnet.com.bean;

public class GridInfo {
	String name;
	int images;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getImages() {
		return images;
	}
	public void setImages(int images) {
		this.images = images;
	}

}