尚學堂【官網】-西安Java培訓|c++培訓|Android培訓|安卓培訓|java視頻教程|軟件工程師|-西安雁塔尚學堂計算機學校
Android安卓教程  尚學堂首頁Java學院Android安卓教程

使用ListView顯示數據

www.zyopwk.live 發布人:java  |  來自:本站  |  發布時間:2017-08-23 10:53:00  |  點擊次數:2750

  使用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開發中所用到特殊方法
在線報名(*為必填項)
云南快乐十分开奖走势图