免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1262 | 回复: 0
打印 上一主题 下一主题

[Android] Adapter & AdapterView [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-22 08:51 |只看该作者 |倒序浏览
  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 ListView 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<? extends Map<String, ?>> 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<CharSequence> 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:
      <GridView xmlns:android="http://schemas.android.com/apk/res/android"
          ....
          android:padding="10dp"
          android:verticalSpacing="10dp"
          android:horizontalSpacing="10dp"
          android:numColumns="auto_fit"
          android:columnWidth="60dp"
          android:stretchMode="columnWidth"
          android:gravity="center"
          />

  6. xxx
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP