zoukankan      html  css  js  c++  java
  • (Android) Binding to Data with AdapterView

    http://developer.android.com/guide/topics/ui/binding.html

    User Interface:     

    Binding to Data with AdapterView

    The AdapterView is a ViewGroup subclass whose child Views are determined by an Adapter that  binds to data of some type. AdapterView is useful whenever you need to display stored data (as opposed to resource strings or drawables) in your layout.

    Gallery, ListView, and  Spinner are examples of AdapterView subclasses that you can use to bind to a specific type of data and display it in a certain way.

    AdapterView objects have two main responsibilities:

    • Filling the layout with data   
    • Handling user selections   

    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).

    The following code sample does the following:

    1. Creates a Spinner with an existing View and binds it to a new ArrayAdapter that reads an array of colors from the local resources.
    2. Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read  people's names from the device contacts (see Contacts.People).
    // Get a Spinner and bind it to an ArrayAdapter that 
    // references a String array.
    Spinner s1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(
    this, R.array.colors, android.R.layout.simple_spinner_item);
    adapter
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s1
    .setAdapter(adapter);

    // Load a Spinner and bind it to a data query.
    private static String[] PROJECTION = new String[] {
    People._ID, People.NAME
    };

    Spinner s2 = (Spinner) findViewById(R.id.spinner2);
    Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);

    SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
    android
    .R.layout.simple_spinner_item, // Use a template
    // that displays a
    // text view
    cur
    , // Give the cursor to the list adapter
    new String[] {People.NAME}, // Map the NAME column in the
    // people database to...
    new int[] {android.R.id.text1}); // The "text1" view defined in
    // the XML template

    adapter2
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s2
    .setAdapter(adapter2);

    Note that it is necessary to have the People._ID column in projection used with CursorAdapter or else you will get an exception.

    If, during the course of your application's life, you change the underlying data that is read by your Adapter, you should call notifyDataSetChanged(). This will notify the attached View that the data has been changed and it should refresh itself.

    Handling User Selections

    You handle the user's selection by setting the class's AdapterView.OnItemClickListener member to a listener and catching the selection changes.

    // Create a message handling object as an anonymous class.
    private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
    public void onItemClick(AdapterView parent, View v, int position, long id)
    {
    // Display a messagebox.
    Toast.makeText(mContext,"You've got an event",Toast.LENGTH_SHORT).show();
    }
    };

    // Now hook into our object and set its onItemClickListener member
    // to our class handler object.
    mHistoryView
    = (ListView)findViewById(R.id.history);
    mHistoryView
    .setOnItemClickListener(mMessageClickedHandler);

    For more discussion on how to create different AdapterViews, read the following tutorials:Hello Spinner,Hello ListView, andHello GridView.

    做个快乐的自己。
  • 相关阅读:
    FastAdmin 开发时对数据库进行版本管理 (非 think-migration)
    使用 mysqldump 备份时的一些参数
    软件开源后可能遇到的问题
    基于 FastAdmin 开发后台流程 (持续更新)
    FastAdmin 怎么把模块设置默认的首页?
    FastAdmin 离线安装 ueditor 出现 rule 错误
    Windows nginx php cgi-fcgi 配置 xdebug
    FastAdmin 插件刷新缓存出现 200 红色提示框解决 always_populate_raw_post_data
    参与开源项目给我带来了什么?
    为什么要求将问题发到社区?
  • 原文地址:https://www.cnblogs.com/Jessy/p/2328005.html
Copyright © 2011-2022 走看看