Android UI:ListView - SimpleAdapter实例详解_Android

Android UI:ListView -- SimpleAdapter

SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便。

layout :

<?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="horizontal">
    <ListView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:divider="#7f00"    //分割线
      android:dividerHeight="2dp"
      android:id="@+id/listview_sample"/>
</LinearLayout>

header layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:src="@mipmap/ic_launcher"/>
</LinearLayout>

自定义布局  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="match_parent"
  android:orientation="horizontal">
  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="3px"
    android:id="@+id/img"/>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:textSize="16sp"
      android:id="@+id/title"/>
    <TextView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/info"
      android:textSize="16sp"/>
  </LinearLayout>

</LinearLayout>

Java 代码:

public class SampleAdapterActivity extends Activity {

  private ListView mListview;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sampleadapter_layout);
    mListview = (ListView) findViewById(R.id.listview_sample);
    SimpleAdapter adapter = new SimpleAdapter(this,
        getData(), //数据来源
        R.layout.item_listview, //对应item view
        new String[]{"img","title","info"}, //data 中对应值
        new int[]{R.id.img,R.id.title,R.id.info}); //填充layout位置
    mListview.setHeaderDividersEnabled(true);   //是否显示头view 的分割线
    View header = View.inflate(this,R.layout.listview_header,null);
    View footer = View.inflate(this,R.layout.listview_header,null);
    mListview.addHeaderView(header);  //添加头部view
    mListview.addFooterView(footer);   //添加底部view
    mListview.setAdapter(adapter);
  }

  @Override
  protected void onResume() {
    super.onResume();
  }
  private List<? extends Map<String,?>> getData() {
    List<Map<String,Object>> items = new ArrayList<Map<String, Object>>();
    for (int i = 0; i < 5; i++) {
      Map<String,Object> item = new HashMap<String,Object>();
      item.put("img",R.mipmap.ic_launcher);
      item.put("title","title -- " + i );
      item.put("info","info -- " + i );
      items.add(item);
    }
    return items;
  }
}

 显示效果

 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
simpleadapter
android adapter详解、android创意实例详解、simpleadapter、simpleadapter的用法、simpleadapter参数,以便于您获取更多的相关知识。

时间: 2024-07-31 02:06:26

Android UI:ListView - SimpleAdapter实例详解_Android的相关文章

Android UI:ListView - SimpleAdapter实例详解

Android UI:ListView -- SimpleAdapter SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便. layout : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo

Android编程之SurfaceView实例详解_Android

本文实例讲述了Android编程之SurfaceView用法.分享给大家供大家参考,具体如下: 关于surfaceView相关知识: View和SurfaceView主要区别: 1. View只能在UI线程中刷新,而SurfaceView可以在子线程中刷新 2. SurfaceView可以控制刷新频率 SurfaceView几个重要的方法: 1. 继承SurfaceView 后调用getHolder()方法可以获取到mSurfaceHolder对象这个对于可以控制SurfaceView的绘制 2

Android的搜索框架实例详解_Android

基础知识 Android的搜索框架将代您管理的搜索对话框,您不需要自己去开发一个搜索框,不需要担心要把搜索框放什么位置,也不需要担心搜索框影响您当前的界面.所有的这些工作都由SearchManager类来为您处理(以下简称"搜索管理器"),它管理的Android搜索对话框的整个生命周期,并执行您的应用程序将发送的搜索请求,返回相应的搜索关键字. 当用户执行一个搜索,搜索管理器将使用一个专门的Intent把搜索查询的关键字传给您在配置文件中配置的处理搜索结果的Activity.从本质上讲

Android控件之ListView用法实例详解_Android

本文实例讲述了Android控件之ListView用法.分享给大家供大家参考.具体如下: 示例一: 在android开发中ListView是比较常用的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示. main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/LinearLayout01" androi

Android Dialog对话框用法实例详解_Android

本文实例讲述了Android Dialog对话框用法.分享给大家供大家参考,具体如下: Activities提供了一种方便管理的创建.保存.回复的对话框机制,例如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int),dismissDialog(int)等方法,如果使用这些方法的话,Activity将通过getOwnerActivity()方法返回该Activity管理的对话框(dialog). 1. onCreat

Android程序设计之AIDL实例详解_Android

通常来说,AIDL这项技术在我们的应用开发过程中并不是很常用,虽然新浪微博提供了SSO登录,但是其原理就是使用AIDL.本文就以完整的实例形式讲述了AIDL的原理及实现方法. AIDL(AndRoid接口描述语言)是一种借口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成 AIDL可识别的参数(可能是多个参数), 然后使用AIDL来传递这些参数,

android之camera用法实例详解_Android

本文实例讲述了android之camera用法.分享给大家供大家参考.具体如下: 1.关于预览横竖差90度的问题 原因分析 经过查证和实验,可以证实:Android提供的SDK(android.hardware.Camera)里大概不能正常的使用竖屏(portrait layout)加载照相机,当用竖屏模式加载照相机时会产生以下情况: ①. 照相机成像左倾90度(倾斜): ②. 照相机成像长宽比例不对(失比). 之所以是"大概",原因是因为可能可以通过一些比较复杂的手段解决.如果以上成

Android Toast通知用法实例详解_Android

本文实例讲述了Android Toast通知用法.分享给大家供大家参考,具体如下: Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. 1.默认用法 复制代码 代码如下: Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show(); 2.Fragment中的用法 复制代码 代码如下: Toast.makeText(getActivity(),"网络连接错误,请检

Android发送邮件的方法实例详解_Android

本文实例讲述了Android发送邮件的方法.分享给大家供大家参考,具体如下: 在android手机中实现发送邮件的功能也是不可缺少的.如何实现它呢?下面以简单的例子进行说明. 程序如下: import java.util.regex.Matcher; import java.util.regex.Pattern; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import