Android之怎样使用ListView列表视图

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







posted @ 2017-07-03 17:16  yxysuanfa  阅读(235)  评论(0编辑  收藏  举报