GridView实现图标显示
扫描二维码
随时随地手机看文章
package com.example.administrator.gridview; import android.content.Context; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.GridView; import android.widget.SimpleAdapter; import android.widget.Toast; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends Activity { private GridView gridView; private Context mContext; private List<Map> data_list; private Object[]pics = {R.drawable.address_book,R.drawable.calendar,R.drawable.camera,R.drawable.clock,R.drawable.games_control,R.drawable.messenger,R.drawable.ringtone,R.drawable.settings,R.drawable.speech_balloon,R.drawable.weather,R.drawable.world,R.drawable.youtube}; private String[]texts = {"地址簿","日历","照相机","时钟","游戏","信使","铃声","设置","微信","天气","地图","YouTube"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; gridView = (GridView)findViewById(R.id.gridView); data_list = new ArrayList<Map>(); SimpleAdapter adapter = new SimpleAdapter(mContext,getData(),R.layout.item,new String[]{"pic","text"},new int[]{R.id.pic,R.id.text}); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new GridView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { HashMap_item = (HashMap)gridView.getItemAtPosition(position); Toast.makeText(mContext,_item.get("pic") + "=>" + _item.get("text"),Toast.LENGTH_SHORT).show(); } }); } private List<Map> getData(){ int length = pics.length; for (int i=0 ; i < length ; i++){ Mapdata = new HashMap(); data.put("pic",pics[i]); data.put("text",texts[i]); data_list.add(data); } return data_list; } }
item.xml