本人初学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();
}
});
}
}       //本篇文章记录日常代码,希望也可以帮到需要的人
                    ————鲨哒哒

最新文章

  1. UIkit框架之UIPickerView
  2. 根据用户输入的二代身份证号码自动为其计算生成出生日期的javascript方法
  3. linux消息队列操作
  4. 【开源专访】Sea.js创始人玉伯的前端开发之路
  5. c#xml序列化对象,xml标记都缩写了
  6. javascript. String方法扩张.
  7. #include &lt;process.h&gt;
  8. JVM-类的四种载入方式
  9. PHP 反射类学习记录
  10. [Linux容器]当我们谈容器的时候,我们在谈什么
  11. Haproxy+Keepalived高可用环境部署梳理(主主和主从模式)
  12. JavaScript----特效代码
  13. CS229 6.7 Neurons Networks whitening
  14. ★itext-为pdf文件添加页眉页脚 | 3步完成 |
  15. Spring MVC @RequestParam
  16. React中的高阶组件,无状态组件,PureComponent
  17. UVA 580 Critical Mass
  18. Harbor实现容器镜像仓库的管理和运维
  19. 使用shell统计字符串出现的次数,并从大到小进行排序显示
  20. jQuery实现按钮5秒后可以点击

热门文章

  1. (iOS)关于zbar扫描条形码,所搭载的设备
  2. MySQL学习笔记(三)&mdash;索引
  3. JavaScript中的6种运算符总结
  4. MCDownloader(iOS下载器)说明书
  5. 线段树(hdu 1556)
  6. C++queue容器学习(详解)
  7. js字符串的操作
  8. 用户登录(Material Design + Data-Binding + MVP架构模式)实现
  9. [第一阶段] Python学习
  10. 我的Python---1