WEB开发网
开发学院手机开发Android 开发 Android GridView的使用 阅读

Android GridView的使用

 2012-09-12 13:33:34 来源:WEB开发网   
核心提示:下面是Java代码如何使用GridViewpackage view;import java.util.ArrayList;import java.util.HashMap;import model.HeroSimpleInfo;import service.ObtainInfo;import android.app.A

下面是Java代码如何使用GridView

package view;

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

import model.HeroSimpleInfo;
import service.ObtainInfo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.zhychneg.dota.R;

public class BrainHeroActivity extends Activity implements OnItemClickListener{

	GridView gv=null;
	long exitTime=0;
	HeroSimpleInfo []heros=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.brainheroall);
		gv=(GridView) findViewById(R.id.brainheroall);
		
		
		ArrayList<HashMap<String,Object>> al=new ArrayList<HashMap<String,Object>>();
	
		
		heros=ObtainInfo.getBrainHeroSimpleInfo();
		
		
		for(int i=0;i<heros.length;i++)
		{
			HashMap<String, Object> map = new HashMap<String, Object>();  
		       
	        map.put("icon", heros[i].getRes());//添加图像资源的ID  
	        map.put("name", heros[i].getName());//按序号做ItemText  
	        al.add(map);  
		}
		
		SimpleAdapter sa=new SimpleAdapter(this,al,R.layout.gridview_layout,new String[]{"icon","name"},new int[]{R.id.ItemImage,R.id.ItemText});
		gv.setAdapter(sa);
		gv.setOnItemClickListener(this);
		
		
		
		
		
		
	}
	
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		
		Intent intent =new Intent(this,AboutHeroActivty.class);
		intent.putExtra("HERO", heros[position]);
		this.startActivity(intent);
		
	}
	

}

上一页  1 2 

Tags:Android GridView 使用

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接