Android列表视图(ListView)

ListView是一个显示滚动项列表的示视图组(viewgroup),通过使用适配器(Adapter)把这些列表项自动插入到列表中。适配器比如从一个数组或是数据库查询获取到数据,然后转换每一项成为可放入到列表的视图。

列表的显示需要三个元素:

(1)    ListView:用来展示列表的view。

(2)    适配器:用来把数据映射到ListView上。

(3)    数据:具体的将被映射的字符串、图片或是基本组件。

图1

对于我们如何使用适配器来动态插入view,参考链接:

http://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

1.      使用加载器(Using a Loader)

为了实现异步加载数据,且为了避免在查询时阻塞APP主线程,从Android3.0开始引入CursorLoader,这是一种查询Cursor的标准方式。当CursorLoader接收到Cursor结果(查询结束),LoaderCallbacks接收到对onLoadFinished()方法的回调,此方法使用新的Cursor来更新适配器,然后列表视图显示这个结果。

虽然CursorLoader的API是从Android3.0(API 11)开始引入的,但可以通过使用支持的库来使用:http://developer.android.com/tools/extras/support-library.html,但要求Android系统版本在1.6及之后的版本。

public
class ListViewLoader
extends ListActivity

        implements
LoaderManager.LoaderCallbacks<Cursor>
{



    // This is the Adapter being used to display the list's data

    SimpleCursorAdapter mAdapter;



    // These are the Contacts rows that we will retrieve

    static final
String[] PROJECTION
= new
String[] {ContactsContract.Data._ID,

            ContactsContract.Data.DISPLAY_NAME};



    // This is the select criteria

    static final
String SELECTION =
"((" +

            ContactsContract.Data.DISPLAY_NAME
+ " NOTNULL)AND ("
+

            ContactsContract.Data.DISPLAY_NAME
+ " != ''))";



    @Override

    protected void onCreate(Bundle savedInstanceState)
{

        super.onCreate(savedInstanceState);



        // Create a progress bar to display whilethe list loads

        ProgressBar progressBar
= new ProgressBar(this);

        progressBar.setLayoutParams(new
LayoutParams(LayoutParams.WRAP_CONTENT,

                LayoutParams.WRAP_CONTENT,
Gravity.CENTER));

        progressBar.setIndeterminate(true);

        getListView().setEmptyView(progressBar);



        // Must add the progress bar to the root ofthe layout

        ViewGroup root
= (ViewGroup) findViewById(android.R.id.content);

        root.addView(progressBar);



        // For the cursor adapter, specify whichcolumns go into which views

        String[] fromColumns
= {ContactsContract.Data.DISPLAY_NAME};

        int[] toViews
= {android.R.id.text1};
// The TextViewin simple_list_item_1



        // Create an empty adapter we will use todisplay the loaded data.

        // We pass null for the cursor, then updateit in onLoadFinished()

        mAdapter =
new SimpleCursorAdapter(this,


                android.R.layout.simple_list_item_1,
null,

                fromColumns, toViews,
);

        setListAdapter(mAdapter);



        // Prepare the loader.  Eitherre-connect with an existing one,

        // or start a new one.

        getLoaderManager,
null,
this);

    }



    // Called when a new Loader needs to be created

    public Loader<Cursor> onCreateLoader(int
id, Bundle args)
{

        // Now create and return a CursorLoaderthat will take care of

        // creating a Cursor for the data beingdisplayed.

        return new
CursorLoader(this,
ContactsContract.Data.CONTENT_URI,

                PROJECTION, SELECTION,
null,
null);

    }



    // Called when a previously created loader has finished loading

    public void onLoadFinished(Loader<Cursor>
loader, Cursor data)
{

        // Swap the new cursor in.  (Theframework will take care of closing the

        // old cursor once we return.)

        mAdapter.swapCursor(data);

    }



    // Called when a previously created loader is reset, making the dataunavailable

    public void onLoaderReset(Loader<Cursor>
loader) {

        // This is called when the last Cursorprovided to onLoadFinished()

        // above is about to be closed.  We needto make sure we are no

        // longer using it.

        mAdapter.swapCursor(null);

    }



    @Override

    public void onListItemClick(ListView l,
View v,
int position,
long id) {

        // Do something when a list item is clicked

    }

}

链接:

Android开发者:

http://developer.android.com/guide/topics/ui/layout/listview.html

Android之ListView原理学习与优化总结

http://mzh3344258.blog.51cto.com/1823534/889879

androidListView详解

http://www.cnblogs.com/allin/archive/2010/05/11/1732200.html

Android的CursorLoader用法小结

http://www.linuxidc.com/Linux/2013-05/84572.htm

Android列表视图(List View)的更多相关文章

  1. Android——列表视图(ListView)

    列表视图是android中最常用的一种视图组件,它以垂直列表的形式列出需要显示的列表项.在android中有两种方法向屏幕中添加列表视图:一种是直接使用ListView组件创建:另外一种是让Activ ...

  2. Android的视图(View)组件

    Android的绝大部分UI组件都放在android.widget包及其子包.android,view包及其子包中,Android应用的所有UI组件都继承了View类,View组件非常类似于Swing ...

  3. Android——列表视图 ListView(三)BaseAdapter

    activity_activitybase.xml <?xml version="1.0" encoding="utf-8"?> <ListV ...

  4. Android列表视图ListView和ListActivity-android学习之旅(二十四)

    ListView简介 ListView是android中常用的一种控件,创建ListView有两种方式: 1.在xml中使用ListView控件创建. 2.使用activity继承ListActivi ...

  5. Android——列表视图 ListView(二)SimpleAdapter

    SimpleAdapter:可显示文字加图片 activity_activitysimple.xml <?xml version="1.0" encoding="u ...

  6. Android——列表视图 ListView(一)Arrayadapter

    一.ArrayAdapter 只显示文字 activitylistview_layout.xml <?xml version="1.0" encoding="utf ...

  7. Android应用开发学习之列表视图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 列表视图我们会经常用到,可以通过两种方式来创建列表视图,一种方式是直接使用ListView组件创建,另一种方式是通过 ...

  8. 【Android】9.3 自定义列表视图的外观

    分类:C#.Android.VS2015: 创建日期:2016-02-18 一.简介 自定义的列表视图通常用Resources/Layout文件夹下的axml文件中的资源来声明,适配器则通过Id去加载 ...

  9. <Android>列表、网格、画廊视图及适配器的绑定

    列表视图和适配器的绑定 列表视图既可以使用ListView组件,也可以继承ListActivity.显示可以是ArrayAdapter,也可以是游标SimpleCursorAdapter,还可以是继承 ...

随机推荐

  1. linux设备驱动模型

    尽管LDD3中说对多数程序员掌握设备驱动模型不是必要的,但对于嵌入式Linux的底层程序员而言,对设备驱动模型的学习非常重要. Linux设备模型的目的:为内核建立一个统一的设备模型,从而又一个对系统 ...

  2. mysql数据库本地化操作

    <?php if(!defined('SITE_PATH')){ define('SITE_PATH',dirname(dirname(__FILE__))); } $dbconfig=incl ...

  3. 按键消抖VERILOG实现

    对于消抖,有很多种写法.今天分享一下我的写法. 基本思路: 1. 看图                     图1                                           ...

  4. Java线程同步(synchronized)——卖票问题

    卖票问题通常被用来举例说明线程同步问题,在Java中,采用关键字synchronized关键字来解决线程同步的问题. Java任意类型的对象都有一个标志位,该标志位具有0,1两种状态,其开始状态为1, ...

  5. 编译时IOS Device 无法选择的情况

    问题描述:当你项目开发环境Xocode版本高于你本地Xocode 编译版本时,在本地运行会出现如下错误: 解决:  重写调整Deloyment Target 的版本 注:还有一种情况会出现如上错误,并 ...

  6. Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)

    Windows上两个tomcat, 虚拟机中ip为192.168.0.30的centos上一个(测试用三台就够了, 为了测试看见端口所以没有使用nginx转发请求) 开始 1.windows上开启两个 ...

  7. MySQL、SqlServer、Oracle三大主流数据库分页查询

    在这里主要讲解一下MySQL.SQLServer2000(及SQLServer2005)和ORCALE三种数据库实现分页查询的方法.可能会有人说这些网上都有,但我的主要目的是把这些知识通过我实际的应用 ...

  8. 【DP】BZOJ 1260: [CQOI2007]涂色paint

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MBSubmit: 893  Solved: 540[Submit][Stat ...

  9. [转载]淘宝API调用 申请 获取session key

    http://www.cnblogs.com/zknu/archive/2013/06/14/3135527.html 在调用淘宝的API时,我们都会用到appkey,appsecret,appses ...

  10. unity3d结合轮廓显示,实现完整的框选目标(附Demo代码)

    原地址:http://dong2008hong.blog.163.com/blog/static/469688272013111554511948/ 在unity里实现,其实很简单,因为有两个前提:1 ...