乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      Android ListView和ListAdapter

       瞻云軒 2015-05-08
      一個(gè)ListView顯示出來(lái)需要3個(gè)東西:
      1,listview(用來(lái)顯示數(shù)據(jù)的列表)。
      2,Data(需要顯示的數(shù)據(jù))。
      3,一個(gè)綁定Data和Listview的適配器ListAdapter。
      一,ListView
      1,ListView的每一項(xiàng)其實(shí)都是TextView。
      2,通過(guò)setAdapter方法來(lái)調(diào)用一個(gè)listAdapter來(lái)綁定數(shù)據(jù)。
      二,ListAdapter
      1,ListAdapter是綁定Data和Listview的適配器。但是,它是接口,需要使用它的子類。
      常見(jiàn)的子類有:arrayAdapter,SimpleAdapter ,CursorAdapter
      2,android系統(tǒng)默認(rèn)提供了很多默認(rèn)的布局方式,也可以自定義。

      Android.R.layout.simple_list_item_1:每一項(xiàng)只有一個(gè)TextView
      Android.R.layout.simple_list_item_2:每一項(xiàng)有兩個(gè)TextView
      Android.R.layout.simpte.list_item_single_choice,每一項(xiàng)有一個(gè)TextView,但是這一項(xiàng)可以被選中。
      三,ArrayAdapter
      1,數(shù)組適配器,它的作用就是一個(gè)數(shù)組和listview之間的橋梁,它可以將數(shù)組里邊定義的數(shù)據(jù)一一對(duì)應(yīng)的顯示在Listview里邊。
      2,ListView的每個(gè)TextView里邊顯示的內(nèi)容就是數(shù)組里邊的對(duì)象調(diào)用toString()方法后生成的字符串。
      3,這是一個(gè)簡(jiǎn)單創(chuàng)建一個(gè)list的例子其中的代碼:
      ListView listview=new ListView(this);
      // 構(gòu)造一個(gè)listview對(duì)象
      String[] data = {“google”,”amazon”,”facebook”};
      // 構(gòu)造一個(gè)數(shù)組對(duì)象,也就是數(shù)據(jù)
      listview.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice, data));
      //構(gòu)造一個(gè)array適配器,然后listview對(duì)象通過(guò)setAdapter方法調(diào)用適配器來(lái)和自己綁定數(shù)據(jù)
      list.setItemsCanFocus(true);
      list.setChoiceMode(listview.CHOICE_MODE_MULTIPLE);
      setContentView(listview);


      四,SimpleAdapter
      1,作用是ArrayList和ListView的橋梁。這個(gè)ArrayList里邊的每一項(xiàng)都是一個(gè)Map<String,?>類型。ArrayList當(dāng)中的每一項(xiàng)Map對(duì)象都和ListView里邊的每一項(xiàng)進(jìn)行數(shù)據(jù)綁定一一對(duì)應(yīng)。
      2,SimpleAdapter的構(gòu)造函數(shù):
      SimpleAdapter(Context  context, List<? extends Map<String, ?>>  data, int resource, String[]  from, int[] to)
      參數(shù):
      1,context:上下文。
      2,data:基于Map的list。Data里邊的每一項(xiàng)都和ListView里邊的每一項(xiàng)對(duì)應(yīng)。Data里邊的每一項(xiàng)都是一個(gè)Map類型,這個(gè)Map類里邊包含了ListView每一行需要的數(shù)據(jù)。
      3,resource :就是一個(gè)布局layout,可引用系統(tǒng)提供的,也可以自定義。
      4,from:這是個(gè)名字?jǐn)?shù)組,每個(gè)名字是為了在ArrayList數(shù)組的每一個(gè)item索引Map<String,Object>的Object用的。
      5,to:里面是一個(gè)TextView數(shù)組。這些TextView是以id的形式來(lái)表示的。例如:Android.R.id.text1,這個(gè)text1在layout當(dāng)中是可以索引的。
      3,通過(guò)一個(gè)類子來(lái)理解下:
      listitem.xml文件:
      <?xml version=”1.0″ encoding=”utf-8″?>
      <LinearLayout xmlns:android=”http://schemas./apk/res/android
      android:orientation=”horizontal” android:layout_width=”fill_parent”
      android:layout_height=”wrap_content”>
      <TextView android:id=”@+id/mview1″ android:layout_width=”100px”
      android:layout_height=”wrap_content” />
      <TextView android:id=”@+id/mview2″
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content” />
      </LinearLayout>
      下面是activity文件的部分代碼
      List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
      Map<String,Object> item;
      item = new HashMap<String,Object>();
      item.put(“姓名”,”張三”);
      item.put(“性別”,”男”);
      data.add(item);
      item = new HashMap<String,Object>();
      item.put(“姓名”,”李四”);
      item.put(“性別”,”女”);
      data.add(item);
      // 上面是構(gòu)造數(shù)據(jù)部分。
      ListView listview= new ListView(this);
      //構(gòu)造listview對(duì)象。
      SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.listitem,new String[]{“姓名”,”性別”},new int[]{R.id.TextView01,R.id.TextView02});
      /*構(gòu)造一個(gè)適配器。
      *    1,第三個(gè)參數(shù)是說(shuō)明用的是自定義的布局R.layout.listtiem。
      * 2,第四和第五個(gè)參數(shù)一起理解:
      *          把我們添加數(shù)據(jù)時(shí)姓名那一列對(duì)應(yīng)到R.id.TextView01這個(gè)TextView中,把性別對(duì)應(yīng)到R.id.TextView02這個(gè)TextView中。
      *          如果把from改為new String[]{“姓名”,”姓名”},測(cè)試下就會(huì)明白。
      */
      listview.setAdapter(adapter);
      setContentView(listview);

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多