GridView實(shí)現(xiàn)圖標(biāo)顯示
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?=?{"地址簿","日歷","照相機(jī)","時(shí)鐘","游戲","信使","鈴聲","設(shè)置","微信","天氣","地圖","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