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. Python并发编程-一个简单的多进程实例

    import time from multiprocessing import Process import os def func(args,args2): #传递参数到进程 print(args, ...

  2. hdu 1690 Bus System (有点恶心)

    Problem Description Because of the huge population of China, public transportation is very important ...

  3. BZOJ 3231: [Sdoi2008]递归数列 (JZYZOJ 1353) 矩阵快速幂

    http://www.lydsy.com/JudgeOnline/problem.php?id=3231   和斐波那契一个道理在最后加一个求和即可 #include<cstdio> #i ...

  4. [BZOJ 4071] 巴邻旁之桥

    Link: BZOJ 4071传送门 Solution: 首先算出能提前算的贡献 $K=1$:肯定选中间的点,小学数学 $K=2$:对于每对$(x,y)$一定选离$(x+y)/2$近的桥 也就是说将$ ...

  5. Yii2 init 与 beforeAction 区别

    1.执行顺序 init > beforeAction 2.调用子函数时,两个函数都不会再次执行 3.返回值 init返回false继续执行,beforeAction停止执行 4.执行EXIT,全 ...

  6. liblinear参数及使用方法(原创)

    开发语言:JAVA 开发工具:eclipse (下载地址 http://www.eclipse.org/downloads/) liblinear版本:liblinear-1.94.jar (下载地址 ...

  7. 注解@Aspect实现AOP功能

    springboot中pom引入jar <!-- aop 切面 --> <dependency> <groupId>org.springframework.boot ...

  8. 三、python的数据类型 列表、元组、字典

    1.list 列表 列表是由一序列特定顺序排列的元素组成的.可以把字符串,数字,字典等都可以任何东西加入到列表中,列表中的元素之间没有任何关系.列表也是自带下标的,默认也还是从0开始. List常用的 ...

  9. 内功心法 -- java.util.LinkedList<E> (6)

    写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...

  10. SCOJ 4427: Miss Zhao's Graph dp

    4427: Miss Zhao's Graph 题目连接: http://acm.scu.edu.cn/soj/problem.action?id=4427 Description Mr Jiang ...