Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2091522
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: 嵌入式

2011-07-11 16:54:24

  1. Docs
    http://developer.android.com/guide/topics/ui/binding.html
  2. Summary
    • Adapter
      Adapter binds to a collection data of some type, it determines the child view of AdapterView.
    • AdapterView
      The AdapterView object is an implementation of ViewGroup whose child Views are determined by an Adapter that binds to data of some type.
  3. Adapter
    The Adapter acts like a courier between your data source (perhaps an array of external strings) and the AdapterView, which displays it.

    • ListAdapter
      The bridge between a and the data that backs the list.

    • SpinnerAdapter
      The bridge between a Spinner and its data.

    • BaseAdapter
      Common base class of common implementation for an Adapter that can be used in both ListView and Spinner. If all view in the AdapterView are the same, you can use SimpleAdapter directly; otherwise, you need to extend BaseAdapter.

      You can use this to customize Adapter, ie:

      private String m_rows[] = {"Jimmy", "Jacky", "Lucy", "HanMeimei", "LiLei"}

      public class MyAdapter extends BaseAdapter

          private class ItemViewHolder
          {
              ImageView m_icon;
              TextView m_name;
              TextView m_time;
          }
       
       
          public int getCount()
          {                         
              return m_rows.length;  //Return the count of rows
          } 
       
          public Object getItem(int position)
          {      
              return m_rows[position]; 
          } 
       
          public long getItemId(int position)
          {   
              return position; 
          } 
       
          public View getView(int position, View convertView, ViewGroup parent)
          {  
              ItemViewHolder holder;

              if (convertView == null)
              {
                  View v = inflater.inflate(R.layout.xxx, null);
                  holder = new ItemViewHolder();
                  holder.m_icon = (ImageView)v.findViewById(R.id.icon);
                  holder.m_name = (TextView)v.findViewById(R.id.name);
                  holder.m_time = (TextView)v.findViewById(R.id.time);
       
                  v.setTag(holder);

                  convertView = v;
              }
              else
              {
                  holder = convertView.getTag();
              }   

              holder.m_icon.setImageBitmap(xxx);
              holder.m_name.setText(m_rows[position]);
              holder.m_time.setText(xxx);
         
              return v;
          }   


      ListView listview = (ListView) findViewById(xxx);
      MyAdapter adapter = new MyAdapter();
      listview.setAdapter(adapter);

    • ArrayAdapter
      For reading from an arbitrary array (Array or List). A row can only contain one data.

      Code Snip a):
          String[] sw = new String[100];
         
      for (int i = 0; i < 100; i++
      {
              sw[i] 
      = "listtest_" +
       i;
          }


          ArrayAdapter
      <String> adapter = new ArrayAdapter<String(this,
                       android.R.layout.simple_list_item_1,
      //Use system defined layout file
                       sw);

          setListAdapter(adapter);


      Code Snip b):
          String[] sw = new String[100];
         
      for (int i = 0; i < 100; i++
      {
              sw[i] 
      = "listtest_" +
       i;
          }


          ArrayAdapter
      <String> adapter = new ArrayAdapter<String>(this,
                       R.layout.my_simple_list_item, 
      // Use customized layout file
                  R.id.id_textview, //The view (defined in R.layout.my_simple_list_item) to display data
                       sw);
          setListAdapter(adapter);


      • Set child view's style of AdapterView: adapter.setDropDownViewResource
      • xxx

    • SimpleAdapter
      An easy adapter to map static data to views defined in an XML file. A row can contains a set of data.

      We can back all data into List (such as an ArrayList), the elements of the List is Map (such as HashMap), for example, List> mData. Each entry in the List corresponds to one row in the list (Each row may contain a set of data and each one need to show in the specific sub-view, so we back data of a row into Map that can be get via keys later on). We also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views (via view id).

      Code Snip:
      (From: http://blog.csdn.net/shichexixi/article/details/5585823)
        
          setContentView(R.layout.main);
        ArrayList
      <HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>
      ();
         
      for (int i = 0; i < 10; i++
      {
              HashMap
      <String, Object> user = new HashMap<String, Object>
      ();
              user.put(
      "img"
      , R.drawable.user);
              user.put(
      "username""Name(" + i+")"
      );
              user.put(
      "age", (20 + i) + ""
      );
              users.add(user);
          }

         
          SimpleAdapter saImageItems 
      = new SimpleAdapter(this,
                      users,
               // Data list

                      R.layout.user, // Define layout of row in AdapterView
                        new String[] "img""username""age" }// Keys to get data for the correcponding views in current row, the views are specified in the next argument via view id.
                        new int[] { R.id.img, R.id.name, R.id.age } // These id are defined in R.layout.user, the specific data got from Map with keys specified in the previous argument will display in the views .
                        );

          // Set Adapter

        ((ListView) findViewById(R.id.users)).setAdapter(saImageItems);

    • CursorAdapter & ResourceCursorAdapter & SimpleCursorAdapter
      For reading database data from a Cursor.

      • ResourceCursorAdapter
        Like CursorAdapter, but you can customize layout for row.
      • SimpleCursorAdapter
        Like SimpleAdapter, but the difference are:
        1. All data is stored in Cursor rather than List
        2. The data for current row are get via columns name rather than keys
      • Set child view's style of AdapterView: adapter.setDropDownViewResource
      • xxx

    • HeaderViewListAdapter
      ListAdapter used when a ListView has header and/or footer views.
    • Refresh AdapterView
      When you add/remove line(s) to/from Adapter, you can call Adapter.notifyDataSetChanged() to notify AdapterView to refresh, but if you only change the contents of existing row, that API doesn't take effective.
    • xxx

  4. ExpandableListAdapter
    An adapter that links a ExpandableListView with the underlying data. The implementation of this interface will provide access to the data of the children (categorized by groups), and also instantiate Views for children and groups.
    • BaseExpandableListAdapter
    • CursorTreeAdapter
    • ResourceCursorTreeAdapter
    • SimpleCursorTreeAdapter
    • SimpleExpandableListAdapter

  5. AdapterView
    AdapterView objects have two main responsibilities:
    • Filling the layout with data
      Inserting data into the layout is typically done by binding the AdapterView class to an Adapter, which retrieves data from an external source (perhaps a list that the code supplies or query results from the device's database).
    • Handling user selections

    Classical AdapterView:
    • ListView / ExpandableListView
      A view that shows items in a vertically scrolling list.

      • Methods
        1. getItemAtPosition
          Is a reference to Adapter.getItem(xxx).
        2. xxx
      • xxx

    • Spinner
      A view that displays one child at a time and lets the user pick among them.

      Spinner s1 = (Spinner) findViewById(R.id.spinner1);
              ArrayAdapter adapter = ArrayAdapter.createFromResource(
                      this, R.array.colors, android.R.layout.simple_spinner_item);
      //Must set dropdown view resource for Spinner       adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
              s1.setAdapter(adapter);

    • Gallery
      A view that shows items in a center-locked, horizontally scrolling list.

    • GridView
      A view that shows items in two-dimensional scrolling grid.

      Sample:
          ....
          android:padding="10dp"
          android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp"
          android:numColumns="auto_fit"
          android:columnWidth="60dp"
          android:stretchMode="columnWidth"
          android:gravity="center"
          />

  6. xxx
阅读(826) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~