- 浏览: 139012 次
- 性别:
- 来自: 枣阳
文章分类
- 全部博客 (61)
- dwr (1)
- Flex (8)
- android (15)
- html转换成pdf (1)
- 八款开源 Android 游戏引擎 (巨好的资源) (1)
- url (0)
- Junit测试中找不到junit.framework.testcase (0)
- Junit (1)
- Java (4)
- spring (2)
- itext (1)
- JDBC (2)
- 正则表达式 (1)
- package (1)
- SVN (1)
- json (2)
- 常见问题 (1)
- SQL (1)
- Html5 (3)
- 看看 (1)
- 理论知识 (0)
- JavaScript (0)
- Jquery (0)
- MySQL MyISAM InnoDB 区别 (0)
- MySQL (0)
- struts2 标签 获取s:param的值 子页面获取 s:include s:param的值 (0)
- Oracle (1)
- Web (1)
- 性能 (0)
- Tomcat (0)
- Struts2 (5)
- tools (0)
- Exception (0)
- web开发问题 (0)
- log (0)
- Struts 2验证框架出错:403 for URL:http:////www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd (1)
- IOS (0)
- eclipse (0)
- webservice (0)
- AOP (0)
- View (0)
- 视图 (0)
- hsqldb (0)
- jar包 (0)
- Annotation (0)
- error at ::0 can't find referenced pointcut和error at ::0 formal unbound in point (0)
- Demo (0)
- 精彩文章 (0)
- rest (0)
- Test (0)
- 工具 (0)
- linux (0)
- 常用知识 (0)
- JavaScript 遍历JSON (0)
- 继承了之后还要不要注入的问题 (0)
- liunx (0)
- jQuery Mobile (1)
- ext (0)
- 二维码微信扫描 (0)
- 分享 (0)
- iOS7 (1)
- http (0)
- object-c (0)
- nginx (0)
- myEclipse10 (1)
- VM (0)
- window (0)
- server (0)
- lvs (0)
- 在线支付 (0)
- 安全技术 (0)
- 知识 (0)
- servlet (0)
- 支付 (0)
- mybatis (0)
- 服务器 (2)
- 使用SeaJS,require加载Jquery的时候总是为null (0)
- seaJs (0)
- 微信 (1)
最新评论
-
medlying:
html中的js能够被解析执行吗
Itext 实现 html转换成pdf -
794581572:
还是出现了乱码.. 编码用utf-8还是出了乱码
Itext 实现 html转换成pdf -
mbq820:
楼主为什么我的 提示不支持该字体:Unsupported fo ...
Itext 实现 html转换成pdf -
SwordShadow:
博主写的太好了,可以转载吗?
Itext 实现 html转换成pdf -
sinotao1:
写得非常好。
Struts2 中的数据传输
SimpleAdapter
SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便
SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文
参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致
参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id
参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称
参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应
SimpleAdapter可以使用自定义的ListView,然后setContentView即可。也可以直接使用系统自带的ListAcitivity,该ListActivity实现了ListView,显示ListView的时候做了很多优化。
ListActivity直接extends ListActivity即可,不需要在setContentView了
例子一:自定义布局,显示本地资源
如果直接继承ListAcitivty,则不需要自定义ListView,下面的是列表项单项显示格式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3px"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
/>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
Activity
package com.loulijun.demo13;
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.widget.SimpleAdapter;
public class Demo13Activity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.main, new String[] { "img", "title", "info" },
new int[] { R.id.img, R.id.title, R.id.info });
setListAdapter(adapter);
}
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("img", R.drawable.e001);
map.put("title", "小宗");
map.put("info", "电台DJ");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.e002);
map.put("title", "貂蝉");
map.put("info", "四大美女");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.e04b);
map.put("title", "奶茶");
map.put("info", "清纯妹妹");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.e04e);
map.put("title", "大黄");
map.put("info", "是小狗");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.e11a);
map.put("title", "hello");
map.put("info", "every thing");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", R.drawable.e11d);
map.put("title", "world");
map.put("info", "hello world");
list.add(map);
return list;
}
}
注:自定义ListView也有其好处,因为继承的ListAcitivity布局的样子已经定了下来,但是如果我们需要在ListView中实现某些效果,比如快速滚动条,就需要自定义了。另外如果你继承的比如是TabActivity等其他
Acitivty的话,就不能继承ListAcitivty了,因为JAVA是单继承,这时候还是需要自定义的ListView
如果自定义ListView而不是继承ListActivity需要如下样子实现
mylist.xml,在ListView中可以定义其他属性
-----------------------------------------------------------------------------------------
<?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="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
-----------------------------------------------------------------------------------------
如果使用自定义的ListView就需要在上面的代码修改一下下了,Acitivity其他部分都一样,区别在于灰色区域
public class Demo13Activity extends Activity {
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
lv = (ListView)findViewById(R.id.listview);
SimpleAdapter adapter = new SimpleAdapter(this, getData(),
R.layout.main, new String[] { "img", "title", "info" },
new int[] { R.id.img, R.id.title, R.id.info });
//setListAdapter(adapter);
lv.setAdapter(adapter);
}
运行结果:
|------------------------------------------------------------------------|
用户可以自定义布局,可以是线性布局,也可以是网格布局等等
接下来说说ViewBinder的使用,上面的例子中我们显示了本地资源,图片都是保存在本地的,但是用上面的方法显示网络上获取的图片却有问题,因为如果ListView要显示外部资源的话必须要设置ViewBinder,通过ViewBinder的绑定机制来显示网络资源,下面是个显示网络图片的例子(如果可能的话,最好还是使用BaseAdapter)
例子二:自定义布局,显示网络资源,ViewBinder的使用
由于需要访问网络资源,首先在你的清单文件中加入权限
package com.loulijun.demo13;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
public class Demo13Activity extends Activity {
private ListView lv;
private static final String iphoneUrl = "http://www.51aigoo.com/images/20100107/6b21df8c2419480e.jpg";
private static final String macbookproUrl = "http://www.esundigi.net/images/goods/20110317/6ece8f319694f0b1.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylist);
lv = (ListView)findViewById(R.id.listview);
SimpleAdapter adapter = new SimpleAdapter(
this,
getData(),
R.layout.main,
new String[] {"img","title","info"},
new int[] { R.id.img, R.id.title, R.id.info});
//setListAdapter(adapter);
adapter.setViewBinder(new MyViewBinder());
lv.setAdapter(adapter);
}
//获取网络图片资源,返回类型是Bitmap,用于设置在ListView中
public Bitmap getBitmap(String httpUrl)
{
Bitmap bmp = null;
//ListView中获取网络图片
try {
URL url = new URL(httpUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream is = conn.getInputStream();
bmp = BitmapFactory.decodeStream(is);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmp;
}
//ListView上需要显示的数据
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("img", getBitmap(iphoneUrl));
map.put("title", "iphone4");
map.put("info", "可远观而买不起嫣");
list.add(map);
map = new HashMap<String, Object>();
map.put("img", getBitmap(macbookproUrl));
map.put("title", "Macbook pro");
map.put("info", "明年买个玩玩");
list.add(map);
return list;
}
}
//实现ViewBinder接口
class MyViewBinder implements ViewBinder
{
/**
* view:要板顶数据的视图
* data:要绑定到视图的数据
* textRepresentation:一个表示所支持数据的安全的字符串,结果是data.toString()或空字符串,但不能是Null
* 返回值:如果数据绑定到视图返回真,否则返回假
*/
@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if((view instanceof ImageView)&(data instanceof Bitmap))
{
ImageView iv = (ImageView)view;
Bitmap bmp = (Bitmap)data;
iv.setImageBitmap(bmp);
return true;
}
return false;
}
}
运行结果:
原文:http://www.cnblogs.com/loulijun/archive/2011/12/27/2303488.html
发表评论
-
Android View三种属性——VISIBLE,INVISIBLE,GONE
2014-08-12 15:12 0INVISIBLE:仍旧会占用空间,只是内容不显示。 GO ... -
android Gson的使用
2014-07-29 15:47 0相对于较为传统的Json解析来说,google共享的开源Gs ... -
利用HTML5开发Android笔记
2014-01-08 19:32 0http://johncookie.iteye.com/bl ... -
android 中 webview 怎么用 localStorage?
2014-01-08 19:30 0我在 android里面 使用html5的 localSto ... -
Android 处理流程
2013-11-20 00:50 0今天看了一下别人写的android代码。 大致了解了and ... -
Android开发规范
2013-08-17 08:50 0[size=large]一、Android编码 ... -
android关于AndroidManifest.xml详细分析
2013-07-29 23:57 0http://my.eoe.cn/1087692/arch ... -
Android 精华文章
2013-07-29 23:33 0AndroidManifest.xml http:/ ... -
记事本
2013-04-15 16:28 0对于WebChromeClient,WebViewClien ... -
关于android WebViewClient的方法解释
2013-04-15 14:49 31841、public boolean shouldOverri ... -
PhoneGap 在Android 手机上的全屏(FullScreen)问题
2013-04-15 11:13 1699(注:本方法只适用于PhoneGap 0.93或更高版本) ... -
android 界面布局
2012-06-12 23:26 1476布局: 在 android 中我们常用的布局方式有这 ... -
Activity利用Handler与Thread进行通讯,写了一个简单Demo
2012-06-11 23:32 5831最近写了一个列子,想跟大家一起分享. 用android.os. ... -
Android 文件的保存和读取
2012-05-30 09:55 1327Android 给我们提供了两个方法返回输入、输出流,分别为: ... -
Android中strings.xml文件
2012-05-29 18:13 1500如果动态的修改Android中strings.xml文件中的值 ... -
Android 中LayoutInflater的使用
2012-05-28 17:54 1432在实际开发种LayoutInflater这个类还是非常有用的, ... -
Android 应用程序之间数据共享—ContentProvider
2012-05-25 11:56 1287在Android 应用程序之间数据共享—-ContentRes ... -
Android Bind Service
2012-05-24 10:57 1159启动Service有两种方式:startService 与 b ... -
Android BroadcastReceiver 学习
2012-05-23 14:12 1348BroadcastReceiver 用于异步 ... -
Android DDMS
2012-05-22 10:20 1135DDMS 的全称是Dalvik Debug Monitor S ...
相关推荐
ListView使用simpleAdapter填充实现,数据结构是HashMap,对应的我的博客地址是: http://blog.csdn.net/u012320459/article/details/47667869
android-world-of-listview-android
Android-react-native-timeline-listview.zip,React本机应用程序的时间线组件,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有安全性优势。
android-pulltorefresh-listview 下拉刷新ListView
ListView和SimpleAdapter的简单应用
It works in all version of Android and it's very easy to adapt to your project. How to use with Gradle Simply add the repository to your build.gradle file: repositories { jcenter() maven { url '...
drag-sort-listview是一个支持拖拽排序和左右滑动删除功能的自定义ListView.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
•Android---UI篇---Tab Layout(选项卡布局) • •Andorid---UI篇---TableLayout(表格布局) ...•Android---UI篇---ListView之SimpleCursorAdapter(列表)---3 • •Android---UI篇---Menu(菜单)
SimpleAdater 简单适配器 配合listView使用 ,将数据展示在列表中 进阶1
2-android-pulltorefresh-listview 下拉刷新ListView.zip
react-native-sortable-listview, ListView的拖放功能包装,用于响应本机 react-native-sortable-listviewListView的拖放功能包装,用于响应本机。 拖动时允许拖放自动滚动行。将它添加到你的项目中运行 npm install...
Android开发笔记---ListView
Android--开发--ListView上下翻页效果
Android--开发-- ListView下拉刷新 Demo
Android中尝试气泡短信编程初探实例 ListView使用SimpleAdapter适配器详解 具体参考小魏博客:http://blog.csdn.net/xiaowei_cqu/article/details/7045497
21.[开源][安卓][拖拽]drag-sort-listview-master DragSortListView(DSLV)是Android ListView的一个扩展,支持拖拽排序和左右滑动删除功能。重写了TouchInterceptor(TI)类来提供更加优美的拖拽动画效果。 DSLV...
Android--开发--ListView 中的item随意拖动
android-pro-listview.7z
android-custom-listview.7z
Android--开发--ListView 实现点击侧边A-Z快速查找