android学习之ListView总结
来源:互联网 发布:杭州龙席网络怎么样 编辑:程序博客网 时间:2024/04/29 14:29
ListView总结
在原来的基础上,增加了点击后的消息响应函数。
源代码下载:http://download.csdn.net/detail/nuptboyzhb/4484920
列表的显示需要三个元素:
1.ListVeiw 用来展示列表的View。
2.适配器 用来把数据映射到ListView上的中介。
3.数据 具体的将被映射的字符串,图片,或者基本组件。
分类:
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
我们从最简单的ListView开始
MyListView.java
- package org.lee.android;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- import android.widget.AdapterView.OnItemClickListener;
- publicclass MyListView extends Activity implementsOnItemClickListener{
- private ListView listView;
- //private List<String> data = new ArrayList<String>();
- @Override
- publicvoid onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- listView = new ListView(this);
- List<String>list=getData();
- ArrayAdapter<String>adapter=newArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list);
- listView.setAdapter(adapter);
- setContentView(listView);
- listView.setOnItemClickListener(this);//绑定监听接口
- }
- private List<String> getData(){
- List<String> data = newArrayList<String>();
- data.add("测试数据1");
- data.add("测试数据2");
- data.add("测试数据3");
- data.add("测试数据4");
- return data;
- }
- /*实现OnItemClickListener接口*/
- @Override
- publicvoid onItemClick(AdapterView<?> parent, View v, int position, long id){
- Log.d("Click","you have click!");
- switch(position){
- case 0:
- Toast.makeText(MyListView.this,
- "你点击了第一个!",
- Toast.LENGTH_SHORT).show();
- break;
- case 1:
- Toast.makeText(MyListView.this,
- "你点击了第二个!",
- Toast.LENGTH_SHORT).show();
- break;
- case 2:
- Toast.makeText(MyListView.this,
- "你点击了第三个!",
- Toast.LENGTH_SHORT).show();
- break;
- case 3:
- Toast.makeText(MyListView.this,
- "你点击了第四个!",
- Toast.LENGTH_SHORT).show();
- break;
- }
- }
- }
package org.lee.android;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;publicclass MyListView extends Activity implementsOnItemClickListener{ private ListView listView; //private List<String> data = new ArrayList<String>(); @Override publicvoid onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); listView = new ListView(this); List<String>list=getData(); ArrayAdapter<String>adapter=newArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,list); listView.setAdapter(adapter); setContentView(listView); listView.setOnItemClickListener(this);//绑定监听接口 } private List<String> getData(){ List<String> data = newArrayList<String>(); data.add("测试数据1"); data.add("测试数据2"); data.add("测试数据3"); data.add("测试数据4"); return data; } /*实现OnItemClickListener接口*/ @Override publicvoid onItemClick(AdapterView<?> parent, View v, int position, long id){ Log.d("Click","you have click!"); switch(position){ case 0: Toast.makeText(MyListView.this, "你点击了第一个!", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MyListView.this, "你点击了第二个!", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MyListView.this, "你点击了第三个!", Toast.LENGTH_SHORT).show(); break; case 3: Toast.makeText(MyListView.this, "你点击了第四个!", Toast.LENGTH_SHORT).show(); break; } }}
上面代码使用了ArrayAdapter(Context context,int textViewResourceId, List<T>objects)来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的 构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布 局,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时 用setAdapter()完成适配的最后工作。
SimpleAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方便显示而已。
下面的程序是实现一个带有图片的类表。
首先需要定义好一个用来显示每一个列内容的xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <ImageViewandroid:idImageViewandroid:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="5px"/>
- <LinearLayout android:orientation="vertical"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- <TextView android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFFFFFFF"
- android:textSize="22px" />
- <TextView android:id="@+id/info"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFFFFFFF"
- android:textSize="13px" />
- </LinearLayout>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageViewandroid:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5px"/> <LinearLayout android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="22px" /> <TextView android:id="@+id/info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#FFFFFFFF" android:textSize="13px" /> </LinearLayout></LinearLayout>
下面是实现代码java:
- package org.lee.android;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- importandroid.widget.AdapterView.OnItemClickListener;
- /**
- * @author allin
- *
- */
- publicclass MyListView3 extends ListActivity implementsOnItemClickListener{
- // private List<String> data = new ArrayList<String>();
- @Override
- publicvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
- new String[]{"title","info","img"},
- newint[]{R.id.title,R.id.info,R.id.img});
- setListAdapter(adapter);
- ListViewlistView=getListView();
- listView.setOnItemClickListener(this);
- }
- private List<Map<String,Object>> getData() {
- List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
- Map<String, Object> map =new HashMap<String, Object>();
- map.put("title", "G1");
- map.put("info", "google 1");
- map.put("img",R.drawable.i1);
- list.add(map);
- map = new HashMap<String,Object>();
- map.put("title", "G2");
- map.put("info", "google 2");
- map.put("img",R.drawable.i2);
- list.add(map);
- map = new HashMap<String,Object>();
- map.put("title", "G3");
- map.put("info", "google 3");
- map.put("img",R.drawable.i3);
- list.add(map);
- return list;
- }
- /*实现OnItemClickListener接口*/
- @Override
- publicvoid onItemClick(AdapterView<?> parent,View v, int position, long id){
- Log.d("Click","you have click!");
- switch(position){
- case 0:
- Toast.makeText(MyListView3.this,
- "你点击了第一个!",
- Toast.LENGTH_SHORT).show();
- break;
- case 1:
- Toast.makeText(MyListView3.this,
- "你点击了第二个!",
- Toast.LENGTH_SHORT).show();
- break;
- case 2:
- Toast.makeText(MyListView3.this,
- "你点击了第三个!",
- Toast.LENGTH_SHORT).show();
- break;
- }
- }
- }
package org.lee.android; import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map; import android.app.ListActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.AdapterView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;importandroid.widget.AdapterView.OnItemClickListener;/** * @author allin * */publicclass MyListView3 extends ListActivity implementsOnItemClickListener{ // private List<String> data = new ArrayList<String>(); @Override publicvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist, new String[]{"title","info","img"}, newint[]{R.id.title,R.id.info,R.id.img}); setListAdapter(adapter); ListViewlistView=getListView(); listView.setOnItemClickListener(this); } private List<Map<String,Object>> getData() { List<Map<String,Object>> list = new ArrayList<Map<String, Object>>(); Map<String, Object> map =new HashMap<String, Object>(); map.put("title", "G1"); map.put("info", "google 1"); map.put("img",R.drawable.i1); list.add(map); map = new HashMap<String,Object>(); map.put("title", "G2"); map.put("info", "google 2"); map.put("img",R.drawable.i2); list.add(map); map = new HashMap<String,Object>(); map.put("title", "G3"); map.put("info", "google 3"); map.put("img",R.drawable.i3); list.add(map); return list; } /*实现OnItemClickListener接口*/ @Override publicvoid onItemClick(AdapterView<?> parent,View v, int position, long id){ Log.d("Click","you have click!"); switch(position){ case 0: Toast.makeText(MyListView3.this, "你点击了第一个!", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(MyListView3.this, "你点击了第二个!", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(MyListView3.this, "你点击了第三个!", Toast.LENGTH_SHORT).show(); break; } }}
使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键 值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。
- android学习之ListView总结
- android学习之ListView总结
- Android ListView学习总结
- Android-ListView 学习总结
- Android学习之ListView
- Android学习之 Listview
- android之ListView学习
- Android学习之ListView
- Android研究院之ListView原理学习与优化总结
- Android研究院之ListView原理学习与优化总结
- Android研究院之ListView原理学习与优化总结
- Android之ListView原理学习与优化总结
- Android研究院之ListView原理学习与优化总结
- Android之ListView原理学习与优化总结
- Android之ListView原理学习与优化总结
- Android研究院之ListView原理学习与优化总结
- Android之ListView原理学习与优化总结
- Android之ListView原理学习与优化总结
- Android学习之BroadcastReceiver总结
- Nexus s Android 4.1.1 通过USB共享Ubuntu网络
- 用MyEclipse 10在Tomcat 6上搭建Structs2.1+Spring2.5+Hibernate3.2框架实例
- 学习android之Service
- libtiff+opencv2.3.1+vs 2005 编译tiff出错
- android学习之ListView总结
- java 堆、栈和常量池
- android学习之选择媒体库中的图片
- 网络基础之几个基础问题(整理)
- Android开发之文件浏览器
- visual tudio - vtune, Administrative privileges are required
- 红帽linux5.5 linux 开发环境的搭建 1
- Java网络编程总结
- 包含Iphlpapi.h 编译错误 error C2065: 'HRASCONN'