Android控件之ListView用法实例详解
作者:Ruthless 时间:2023-09-11 00:39:26
本文实例讲述了Android控件之ListView用法。分享给大家供大家参考。具体如下:
示例一:
在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyListView">
</ListView>
</LinearLayout>
my_listitem.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/myListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip">
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/itemTitle"
android:textSize="20dip">
</TextView>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/itemText">
</TextView>
</LinearLayout>
LsActivity类:
package com.ljq.ls;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class LsActivity extends Activity {
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.MyListView);
//组织数据源
List<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
for(int i=0;i<10;i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("itemTitle", "This is Title");
map.put("itemText", "This is text");
mylist.add(map);
}
//配置适配器
SimpleAdapter adapter = new SimpleAdapter(this,
mylist,//数据源
R.layout.my_listitem,//显示布局
new String[] {"itemTitle", "itemText"}, //数据源的属性字段
new int[] {R.id.itemTitle,R.id.itemText}); //布局里的控件id
//添加并且显示
list.setAdapter(adapter);
}
}
运行结果如下图所示:
示例二:
目录结构
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<!-- 使用相对布局 -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:textSize="20dip"
android:id="@+id/id"/>
<TextView android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignTop="@id/id"
android:layout_toRightOf="@id/id"
android:textSize="20dip"
android:id="@+id/name"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/name"
android:layout_toRightOf="@id/name"
android:textSize="20dip"
android:id="@+id/age"/>
</RelativeLayout>
实体JavaBean:
package com.ljq.domain;
public class Person {
private String id;
private String name;
private String age;
public Person() {
super();
}
public Person(String id, String name, String age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
自定义适配器PersonAdapter
package com.ljq.ls;
import java.util.List;
import com.ljq.domain.Person;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
/**
* ListView加载adapter的过程
*
* 1、先判断adapter有多少数据项,根据这个数据确定有多少个item
*
* 2、确定每个item里加载哪个view
*
* 3、在view里加载要显示的数据
*
* @author jiqinlin
*
*/
public class PersonAdapter extends ArrayAdapter{
private LayoutInflater layoutInflater = null;
private List<Person> persons;
public PersonAdapter(Context context, int textViewResourceId, List objects) {
super(context, textViewResourceId, objects);
layoutInflater = LayoutInflater.from(context);
persons = objects;
}
/**
* 获取adapter里有多少个数据项
*/
@Override
public int getCount() {
return persons.size();
}
@Override
public Object getItem(int position) {
return persons.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
/**
* 创建显示的数据界面
*
* Adapter的作用就是ListView界面与数据之间的桥梁,
* 当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View。
* 想过没有? 在我们的列表有1000000项时会是什么样的?是不是会占用极大的系统资源?
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
/*
// 优化前
ViewHolder holder = new ViewHolder();
convertView = layoutInflater.inflate(R.layout.main, null);
holder.id = (TextView)convertView.findViewById(R.id.id);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.age = (TextView)convertView.findViewById(R.id.age);
convertView.setTag(holder);
holder.id.setText(persons.get(position).getId());
holder.name.setText(persons.get(position).getName());
holder.age.setText(persons.get(position).getAge());
return convertView;
*/
// 优化后
ViewHolder holder;
if(convertView == null){
convertView = layoutInflater.inflate(R.layout.main, null);
holder = new ViewHolder();
holder.id = (TextView)convertView.findViewById(R.id.id);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.age = (TextView)convertView.findViewById(R.id.age);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.id.setText(persons.get(position).getId());
holder.name.setText(persons.get(position).getName());
holder.age.setText(persons.get(position).getAge());
return convertView;
}
/**
* 界面上的显示控件
*
* @author jiqinlin
*
*/
private static class ViewHolder{
private TextView id;
private TextView name;
private TextView age;
}
}
类LsActivity
package com.ljq.ls;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.ljq.domain.Person;
public class LsActivity extends ListActivity {
private ArrayList<Person> persons = new ArrayList<Person>();
private PersonAdapter personAdapter = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initData();
personAdapter =new PersonAdapter(LsActivity.this, R.layout.main, persons);
setListAdapter(personAdapter);
registerForContextMenu(getListView());
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Person person = persons.get(position);
Toast.makeText(LsActivity.this, person.getId()+":"+person.getName()
+":"+person.getAge(), Toast.LENGTH_LONG).show();
return;
}
private void initData(){
persons.add(new Person("序号", "姓名", "年龄"));
persons.add(new Person("1", "ljq1", "20"));
persons.add(new Person("2", "ljq2", "20"));
persons.add(new Person("3", "ljq3", "20"));
persons.add(new Person("4", "ljq4", "20"));
persons.add(new Person("5", "ljq5", "20"));
persons.add(new Person("6", "ljq6", "20"));
persons.add(new Person("7", "ljq7", "20"));
persons.add(new Person("8", "ljq8", "20"));
persons.add(new Person("9", "ljq9", "20"));
}
}
运行结果
希望本文所述对大家的Android程序设计有所帮助。
标签:Android,控件,ListView
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Apache Calcite进行SQL解析(java代码实例)
2023-06-26 23:11:59
![](https://img.aspxhome.com/file/2023/2/63322_0s.png)
JAVA代码实现MongoDB动态条件之分页查询
2022-05-21 12:44:11
实例讲解Java的Spring框架中的控制反转和依赖注入
2023-04-24 17:44:49
![](https://img.aspxhome.com/file/2023/8/76228_0s.jpg)
springboot实现基于aop的切面日志
2022-09-09 11:53:11
JDBC增删改查和查唯一的完整代码解析
2023-12-22 15:53:30
Java 八种基本类型和基本类型封装类
2023-11-26 14:15:16
![](https://img.aspxhome.com/file/2023/8/59898_0s.jpg)
JavaGUI常用三种布局使用介绍
2023-05-19 08:01:06
![](https://img.aspxhome.com/file/2023/6/73036_0s.png)
java实现随机验证码图片生成
2022-12-08 02:27:00
![](https://img.aspxhome.com/file/2023/1/82301_0s.jpg)
Java中URL传中文时乱码的解决方法
2022-05-17 02:16:55
Android PickerView实现三级联动效果
2023-02-25 15:05:47
如何基于JavaFX开发桌面程序
2023-10-30 11:06:19
C#操作字符串方法总结实例代码
2022-03-29 20:04:54
WebView的介绍与简单实现Android和H5互调的方法
2023-04-30 19:26:00
![](https://img.aspxhome.com/file/2023/7/101817_0s.png)
SpringBoot瘦身打包部署的实现
2021-09-09 16:54:21
![](https://img.aspxhome.com/file/2023/3/66673_0s.png)
Mapreduce分布式并行编程
2023-04-30 02:43:16
![](https://img.aspxhome.com/file/2023/4/97814_0s.jpg)
JAVA如何按字节截取字符串
2023-11-25 13:31:41
Android Support Library 标题栏(Toolbar)滚动效果实现方法
2022-10-24 03:25:12
![](https://img.aspxhome.com/file/2023/4/138974_0s.gif)
golang实现java uuid的序列化方法
2022-12-30 11:30:57
java虚拟机深入学习之内存管理机制
2023-07-03 21:15:40
![](https://img.aspxhome.com/file/2023/5/101785_0s.png)
SpringBoot集成Redis—使用RedisRepositories详解
2023-09-04 08:55:59
![](https://img.aspxhome.com/file/2023/7/67017_0s.png)