博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
GridView视图(BaseAdapter)
阅读量:5931 次
发布时间:2019-06-19

本文共 4384 字,大约阅读时间需要 14 分钟。

效果图:

1.src文件夹下 ShowActivity.java文件内容:

 

package com.jun.activity;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import com.jun.adapter.MyAdapter;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.widget.GridView;import android.widget.SimpleAdapter;public class ShowActivity extends Activity {    private GridView gridView=null;    private MyAdapter adapter=null;    private SimpleAdapter adapter1=null;    private List
> list=null; private HashMap
map=null; private String data[]={
"图片1","图片2","图片3","图片4","图片5","图6","图片7","图片8","图片9", "图片10","图片11","图片12","图片13","图片14","图片15","图片16"}; private int imgId[]={R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a, R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a, R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a,R.drawable.a}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show); init(); setData(); }private void init(){ gridView=(GridView) findViewById(R.id.grid_view); list=new ArrayList
>();}private void setData(){// 使用BaseAdapter添加数据 adapter=new MyAdapter(ShowActivity.this, data, imgId); gridView.setAdapter(adapter); // 使用SimpeAdapter添加数据// for(int i=0;i
();// // map.put("text", data[i]);// map.put("img", imgId[i]);// list.add(map);// // }// adapter1=new SimpleAdapter(ShowActivity.this, list, R.layout.gridview_item, new String[]{"text","img"}, new int[]{R.id.gridview_text,R.id.gridview_img});// gridView.setAdapter(adapter1);} @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_show, menu); return true; }}

 

2.ShowActivity布局文件activity_show.xml文件内容:

 

 

3.继承自BaseAdapter适配器类MyAdapter.java文件内容:

 

package com.jun.adapter;import com.jun.activity.R;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;public class MyAdapter extends BaseAdapter {    private String data[]=null;    private int imgId[]=null;    private Context context=null;    private LayoutInflater inflater=null;    public MyAdapter(Context context,String[] data, int[] imgId) {        super();        this.data = data;        this.imgId = imgId;        this.context = context;                inflater=LayoutInflater.from(context);    }    @Override    public int getCount() {        // TODO Auto-generated method stub        return data.length;    }    @Override    public Object getItem(int position) {        // TODO Auto-generated method stub        return position;    }    @Override    public long getItemId(int position) {        // TODO Auto-generated method stub        return position;    }    private class Holder{                TextView tv=null;        ImageView img=null;        public TextView getTv() {            return tv;        }        public void setTv(TextView tv) {            this.tv = tv;        }        public ImageView getImg() {            return img;        }        public void setImg(ImageView img) {            this.img = img;        }            }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        // TODO Auto-generated method stub//        获得holder以及holder对象中tv和img对象的实例        Holder holder;        if(convertView==null){                                    convertView=inflater.inflate(R.layout.gridview_item, null);            holder=new Holder();            holder.tv=(TextView) convertView.findViewById(R.id.gridview_text);            holder.img=(ImageView) convertView.findViewById(R.id.gridview_img);                        convertView.setTag(holder);                    }else{            holder=(Holder) convertView.getTag();                    }//        为holder中的tv和img设置内容        holder.tv.setText(data[position]);        holder.img.setImageResource(imgId[position]);//        注意  默认为返回null,必须得返回convertView视图        return convertView;    }}

 

4.GridView组件上面的布局:   gridview_item.xml文件内容:

 

  

 

转载地址:http://onktx.baihongyu.com/

你可能感兴趣的文章
【转载】SweetAlert2 使用
查看>>
oracle如何用sql查看触发器?
查看>>
如何对HashMap按键值排序
查看>>
test
查看>>
Unity小记
查看>>
IIS负载均衡-Application Request Route详解第二篇:创建与配置Server Farm
查看>>
js/jquery/插件表单验证
查看>>
虚拟键值表,模拟按键表
查看>>
Bandwidth内存带宽測试工具
查看>>
为Node.js编写组件的几种方式
查看>>
(轉貼) Anders Hejlsberg談C#、Java和C++中的泛型 (.NET) (C#)
查看>>
30天敏捷结果(24):恢复你的精力
查看>>
JNI——访问数组
查看>>
【定义及安装】Ambari——大数据平台的搭建利器
查看>>
metaClass
查看>>
C#开发和调用Web Service
查看>>
Android6.0机型上调用系统相机拍照返回的resultCode值始终等于0的问题
查看>>
全面理解Git
查看>>
JS敏感信息泄露:不容忽视的WEB漏洞
查看>>
让我们荡起双桨,Android 小船波浪动画
查看>>