尚学堂【官网】-西安Java培训|c++培训|Android培训|安卓培训|java视频教程|软件工程师|-西安雁塔尚学堂计算机学校
Android安卓教程  尚学堂首页Java学院Android安卓教程

使用ListView显示数据

www.xasxt.com 发布人:java  |  来自:本站  |  发布时间:2017-08-23 10:53:00  |  点击次数:2607

  使用ListView显示数据

  在Android中,ListView是用来显示一个列表的控件。每一行列表都是一个独立的元素。这种控件既可以方便的显示从系统中其他应用读取出来的数据,也可独立的为各行元素设置监听器。

  根据API文档中的说明,使用ListView显示控件的基本流程如下:

  1. 将所要显示的数据以ArrayList,Cursor或者Array形式封装成为对象。

  eg1:

  public class Activity01 extends ListActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ArrayList> list = new ArrayList>(); HashMap map1 = new HashMap(); HashMap map2 = new HashMap(); HashMap map3 = new HashMap(); map1.put("user_name", "zhangsan"); map1.put("user_ip", "192.168.0.1"); map2.put("user_name", "zhangsan"); map2.put("user_ip", "192.168.0.2"); map3.put("user_name", "wangwu"); map3.put("user_ip", "192.168.0.3"); list.add(map1); list.add(map2); list.add(map3); SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[] { "user_name", "user_ip" }, new int[] { R.id.user_name,R.id.user_ip}); setListAdapter(listAdapter); }

  ArrayList一般用显示静态数据,其中又包含了容器。每一个容器中又可包含多个数据,作为ListView的一行数据。

  eg2.

  ContentResolver cr=getContentResolver(); Cursor cur=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);

  Cursor一般用于读取其他应用程序的制定的数据。读取完毕后,Cursor指向所得结果集的第一项数据。

  eg3.

  private static final String[] bs = {"a","b","c","d"};

  数组用于显示简单的信息,如在spinner中显示。

  2. 将所得到的数据加入到各自类型对应的适配器中。

  2.1 ArrayList型

  使用SimpleAdapter作为适配器

  public ArrayAdapter (Context context, int resource, int textViewResourceId)

  Since: API Level 1Constructor

  Parameters

  contextThe current context.

  resourceThe resource ID for a layout file containing a layout to use when instantiating views.

  textViewResourceIdThe id of the TextView within the layout resource to be populated

  eg.

  SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[] { "user_name", "user_ip" }, new int[] { R.id.user_name,R.id.user_ip}); setListAdapter(listAdapter);

  2.2 ContentProvider型

  使用SimpleCursorAdapter作为适配器

  Since: API Level 1public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)

  Since: API Level 1Constructor.

  Parameters

  contextThe context where the ListView associated with this SimpleListItemFactory is running

  layoutresource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"

  cThe database cursor. Can be null if the cursor is not available yet.

  fromA list of column names representing the data to bind to the UI. Can be null if the cursor is not available yet.

  toThe views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.

  eg.

  ListAdapter adapter = new SimpleCursorAdapter(this, // 定义List中每一行的显示模板 // 表示每一行包含两个数据项 ,使用了系统内置模板 android.R.layout.simple_list_item_2, // 数据库的Cursor对象 cur, // 从数据库的NAME和NUMBER两列中取数据 new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER }, // 与NAME和NUMBER对应的Views,系统内置的ID new int[] { android.R.id.text1, android.R.id.text2 });

  2.3 数组型

  使用ArrayAdapter

  public ArrayAdapter (Context context, int resource, int textViewResourceId)

  Since: API Level 1Constructor

  Parameters

  contextThe current context.

  resourceThe resource ID for a layout file containing a layout to use when instantiating views.

  textViewResourceIdThe id of the TextView within the layout resource to be populated

  3.将适配器添加到ListVeiw中,使用setAdapter(adap)方法。

当前文:使用ListView显示数据
上一页:Android 软键盘状态并隐藏输入法的实例
下一页:UI开发中所用到特殊方法
在线报名(*为必填项)