Andriod中的数据绑定 :SimpleCursorAdapter篇

本文详细介绍了SimpleCursorAdapter的构造方法及使用方式,包括如何通过参数设置数据与UI间的绑定关系,以及如何使用ViewBinder自定义数据绑定过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public                                                   SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)

  Since: API Level 11

Standard constructor.

Parameters
context          The context where the ListView associated with this SimpleListItemFactory is running
layout          resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"
c          The database cursor.  Can be null if the cursor is not available yet.
from          A list of column names representing the data to bind to the UI.  Can be null             if the cursor is not available yet.
to          The views that should display column in the "from" parameter.            These should all be TextViews. The first N views in this list            are given the values of the first N columns in the from            parameter.  Can be null if the cursor is not available yet.
flags          Flags used to determine the behavior of the adapter, as per CursorAdapter(Context, Cursor, int).

 

SimpleCursorAdapter.ViewBinder

 This class can be used by external clients of SimpleCursorAdapter to bind values fom the Cursor to views. You should use this class to bind values from the Cursor to views that are not directly supported by SimpleCursorAdapter or to change the way binding occurs for views supported by SimpleCursorAdapter.

 

public                           abstract                  boolean      setViewValue (View view, Cursor cursor, int columnIndex)
  Since: API Level 1

Binds the Cursor column defined by the specified index to the specified view. When binding is handled by this ViewBinder, this method must return true. If this method returns false, SimpleCursorAdapter will attempts to handle the binding on its own.

Parameters
view          the view to bind the data to
cursor          the cursor to get the data from
columnIndex          the column at which the data can be found in the cursor
Returns
  • true if the data was bound to the view, false otherwise

 

Cursor要实现AbstractCursor

public  boolean setViewValue(View view, Cursor cursor,  int columnIndex)
    {
        MyCursor = (MyCursor) cursor;
        MyFavorite fav = cur.getBean();       
        
        
         switch (view.getId())
    {
         case R.id.A_name:

            TextView nView = (TextView) view;            
                String name = fav.getName()
                nView.setText(name);
        case R.id.B_name:
       ImageView imageView = (ImageView) view;
       imageView.setVisibility(View.VISIBLE);
       imageView.setImageResource(R.drawable.icon1)
    ....   
        
    
            

       
        
     }        
        Return  true
    
    }

 

Public Methods

        public                                             void      bindView(View view, Context context, Cursor cursor)
  Since: API Level 1

Binds all of the field names passed into the "to" parameter of the constructor with their corresponding cursor columns as specified in the "from" parameter. Binding occurs in two phases. First, if a SimpleCursorAdapter.ViewBinder is available, setViewValue(android.view.View, android.database.Cursor, int) is invoked. If the returned value is true, binding has occured. If the returned value is false and the view to bind is a TextView, setViewText(TextView, String) is invoked. If the returned value is false and the view to bind is an ImageView, setViewImage(ImageView, String) is invoked. If no appropriate binding can be found, an IllegalStateException is thrown.

Parameters
view          Existing view, returned earlier by newView
context          Interface to application's global information
cursor          The cursor from which to get the data. The cursor is already moved to the correct position.
Throws
IllegalStateExceptionif binding cannot occur
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值