码迷,mamicode.com
首页 > 其他好文 > 详细

About ListView

时间:2016-03-13 17:43:36      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

使用Adapter建立(bind)Layout

当layout内容是动态的或者不是预先决定好的,可以使用AdapterView的子类动态完成layout的填充。

AdapterView的子类使用Adapter来bind data到它的layout里面。Adapter的角色是数据源和AdapterView layout的中间人——Adapter从数组、数据库等处取出数据然后把每个条目转换成可以加进AdapterView layout的一个view。

用数据填充Adapter

你可以通过给Adapter建立AdapterView的形式来填充ListView、GridView等AdatperView。这个操作会把外部数据建立成一个view,每一个view代表一个数据条目。

Android提供很多Adapter的子类来给AdapterView获取数据、建立view。两种最常见的adapter是:

ArrayAdapter

当你的数据源是array(数组)的时候使用这个adapter。默认地,ArrayAdapter通过对每个list item调用toString()并且把内容放进TextView中来为每个array item建立一个view。

例如,当你有一个字符串数组想要展示在ListView里面,用构造函数来初始化一个新的ArrayAdapter并且为每个string和string array指定一个latyout。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myStringArray);

构造函数的参数是:

  • App Context
  • 一个包含了为数组中的每个string准备了TextView的layout
  • 字符串数组

然后只要在你的ListView中调用setAdapter():

ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);

为了自定义每个item的外观,你可以为你的数组对象复写(override)的toString()方法。或者建立一个不是TextView的view比如ImageView,继承ArrayAdapter类并且复写getView()来反悔你想为每个item准备的view类型。

SimpleCursorAdapter 

当你的数据来自Cursor的时候使用这个adapter。使用SimpleCursorAdapter,你必须为Cursor中的每一行指定一个layout。例如,如果你想建立一个包含了人名和电话号码的表,你可以使用一个返回Cursor的查询(query),Corsor中一行对应一个人,列则对应人名和电话号码。然后你建立一个字符串数组来指定Cursor中的哪些列你想要放置到result的layout里面,建立一个integer数组来指定列应该被放置到的对应的view:

String[] fromColumns = {ContactsContract.Data.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};
int[] toViews = {R.id.display_name, R.id.phone_number};

When you instantiate the SimpleCursorAdapter, pass the layout to use for each result, the Cursorcontaining the results, and these two arrays:

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
        R.layout.person_name_and_number, cursor, fromColumns, toViews, 0);
ListView listView = getListView();
listView.setAdapter(adapter);

The SimpleCursorAdapter then creates a view for each row in the Cursor using the provided layout by inserting each fromColumns item into the corresponding toViews view.

.

If, during the course of your application‘s life, you change the underlying data that is read by your adapter, you should call notifyDataSetChanged(). This will notify the attached view that the data has been changed and it should refresh itself.

处理点击事件

你可以通过实现AdapterView.OnItemClickListener接口来处理点击AdapterView中每个条目的点击事件。比如:

// Create a message handling object as an anonymous class. private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { // Do something in response to the click } }; listView.setOnItemClickListener(mMessageClickedHandler);

 

 

 参考:https://developer.android.com/guide/topics/ui/declaring-layout.html#AdapterViews

About ListView

标签:

原文地址:http://www.cnblogs.com/larrylawrence/p/5272181.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!