XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="10dp">


    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:id="@+id/image_1"
        android:src="@drawable/touxiang1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="#000000"
        android:text="文字"
        android:id="@+id/text_1"
        />


</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:columnWidth="90dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:id="@+id/gv_1"
    >

</GridView>

JAVA代码

package com.example.dell.wodelianxi;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;

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

public class Gridview extends AppCompatActivity {
    GridView gv;
    List<Map<String, Object>> dL;
    SimpleAdapter sm;
    int[] icon = {R.drawable.yinyue, R.drawable.tongxunlu, R.drawable.youxi,
            R.drawable.rili, R.drawable.shezhi, R.drawable.yueduqi,
            R.drawable.zhinanzhen, R.drawable.shijian, R.drawable.jisuanqi,
            R.drawable.tianqi, R.drawable.zhaoxiangji, R.drawable.xueguan};

    String[] iconname = new String[]{"音乐", "通讯录", "游戏", "日历", "设置", "阅读器", "指南针",
            "时间", "计算器", "天气", "相机", "学管"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gridview);
        gv = (GridView) findViewById(R.id.gv_1);
        //新建List
        dL = new ArrayList<Map<String, Object>>();
        //获取数据
        getData();
        //新建适配器

        String[] str = {"image", "text"};

        int[] intid = {R.id.image_1, R.id.text_1};

        sm = new SimpleAdapter(this, dL, R.layout.gridbujulayout, str, intid);

        gv.setAdapter(sm);

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog alertDialog = new AlertDialog.Builder(Gridview.this).create();


                alertDialog.setMessage( iconname[position].toString());

                alertDialog.show();
            }
        });
    }


    public List<Map<String, Object>> getData() {
        for (int i = 0; i < icon.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", icon[i]);
            map.put("text", iconname[i]);
            dL.add(map);
        }

        return dL;
    }
}

 

posted on 2016-04-10 17:36  halooomo  阅读(347)  评论(0编辑  收藏  举报