【Android 开发教程】ListView的总结范例
来源:互联网 发布:英国数学 知乎 编辑:程序博客网 时间:2024/04/29 13:21
本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/使用一个例子,来总结一下ListView的基本使用。
1. 新建一个工程:ListViewDemo。
2. main.xml中的代码。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Show selected items" android:onClick="onClick"/><ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>3. Strings.xml中的代码。
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, BasicViews5Activity!</string> <string name="app_name">BasicViews5</string> <string-array name="presidents_array"> <item>Dwight D. Eisenhower</item> <item>John F. Kennedy</item> <item>Lyndon B. Johnson</item> <item>Richard Nixon</item> <item>Gerald Ford</item> <item>Jimmy Carter</item> <item>Ronald Reagan</item> <item>George H. W. Bush</item> <item>Bill Clinton</item> <item>George W. Bush</item> <item>Barack Obama</item> </string-array> </resources>4. ListViewDemo中的代码。
public class ListViewDemo extends ListActivity { String[] presidents; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ListView lstView = getListView(); //lstView.setChoiceMode(ListView.CHOICE_MODE_NONE); //lstView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); lstView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); lstView.setTextFilterEnabled(true); presidents = getResources().getStringArray(R.array.presidents_array); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, presidents)); } public void onListItemClick( ListView parent, View v, int position, long id) { Toast.makeText(this, "You have selected " + presidents[position], Toast.LENGTH_SHORT).show(); } // for button click event public void onClick(View view) { ListView lstView = getListView(); String itemsSelected = "Selected items: \n"; for (int i=0; i<lstView.getCount(); i++) { if (lstView.isItemChecked(i)) { itemsSelected += lstView.getItemAtPosition(i) + "\n"; } } Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show(); }}5. 按F11在模拟器上面调试。在ListView中选择一些物件,然后点击上面的Button,就会显示出哪些物件被选择了。
- 【Android 开发教程】ListView的总结范例
- 【Android 开发教程】ContentProvider的使用范例
- 【Android开发】范例2-实现带图标的ListView
- 【Android 开发教程】ListView的基本使用
- 【Android 开发教程】ListView的扩展功能
- Android开发之listview总结
- android开发:自定义ListView总结
- Android 开发ListView学习总结
- android listview的总结
- Blackberry平台开发,教程,范例
- ListView -- MarsChen Android 开发教程学习笔记
- android开发教程之listview使用方法
- android开发教程之listview使用方法
- Android程序设计开发范例
- Android开发:Android的ListView下拉刷新,基于慕课教程的改进版
- Android开发ListView控件使用总结。
- android开发中几种关于ListView用法总结
- 【Android开发】范例1-绘制Android的机器人
- jQuery check if horizontal scroll is present
- matlab函数
- 【Tech-Android-Other】Android中获取SD卡下的图片资源
- gtk带glade的实例
- 尖叫生活网(jjyy365.com)
- 【Android 开发教程】ListView的总结范例
- seo复习,建立.htaccess文件的三种方法
- ubuntu最新系统12.04的64位下载并安装
- 《用Glade2开发图形用户界面》控件简解
- 【Tech-Android-View】Android中利用GridView显示SD卡的图片
- linux网文
- BurstNET 2012年4月最新8折优惠码 多机房可选择
- On Knowledge Interface: 知行合一
- web.xml