博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
带文字的GridView2Activity
阅读量:6673 次
发布时间:2019-06-25

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

1-GridView2Activity

package com.example.lenovo.textapp4;        import android.os.Bundle;        import android.support.v7.app.AppCompatActivity;        import android.view.View;        import android.view.ViewGroup;        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 GridView2Activity extends AppCompatActivity {    GridView gv_1;    List
liv; TextView gv_3; ImageView gv_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_grid_view2); gv_1 = (GridView)findViewById(R.id.gv_1); gv_3 = (TextView)findViewById(R.id.tv_3); gv_2 = (ImageView)findViewById(R.id.gv_2); //1、数据 图片的id liv = new ArrayList<>(); MyClass myClass = new MyClass(R.drawable.f1,"美食1"); liv.add(myClass); liv.add(new MyClass(R.drawable.f2,"美食2")); liv.add(new MyClass(R.drawable.f3,"美食3")); liv.add(new MyClass(R.drawable.f4,"美食4")); liv.add(new MyClass(R.drawable.f5,"美食5")); liv.add(new MyClass(R.drawable.f6,"美食6")); liv.add(new MyClass(R.drawable.f7,"美食7")); liv.add(new MyClass(R.drawable.f8,"美食8")); ImageAdapter imageAdapter = new ImageAdapter(); { gv_1.setAdapter(imageAdapter); } } class MyClass { private int img; private String content; public int getImg() { return img; } public void setImg(int img) { this.img = img; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public MyClass(int img,String content) { this.img = img; this.content = content; } } //适配器 class ImageAdapter extends BaseAdapter { @Override public int getCount() { return liv.size(); } @Override public Object getItem(int position) { return liv.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { //1.得到数据 图片的id MyClass myClass = liv.get(position); //2.准备View if (convertView ==null) { convertView = View.inflate(GridView2Activity.this, R.layout.gridview, null); } //3.适配 //转换 ImageView imageView = (ImageView)convertView.findViewById(R.id.gv_2); imageView.setImageResource(myClass.getImg()); TextView textView = (TextView)convertView.findViewById(R.id.gv_3); textView.setText(myClass.getContent()); return convertView; } }}
View Code

 

2-activity_grid_view2

View Code

 

3-gridview.xml

View Code

转载于:https://www.cnblogs.com/1ming/p/5514264.html

你可能感兴趣的文章
JS实现省市联动效果
查看>>
运算符重载
查看>>
ZOJ3872 Beauty of Array【DP】
查看>>
js_dom 之事件注册、移除 、pageX
查看>>
cmake 添加头文件目录,链接动态、静态库(转载)
查看>>
web crawling(plus4) pretend to be A web
查看>>
nth-of-type和nth-child
查看>>
百度云管家下载慢解决方法
查看>>
POJ 1001 Exponentiation
查看>>
动态改变 网页的宽度 <--> body的滚动条
查看>>
vs code上配置python的运行环境
查看>>
BusyBox ifup udhcpc后台运行
查看>>
十七、oracle 权限
查看>>
Kali渗透测试——urlcrazy
查看>>
高效能程序员的修炼
查看>>
剑指offer——栈的压入、弹出序列
查看>>
5、利用两个栈实现队列,完成push和pop操作
查看>>
[AX]AX2012 纪录缓存
查看>>
Hibernate执行流程
查看>>
清单文件介绍
查看>>