<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      自定義SimpleAdapter

      SimpleAdapter,跟名字一樣,一個(gè)簡單的適配器,既為簡單,就只是被設(shè)計(jì)來做簡單的應(yīng)用的,比如靜態(tài)數(shù)據(jù)的綁定,不過仍然有自定義的空間,比如說在每一個(gè)ListItem中加一個(gè)按鈕并添加響應(yīng)事件.首先還是先看一下SimpleAdapter的定義吧,直接翻譯下SDK doc 吧:

        這是一個(gè)簡單的適配器,可以將靜態(tài)數(shù)據(jù)映射到XML文件中定義好的視圖。你可以指定由Map組成的List(比如ArrayList)類型的數(shù)據(jù)。在ArrayList中的每個(gè)條目對應(yīng)List中的一行。Maps包含每一行的數(shù)據(jù)。你可以指定一個(gè)XML布局以指定每一行的視圖,根據(jù)Map中的數(shù)據(jù)映射關(guān)鍵字到指定的視圖。綁定數(shù)據(jù)到視圖分兩個(gè)階段,首先,如果設(shè)置了SimpleAdapter.ViewBinder,那么這個(gè)設(shè)置的ViewBinder的setViewValue(android.view.View, Object, String)將被調(diào)用。如果setViewValue的返回值是true,則表示綁定已經(jīng)完成,將不再調(diào)用系統(tǒng)默認(rèn)的綁定實(shí)現(xiàn)。如果返回值為false,視圖將按以下順序綁定數(shù)據(jù):
      • 如果View實(shí)現(xiàn)了Checkable(例如CheckBox),期望綁定值是一個(gè)布爾類型。
      • TextView.期望綁定值是一個(gè)字符串類型,通過調(diào)用setViewText(TextView, String)綁定。
      • ImageView,期望綁定值是一個(gè)資源id或者一個(gè)字符串,通過調(diào)用setViewImage(ImageView, int) 或 setViewImage(ImageView, String)綁定數(shù)據(jù)。
        如果沒有一個(gè)合適的綁定發(fā)生將會拋出IllegalStateException。

      先看一下構(gòu)造函數(shù): 

      public SimpleAdapter (Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

      參數(shù):

      • context  SimpleAdapter關(guān)聯(lián)的View的運(yùn)行環(huán)境
      • data    一個(gè)Map組成的List。在列表中的每個(gè)條目對應(yīng)列表中的一行,每一個(gè)map中應(yīng)該包含所有在from參數(shù)中指定的鍵
      • resource    一個(gè)定義列表項(xiàng)的布局文件的資源ID。布局文件將至少應(yīng)包含那些在to中定義了的ID
      • from          一個(gè)將被添加到Map映射上的鍵名
      • to     將綁定數(shù)據(jù)的視圖的ID,跟from參數(shù)對應(yīng),這些應(yīng)該全是TextView

      舉個(gè)例子:

      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      ListView lv = (ListView) findViewById(R.id.listView1);
      String[] from = { "Text", "Button" };
      int[] to = { R.id.text, R.id.button };
      List<Map<String, ?>> list = new ArrayList<Map<String, ?>>();
      for (int i = 0; i < 10; i++) {
      Map<String, String> m = new HashMap<String, String>();
      m.put("Text", "Text" + i);
      m.put("Button", "Button" + i);
      list.add(m);
      }
      SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.listitem, from, to);
      lv.setAdapter(adapter);
      }

      listitem.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
      ="horizontal" >

      <TextView
      android:layout_width="wrap_content"
      android:id
      ="@+id/text"
      android:layout_height
      ="wrap_content"
      android:layout_weight
      ="1" />

      <Button
      android:id="@+id/button"
      android:layout_width
      ="wrap_content"
      android:layout_height
      ="wrap_content" />

      </LinearLayout>

      ListView中的每一項(xiàng)都包含一個(gè)TextView跟一個(gè)Button,在SimpleAdapter的構(gòu)造函數(shù)中,我們指定了要綁定的數(shù)據(jù):list, list是一個(gè)由Map組成的ArrayList, Map的作用就是連同后面的from, to參數(shù)定義數(shù)據(jù)是如何綁定的,在上面的例子中

      String[] from = { "Text", "Button" };
      int[] to = { R.id.text, R.id.button };

      而在for循環(huán)中每個(gè)map都put進(jìn)了兩個(gè)鍵值對,鍵名跟from中定義的一一對應(yīng),這就表示對于ListView中的每一項(xiàng),依次尋找在to參數(shù)中定義的資源ID,根據(jù)這個(gè)資源ID在to參數(shù)數(shù)組中的位置,找到from參數(shù)中對應(yīng)位置的值,以這個(gè)值為鍵,在list中的相應(yīng)項(xiàng)(一個(gè)Map)中以這個(gè)值為鍵取出這個(gè)鍵對應(yīng)的值綁定到這個(gè)資源ID對應(yīng)的視圖中.

      在上面的例子中每一個(gè)ListItem都包含一個(gè)TextView與一個(gè)Button,但程序運(yùn)行起來后會發(fā)現(xiàn),按鈕可以點(diǎn)擊,而ListItem卻無法點(diǎn)擊,而且沒有對每一個(gè)Button關(guān)聯(lián)響應(yīng)事件,ListItem無法點(diǎn)擊是因?yàn)榘粹o搶占了ListItem的焦點(diǎn),在listitem.xml而已文件中對LinearLayout加上一個(gè)屬性就可解決問題:

          android:descendantFocusability="blocksDescendants"

      下面的問題就是Button的響應(yīng)事件了.

      我的們下SimpleAdaper的源碼會發(fā)現(xiàn),數(shù)據(jù)的綁定是能過一個(gè)叫bindView的函數(shù)實(shí)現(xiàn)的

         private void bindView(int position, View view) {
      final Map dataSet = mData.get(position);
      if (dataSet == null) {
      return;
      }

      final ViewBinder binder = mViewBinder;
      final String[] from = mFrom;
      final int[] to = mTo;
      final int count = to.length;

      for (int i = 0; i < count; i++) {
      final View v = view.findViewById(to[i]);
      if (v != null) {
      final Object data = dataSet.get(from[i]);
      String text = data == null ? "" : data.toString();
      if (text == null) {
      text = "";
      }

      boolean bound = false;
      if (binder != null) {
      bound = binder.setViewValue(v, data, text);
      }

      if (!bound) {
      if (v instanceof Checkable) {
      if (data instanceof Boolean) {
      ((Checkable) v).setChecked((Boolean) data);
      } else if (v instanceof TextView) {
      // Note: keep the instanceof TextView check at the bottom of these
      // ifs since a lot of views are TextViews (e.g. CheckBoxes).
      setViewText((TextView) v, text);
      } else {
      throw new IllegalStateException(v.getClass().getName() +
      " should be bound to a Boolean, not a " +
      (data == null ? "<unknown type>" : data.getClass()));
      }
      } else if (v instanceof TextView) {
      // Note: keep the instanceof TextView check at the bottom of these
      // ifs since a lot of views are TextViews (e.g. CheckBoxes).
      setViewText((TextView) v, text);
      } else if (v instanceof ImageView) {
      if (data instanceof Integer) {
      setViewImage((ImageView) v, (Integer) data);
      } else {
      setViewImage((ImageView) v, text);
      }
      } else {
      throw new IllegalStateException(v.getClass().getName() + " is not a " +
      " view that can be bounds by this SimpleAdapter");
      }
      }
      }
      }
      }

      其流程大致是,首先檢查SimpleAdapter有沒有指定SimpleAdapter.ViewBinder,如果指定了就調(diào)用其setViewValue方法, SimpleAdapter.ViewBinder是一個(gè)接口,也只有這一個(gè)方法,如果ViewBinder返回true表示我們已經(jīng)完成了對這個(gè)View的數(shù)據(jù)綁定,就不再調(diào)用系統(tǒng)默認(rèn)的實(shí)現(xiàn),當(dāng)然我們也可以設(shè)置一個(gè)ViewBinder添加一些功能后通過返回false再讓系統(tǒng)綁定數(shù)據(jù),比如對按鈕添加響應(yīng)事件,而按鈕上的文字由默認(rèn)實(shí)現(xiàn)綁定.通過看bindView的實(shí)現(xiàn)就可以明白開始時(shí)所說的綁定順序了:Checkable,TextView,ImageView.

      我們對ListItem的自定義是通過對SimpleAdapter設(shè)置ViewBinder來實(shí)現(xiàn)的

      SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {

      @Override
      public boolean setViewValue(View view, Object data, String textRepresentation) {
      if (view instanceof Button) {
      final View button = view;
      // button.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
      view.setOnClickListener(new OnClickListener() {
      LinearLayout listItem = (LinearLayout) button.getParent();
      TextView tv = (TextView) listItem.findViewById(R.id.text);
      @Override
      public void onClick(View v) {
      Toast.makeText(AdapterDemoActivity.this, tv.getText(), Toast.LENGTH_SHORT).show();
      }
      });
      return false;
      }
      return false;
      }
      };
      adapter.setViewBinder(binder);

      系統(tǒng)對每一個(gè)view調(diào)用binder的setViewValue(此例中是R.id.text和R.id.button,一個(gè)TextView與一個(gè)Button),我們首先檢測這個(gè)view是不是一個(gè)Button,如果是的話就關(guān)聯(lián)點(diǎn)擊事件,可能通過getParent()函數(shù)取得parentView以找到這個(gè)view的兄弟view,比如這個(gè)例子中的實(shí)現(xiàn)就是點(diǎn)擊Button后輸出這個(gè)Button所在的ListItem中的TextView上的文字.

      在setViewValue中可以完全自定義我們的實(shí)現(xiàn),比如在Button后加一個(gè)TextView,當(dāng)然可以加任何View,但這樣做沒任何意義,當(dāng)你需要這樣做時(shí)你不需要用SimpleAdater而應(yīng)該用BaseAdapter:

      SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {

      @Override
      public boolean setViewValue(View view, Object data, String textRepresentation) {
      if (view instanceof Button) {
      final View button = view;
      LinearLayout listItem = (LinearLayout) button.getParent();
      TextView textView = new TextView(AdapterDemoActivity.this);
      textView.setText("AA");
      listItem.addView(textView,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
      return false;
      }
      return false;
      }
      };
      adapter.setViewBinder(binder);

       

       



      posted @ 2012-04-05 00:34  AngelDevil  閱讀(48827)  評論(2)    收藏  舉報(bào)
      主站蜘蛛池模板: 91麻豆亚洲国产成人久久| 日韩有码国产精品一区| 午夜爽爽爽男女污污污网站| 亚洲精品成人片在线观看精品字幕| 99久久久无码国产精品动漫| 亚洲首页一区任你躁xxxxx| 日韩av爽爽爽久久久久久| 亚洲最大福利视频网| 艳妇乳肉豪妇荡乳xxx| 无码国产精品一区二区av| 久久久久久久无码高潮| 精品天堂色吊丝一区二区| 岳阳市| 99久久精品久久久久久婷婷| 欧美性猛交xxxx乱大交丰满| 尤物yw193无码点击进入| 亚洲的天堂在线中文字幕| 久久精品国产九一九九九| 免费又黄又爽又猛的毛片| 亚洲一区二区三区在线观看精品中文| 欧美xxxx黑人又粗又大| 日韩高清视频 一区二区| 91精品国产自产在线蜜臀| 男人+高清无码+一区二区| 深圳市| 亚洲粉嫩av一区二区黑人| 国产成人精品中文字幕| 777奇米四色成人影视色区| 麻豆国产va免费精品高清在线| 日韩视频中文字幕精品偷拍| 亚洲 日韩 在线精品| 老熟妇欲乱一区二区三区| 国内自拍av在线免费| 中文字幕日韩精品亚洲一区| 久久久久国产一区二区| 成人性无码专区免费视频| 久久九九精品99国产精品| 亚洲成在人线在线播放无码| 亚洲人成小说网站色在线| 国产69精品久久久久99尤物| 成人欧美日韩一区二区三区|