Android之如何使用ListView列表视图

来源:互联网 发布:如何申请域名做网站 编辑:程序博客网 时间:2024/04/26 00:45
ListView


列表视图创建方法:
(1)直接使用ListView 组件创建
(2)让Activity继承ListActivity实现


第一种:在XML中直接使用ListView 组件创建
在values/string.xml中
<resources>


   <string name="app_name">AndroidUI</string>
   <string name="action_settings">Settings</string>
   <string name="hello_world">Hello world!</string>


   <string-array name="ctype">
<item>主题模式</item>
<item>主题模式</item>
<item>主题模式</item>
<item>主题模式</item>
<item>主题模式</item>
<item>主题模式</item>
<item>主题模式</item>
   </string-array>


   <string name="title_activity_list_view">ListViewActivity</string>
   <string name="title_activity_list">ListActivity</string>


</resources>


在layout.xml中


<ListView
android:id="@+id/listview1"
android:entries="@array/ctype"//获取资源文件数组
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@color/mycolor1"
android:dividerHeight="3sp"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false" >
   </ListView>






第二种:使用通过适配器指定列表项的方式创建ListView

在Activity中写入:
//根据id获取对象
ListView listView = (ListView) findViewById(R.id.listview1);
//创建适配器对
  
 //参数代表的意思(上下文对象,每行样式类型(必须是TextView类型 android.R.....是系统自带的样式,也可以使用自己定义的),数据本身(可以来自资源数据,也可以来自数组在java中添加))
//以下方式(数据来自资源文件)
ListAdapter adapter = ArrayAdapter.createFromResource(
this, R.array.ctype, android.R.layout.simple_list_item_1);

//给对象加上适配器

listView.setAdapter(adapter);


如何给ListView 添加监听方法?

见:

http://blog.csdn.net/wei_chong_chong/article/details/47606835


如何自定义ListView呢?

见:

http://blog.csdn.net/wei_chong_chong/article/details/47603881


如何给ListView添加文字过滤器:

见:

http://blog.csdn.net/wei_chong_chong/article/details/47603763







0 1
原创粉丝点击