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_2">

</GridView>

JAVA代码

package com.example.dell.wodelianxi;

import android.app.AlertDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

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

public class baseadaptershixian extends AppCompatActivity {

    List<icon> xx;
    GridView gv2;
    ImageView image_1;
    TextView text_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_baseadaptershixian);

        gv2=(GridView)findViewById(R.id.gv_2);


        xx = new ArrayList<icon>();

        xx.add(new icon(R.drawable.yinyue,"音乐"));
        xx.add(new icon(R.drawable.tongxunlu,"通讯录"));
        xx.add(new icon(R.drawable.youxi,"游戏"));
        xx.add(new icon(R.drawable.rili,"日历"));
        xx.add(new icon(R.drawable.shezhi,"设置"));
        xx.add(new icon(R.drawable.yueduqi,"阅读器"));
        xx.add(new icon(R.drawable.zhinanzhen,"指南针"));
        xx.add(new icon(R.drawable.shijian,"时间"));
        xx.add(new icon(R.drawable.jisuanqi,"计算器"));
        xx.add(new icon(R.drawable.tianqi,"天气"));
        xx.add(new icon(R.drawable.zhaoxiangji,"拍照"));
        xx.add(new icon(R.drawable.xueguan,"学管"));

        gv2.setAdapter(new mybase());

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

                alertDialog.setMessage(xx.get(position).getContent());
                alertDialog.show();
            }
        });


    }

    class mybase extends BaseAdapter
    {
        @Override
        public int getCount() {
            return xx.size();
        }

        @Override
        public Object getItem(int position) {
            return xx.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater layoutInflater = getLayoutInflater();

           View v_v =  layoutInflater.inflate(R.layout.gridbujulayout, null);



           icon ii = xx.get(position);

            image_1 = (ImageView)v_v.findViewById(R.id.image_1);

            text_1 = (TextView)v_v.findViewById(R.id.text_1);
这里如果不加v_v,会报空指针异常,要获取加载视图的组件ID image_1.setImageResource(ii.getImage()); text_1.setText(ii.getContent());
return v_v; } } }

 

package com.example.dell.wodelianxi;

/**
 * Created by DELL on 2016/4/10.
 */
public class icon {

    private int image;

    private String content;

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getImage() {
        return image;
    }

    public void setImage(int image) {
        this.image = image;
    }

    public icon( int image,String content) {

        this.image = image;
        this.content = content;
    }
}

 

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