Android中SimpleAdapter的使用—自定义列表
本人初学Android,今天研究到Adapter这块感觉挺有意思的,写了个自定义列表进行测试
首先我们新建一个layout列表布局文件,具体布局可以自己设定。
下面贴上我的自定义布局文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:background="@drawable/list"
android:layout_height="wrap_content"> <ImageView
android:id="@+id/ico"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="@mipmap/ic_launcher"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/biaoti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是标题"
android:layout_marginTop="5dp"
android:textSize="22sp"/>
<TextView
android:id="@+id/content"
android:layout_marginTop="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="我是项目信息"/>
</LinearLayout>
</LinearLayout>
上面代码的效果图如下,整体用的是一个Image,以及两个TextView

不好看就先凑合吧,测试用
接下来我们开始MainActivity.java
package yuntu.com.yuntu; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends AppCompatActivity {
private ListView listView;
//声明标题
private String[] title = new String[]{
"我是第1个Title", "我是第2个Title", "我是第3个Title", "我是第4个Title"
};
//声明内容
private String[] content = new String[]{
"我是第1个content", "我是第2个content", "我是第3个content", "我是第4个content"
};
//声明图标
private int[] imgIds = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list_item01);
List<Map<String, Object>> listitem = new ArrayList<Map<String, Object>>();
for (int i=0;i<content.length;i++){
Map<String, Object> map = new HashMap<String, Object>();
map.put("ico",imgIds[i]);
map.put("title",title[i]);
map.put("content",content[i]);
listitem.add(map);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this,listitem,R.layout.main_list,new String[]{"title","content","ico"},new int[]{R.id.biaoti,R.id.content,R.id.ico});
listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView bt = (TextView) view.findViewById(R.id.biaoti);
TextView nr = (TextView) view.findViewById(R.id.content);
Toast.makeText(MainActivity.this, bt.getText() + "|" + nr.getText(), Toast.LENGTH_SHORT).show();
}
});
}
} //本篇文章记录日常代码,希望也可以帮到需要的人
————鲨哒哒
Android中SimpleAdapter的使用—自定义列表的更多相关文章
- Android中使用ListView绘制自定义表格(2)
上回再写了<Android中使用ListView绘制自定义表格>后,很多人留言代码不全和没有数据样例.但因为项目原因,没法把源码全部贴上来.近两天,抽空简化了一下,做了一个例子. 效果图如 ...
- Android(java)学习笔记137:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器
1.SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表, ...
- Android(java)学习笔记79:Android中SimpleAdapter,ArrayAdapter和BaseAdapter常见的适配器
1. SimpleAdapter(BaseAdapter子类扩展类): simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片)等.可以显示比较复杂的列表 ...
- Android中的树状(tree)列表
树状列表前端挺常用的,还有人专门写过Ztree,Android中有的时候也需要使用到树状列表,上篇文章写了一下ExpandableListView,ExpandableListView最多支持两级结构 ...
- android中RecyclerView控件的列表项横向排列
本文是在上一篇文章的基础上做的修改:android中RecyclerView控件的使用 1.修改列表项news_item.xml:我这里是把新闻标题挪到了新闻图片的下面显示 <?xml vers ...
- Android 中的AlertDialog使用自定义布局
Android使用指定的View开发弹窗功能 Android开发中进程会使用到我们的AlertDialog,但是比较可惜的是我们的Android原生的AlertDialog的效果又比较的简陋,这个时候 ...
- Android中为TextView增加自定义的HTML标签
Android中的TextView,本身就支持部分的Html格式标签.这其中包括常用的字体大小颜色设置,文本链接等.使用起来也比较方便,只需要使用Html类转换一下即可.比如: textView.se ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Android中intent如何传递自定义数据类型
转载自:http://www.cnblogs.com/GoAhead/archive/2012/07/16/2593868.html 大家好,好久不见,今天要给大家讲一下Android中Intent中 ...
随机推荐
- EM最大期望算法-走读
打算抽时间走读一些算法,尽量通俗的记录下面,希望帮助需要的同学. overview: 基本思想: 通过初始化参数P1,P2,推断出隐变量Z的概率分布(E步): 通过隐变量Z的概 ...
- windows编程初步
#include <windows.h> const char g_szClassName[] = "myWindowClass"; LRESULT CALLBACK ...
- 微信小程序获取html内容后展示(C#)
使用场景:微信小程序 具体功能:从服务器获取文章内容 展示在小程序里 使用语言: C# -------------------------------------------------------- ...
- 使用NPOI写入Excel数据(ASP.NET)
第一次做这个写入数据的功能,研究了下npoi的类 IWorkbook wb = new HSSFWorkbook(); //创建表 ISheet sh = wb.CreateSheet("X ...
- python入门编程之基础
Python, 是一种面向对象.解释型计算机程序设计语言.Python语法简洁清晰,特色之一是强制用空白符作为语句缩进.Python的设计哲学是"优雅"."明确" ...
- php 启动过程 - sapi MINIT 过程
php 启动过程 - sapi MINIT 过程 sapi 概念 sapi 是 php 的应用编程接口, server 端接收请求通过 sapi 接口层交给 php 处理 不同的 server 端底层 ...
- Devexpress 中对RedailMenu的使用
最近项目中用到RadialMenu,效果图如下所示: 闲下来就对,devexpress中的RedialMenu的使用总结一下. 第一:假设RedialMenu中全部是BarButtonItem的情况. ...
- CF #311 D. Vitaly and Cycle 加最少边形成奇圈
题目链接:http://codeforces.com/problemset/problem/557/D 大意 给出一个未必联通的无向图(点数至少为3),问最少加多少边可以形成一个奇圈,以及这样做的方案 ...
- Linux 基础(5)
Linux 基础 (五) 一.shell相关知识 shell一般代表两个层面的意思,一个是命令解释器,比如BASH,另外一个就是shell脚本.通过解释器的角度来理解shel 命令分为: ==> ...
- [原创]一种基于Python爬虫和Lucene检索的垂直搜索引擎的实现方法介绍
声明:本文首发在博客园晨星落羽,Shulin_Cao和lvmememe首页,转载请注明出处. 前言 2016.5到2017.5,我们三人(lvmememe,Shulin_Cao,晨星落羽)共同完成了一 ...