ListView在Android中有着很重要的作用。Android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

背景                                                                                          

建了个Person类,里面有Name,Number,id,三个属性。

private String name;
private String number;
private int id;

主要用来向listView中添加信息的。

布局                                                                                            

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"> <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </LinearLayout>

直接放listview上去就OK了。

程序                                                                                           

private ListView lv;
   private List<Person> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = new ArrayList<Person>();
lv = (ListView) findViewById(R.id.lv);
addPerson();
lv.setAdapter(new MyAdapter());
} private class MyAdapter extends BaseAdapter { @Override
public int getCount() {
//返回大小
return list.size();
} @Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return null;
} @Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return 0;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(getApplicationContext());
tv.setTextSize(50);
tv.setTextColor(Color.BLUE);
Person person = list.get(position);
tv.setText(person.toString());
System.out.println("返回位置"+position);
return tv
} } // 添加数据函数
private void addPerson() { for (int i = 0; i < 20; i++) {
Person person1 = new Person("张三" + i, "12345678912", i);
list.add(person1);
}
}

要申明一个adapter,adapter里面放数据,然后listview通过setAdapter配置adapter。

----------------------------简单的分割线------------------------------------简单的---------------------------

如果需要自定义lixtview中当样式的话,可以仙剑一个布局item的布局。

item布局                                                                                  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dip"
android:orientation="horizontal" > <TextView
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="id"
android:textColor="#ff0000"
android:textSize="18sp" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" > <TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="名字"
android:textColor="#000000"
android:textSize="18sp"/>
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="电话"
android:textColor="#88000000"
android:textSize="16sp"/> </LinearLayout> </LinearLayout>

重新写一下adapter里面的getView方法:

getView                                                                                    

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Person person = list.get(position);
View view = View.inflate(MainActivity.this, R.layout.listview_item, null);
//找id
TextView tv_id = (TextView) view.findViewById(R.id.tv_id);
tv_id.setText("id:"+person.getId());
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
tv_name.setText("tv_name:"+person.getName());
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
tv_phone.setText("tv_phone:"+person.getNumber());
return view;
}

我是天王盖地虎的分割线                                                               

源代码:http://pan.baidu.com/s/1dD1Qx01

listview学习.zip

转载请注明出处:http://www.cnblogs.com/yydcdut

Android -- ListView与Adapter的更多相关文章

  1. Android listview与adapter用法(BaseAdapter + getView)

    Android listview与adapter用法http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html package ...

  2. Android listview与adapter用法

    listview与adapter用法 博客分类: android   一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...

  3. 【转】Android listview与adapter用法

    一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...

  4. [Android] ListView关于adapter多种view设置

    使用的关键点是在adapter覆盖两个方法 public int getItemViewType(int position) public int getViewTypeCount() 其它的可另go ...

  5. [Android]ListView的Adapter.getView()方法中延迟加载图片的优化

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html 举个例子吧,以好友列表为例 ListVi ...

  6. android listview用adapter.notifyDataSetChanged()无法刷新每项的图标

    http://blog.csdn.net/caizhegnhao/article/details/41318575 今天在开发中遇到一个很奇怪的listview的问题. 这个问题情景是我的应用需要做一 ...

  7. Android ListView 自定义 Adapter

    自定义Adapter类 public class ListViewAdapter extends BaseAdapter { private static final String TAG = Mai ...

  8. Android ListView 和 ***Adapter 从本地/网络获取歌曲列表

    本文内容 环境 项目结构 测试数据 演示 1:SimpleAdapter 演示 2:BaseAdapter 演示 3:CustomLazyList 演示 4:CustomLazyCompleteLis ...

  9. Android ListView自定义Adapter使用误区

    参考博客:BaseAdapter中重写getview的心得以及发现convertView回收的机制 使用自定义的BaseAdapter实现LIstView的展示 由于Recycler(反复循环器)的机 ...

随机推荐

  1. CodeForces 785D Anton and School - 2

    枚举,容斥原理,范德蒙恒等式. 先预处理每个位置之前有多少个左括号,记为$L[i]$. 每个位置之后有多少个右括号,记为$R[i]$. 然后枚举子序列中第一个右括号的位置,计算这个括号的第一个右括号的 ...

  2. 关于如何在 Unity 的 UI 菜单中默认创建出的控件 Raycast Target 属性默认为 false

    关于如何在 Unity 的 UI 菜单中默认创建出的控件 Raycast Target 属性默认为 false 我们在 Unity 中通过 UI 菜单创建的各种控件,比如 Text, Image 等, ...

  3. js代码小优化

    今天真坑,老大请了两天假,来了之后指指点点,不过人家说的倒是很是到位 好不容易把嵌套小窗口登陆注册功能,做完了,直接调之前写好的登陆注册功能,也就是页面跳转 并不是ajax异步登陆 说让改成ajax ...

  4. eclipse的一些部署

    1. Eclipse导入一个项目Package Exporer-------右键------import------General------Existing Project into Workspa ...

  5. 运用jquery做打印和导出操作

    我最近接手的项目中经常让做出打印和导出统计图和表格 首先说打印,打印如果用echarts做出来的图表,打印的时候,要借助jquery的打印插件. 打印插件: <script src=" ...

  6. code forces 505A

    Mr. Kitayuta's Gift Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  7. Redis_常用5大数据类型简介

    前面介绍了一些redis的的基本配置,以及安装,本文继续学习redis的五大数据类型. 一.Redis的五大数据类型 String(字符串).List(列表).Set(集合).Hash(哈希,类似ja ...

  8. luogu P1965 转圈游戏

    题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此 ...

  9. 【BZOJ2763/洛谷p4563】【分层图最短路】飞行路线

    2763: [JLOI2011]飞行路线 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 4630  Solved: 1797[Submit][Stat ...

  10. 2015 UESTC 数据结构专题D题 秋实大哥与战争 SET的妙用

    D - 秋实大哥与战争 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 D ...