`

【转】android 有关listview的adapter的相关用法

阅读更多

 ListView 是android开发中最常用的组件之一,它通过一个adapter来构建显示通常有三种adapter可以使用ArrayAdapterSimpleAdapter,CursorAdapter。CursorAdapter主要正对数据库使用,下面通过例子介绍ArrayAdapterSimpleAdapter的简单使用:
   1:ArrayAdapter 它接受一个数组或者List作为参数来构建。
      一下通过简单例子说明:
  创建Test 继承ListActivity 这里我们传入一个string数组

 

public class ListTest extends ListActivity {
    /** *//** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] sw = new String[100];
        for (int i = 0; i < 100; i++) {
            sw[i] = "listtest_" + i;
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,sw);//使用系统已经实现好的xml文件simple_list_item_1
        setListAdapter(adapter);
    }
}

 我们通过构建list,并设置每项为一个map来实现:
代码:创建TestList类继承Activity

super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, Object> user = new HashMap<String, Object>();
            user.put("img", R.drawable.user);
            user.put("username", "姓名(" + i+")");
            user.put("age", (20 + i) + "");
            users.add(user);
        }
        SimpleAdapter saImageItems = new SimpleAdapter(this,
                users,// 数据来源
                R.layout.user,//每一个user xml 相当ListView的一个组件 
                new String[] { "img", "username", "age" },
                // 分别对应view 的id
                new int[] { R.id.img, R.id.name, R.id.age });
        // 获取listview
        ((ListView) findViewById(R.id.users)).setAdapter(saImageItems);

 

下面是main.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:text="用户列表" android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:background="#DAA520"
        android:textColor="#000000">
    </TextView>
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView android:text="姓名" 
            android:gravity="center" android:layout_width="160px"
            android:layout_height="wrap_content" android:textStyle="bold"
            android:background="#7CFC00">
        </TextView>
        <TextView android:text="年龄" 
            android:layout_width="170px" android:gravity="center"
            android:layout_height="wrap_content" android:textStyle="bold"
            android:background="#F0E68C">
        </TextView>
    </LinearLayout>
<ListView android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:id="@+id/users">
    </ListView>
</LinearLayout>

 

之中listView前面的可以说是标题行,listview相当于用来显示数据的容器,里面每行是一个用户信息,而用户信息是样子呢?
看看use.xml

<?xml version="1.0" encoding="utf-8"?>  
<TableLayout    
         android:layout_width="fill_parent"    
         xmlns:android="http://schemas.android.com/apk/res/android"    
         android:layout_height="wrap_content"    
         > 
         <TableRow > 
         <ImageView    
               android:layout_width="wrap_content"    
               android:layout_height="wrap_content"   
               android:id="@+id/img">    
         </ImageView>  
         <TextView    
               android:layout_height="wrap_content"    
               android:layout_width="150px"    
               android:id="@+id/name">  
         </TextView>  
         <TextView    
               android:layout_height="wrap_content"   
               android:layout_width="170px"    
               android:id="@+id/age">  
         </TextView> 
         </TableRow> 
</TableLayout> 

 

也就是说每行包含了一个img 和2个文字信息
这个文件以参数的形式通过adapter在listview中显示。
也就是:
SimpleAdapter saImageItems = new SimpleAdapter(this,
                users,
// 数据来源
                R.layout.user,//每一个user xml 相当ListView的一个组件 
                new String[] "img""username""age" },

                
// 分别对应view 的id
                new int[] { R.id.img, R.id.name, R.id.age });

 

 

 

  • 大小: 70.2 KB
  • 大小: 86.1 KB
分享到:
评论

相关推荐

    Android listview与adapter用法

    本篇文章将详细探讨Android中的Adapter用法及其在ListView中的应用。 1. **Adapter的概念** - `Adapter`是Android中一个核心的概念,它扮演着数据源与UI组件之间的桥梁角色。具体来说,Adapter负责将后端的数据...

    Android-listview与adapter用法最新版本

    本文档主要讲述的是Android-listview与adapter用法;要使用ListView,首先要了解什么是适配器。适配器是一个连接数据和AdapterView(ListView就是一个典型的AdapterView,后面还会学习其他的)的桥梁,通过它能有效...

    android中一个简单的LIstView用法例子

    这个简单的ListView用法例子将帮助初学者理解和掌握如何在实际项目中运用ListView。 首先,我们来了解一下ListView的基本结构。ListView是Android SDK中的一个视图类,继承自AbsListView。它的核心功能是展示一个可...

    Android_ListView控件基本用法

    ### Android ListView 控件基本用法详解 #### 一、引言 在Android应用开发中,经常需要展示大量的数据项,并且这些数据项通常具有相似的结构。为了有效地处理这类需求,Android提供了一种非常有用的控件——`...

    android ListView简单用法

    这个教程将详细介绍Android ListView的基本用法。 ### 1. 添加ListView到布局 首先,在XML布局文件中添加ListView组件。一个基本的ListView元素如下所示: ```xml &lt;ListView android:id="@+id/list_view" ...

    Android中ListView的用法

    在Android开发中,ListView是一个非常重要的组件,它用于显示一列可滚动的项目,通常用于显示大量的数据。本教程将深入讲解如何在Android应用中使用ListView,以及如何结合不同的数据适配器,如ArrayAdapter和...

    Android studio异步数据加载ListView+Adapter+AsyncTask

    本教程将详细讲解如何在Android Studio中结合ListView、Adapter和AsyncTask实现异步数据加载。 首先,我们需要了解ListView的基本用法。ListView是一种可以滚动并显示多个同种类型的视图组件,通过Adapter进行数据...

    Android ListView控件基本用法

    Android ListView 控件基本用法 Android ListView 控件是一种基础控件,主要用于展示列表数据。在 Android 开发中,ListView 控件是最常用的控件之一。下面我们将详细介绍 Android ListView 控件的基本用法。 一、...

    android应用源码Android重写listview实现下拉刷新.zip

    通过这个源码学习,你可以掌握Android中ListView的高级用法,以及如何实现下拉刷新功能,这对于任何Android开发者来说都是必备的技能。实践中,你还可以结合其他第三方库,如SwipeRefreshLayout,进一步优化用户体验...

    android 一些listview例子

    通过学习这些例子,开发者不仅可以掌握ListView的基本用法,还能理解如何根据实际需求进行定制和优化,从而在Android应用开发中游刃有余。在实际项目中,结合“androidlee”中的代码示例,将有助于更深入地理解和...

    Android开发ListView实现三级菜单

    首先,我们需要了解ListView的基本用法。ListView通过Adapter来绑定数据,Adapter是连接ListView和数据源的桥梁。常见的Adapter有ArrayAdapter、SimpleAdapter等,开发者可以根据实际需求选择或自定义Adapter。在...

    android--ListView实现带标题栏效果(附源码)

    在Android开发中,ListView是一种常用的UI组件,常用于展示大量数据列表。本教程将详细介绍如何在ListView中实现带标题栏的效果。标题栏通常用于显示分类信息或者操作按钮,为用户提供更好的交互体验。以下是对实现...

    最简单listview + adapter

    在Android开发中,ListView是一种非常常见的控件,用于展示大量数据列表。它的高效性和可滚动性使得它在各种场景下都十分实用,如联系人列表、应用列表等。本教程将针对初学者,讲解如何使用ListView和Adapter来构建...

    Android ListView实例源码

    综上所述,"Android ListView 实例源码"应包含了ListView的基本用法、自定义Adapter的实现、事件监听、性能优化等多个方面。通过研究这个源码,开发者可以深入理解ListView的工作原理,并学习如何在实际项目中高效地...

    android的listview和gridView的例子

    总结来说,ListView和GridView是Android中展示数据的重要控件,它们依赖于Adapter来实现数据和视图的交互。理解并熟练掌握Adapter的工作原理和使用方法,对于开发出高效、动态的用户界面至关重要。通过分析提供的...

    Android之ListView+BaseAdapter左右按键切换

    首先,我们需要了解ListView的基本用法。ListView在XML布局文件中可以通过`&lt;ListView&gt;`标签进行声明,并且通常会设置一个id以便在Java代码中引用。ListView的显示效果取决于其使用的Adapter,所以我们需要创建一个...

    Android开发教程:仿通讯录ListView小例子

    这个练习不仅可以帮助你掌握ListView的基本用法,还能让你了解到如何在实际项目中整合数据和视图,提升Android开发技能。记得实践是检验真理的唯一标准,动手尝试才能更好地理解和掌握这些知识点。

    Android之Adapter用法总结

    ### Android之Adapter用法总结 #### 一、概念与作用 **Adapter** 是 Android 开发中连接后端数据和前端视图的一个关键组件。它作为数据和用户界面(UI)之间的桥梁,在Android开发中扮演着极其重要的角色。尤其是...

    android listview

    首先,让我们了解ListView的基本用法。ListView通过适配器(Adapter)来绑定数据源,它可以是ArrayList、Cursor等。Adapter负责将数据转化为ListView项的视图(View)。开发者通常需要自定义一个继承自BaseAdapter的...

    安卓手机 ListView控件的三种使用方式

    在Android开发中,ListView是应用界面中非常常见的一种控件,它用于展示大量数据的列表形式,具有良好的滚动性能和可扩展性。本教程将详细讲解如何在安卓手机上使用ListView控件,从基础到进阶,通过四个案例来帮助...

Global site tag (gtag.js) - Google Analytics