Android常见UI组件之ListView(二)——定制ListView
这一篇接上篇。展示ListView中选择多个项及实现筛选功能~
1、在位于res/values目录下的strings.xml文件里加入例如以下代码:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">BasicView5</string> <string name="action_settings">Settings</string> <string name="hello_world">Hello world!</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>
2、改动上一篇中的BasicView5.java文件的代码。改动后的代码例如以下:
package com.example.basicview5; import android.os.Bundle; import android.app.Activity; import android.app.ListActivity; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends ListActivity { String[] presidents;//将列表信息存储在strings.xml文件里,再以编程方式读取 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ---no need to call this---// // setContentView(R.layout.activity_main); ListView listView = getListView();// 获取ListActivity的列表视图 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// 能够选择多个项 listView.setTextFilterEnabled(true);//启用筛选功能。在键盘上输入,ListView自己主动筛选 // getResources()方法以编程方式检索与应用程序捆绑的资源 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(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
3、执行程序,效果例如以下:
4、在activity_main.xml文件里加入代码例如以下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="Show selected items" /> <ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
5、在BasicView5.java文件里加入代码,例如以下:package com.example.basicview5; import android.os.Bundle; import android.app.Activity; import android.app.ListActivity; import android.view.Menu; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends ListActivity { String[] presidents;//将列表信息存储在strings.xml文件里。再以编程方式读取 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ---need to call this---// setContentView(R.layout.activity_main);//因为在xml文件里加入了一个ListView部分填充一个活动,所以须要载入活动内容。 ListView listView = getListView();// 获取ListActivity的列表视图 listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);// 能够选择多个项 listView.setTextFilterEnabled(true);//启用筛选功能。在键盘上输入,ListView自己主动筛选 // getResources()方法以编程方式检索与应用程序捆绑的资源 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(); } public void onClick(View view) { ListView listView = getListView(); String itemsSelected = "Selected items: "; for(int i = 0; i < listView.getCount(); i++) { if(listView.isItemChecked(i)) {//找出被选中的item itemsSelected += listView.getItemAtPosition(i) + " ";//返回指定位置的項的名称 } } Toast.makeText(this, itemsSelected, Toast.LENGTH_LONG).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
6、执行效果例如以下:
筛选并选中几个项。再按button显示选择了那些项: