CursorAdapter使用






1. Adapter的关系图 



2. CursorAdapter构造函数

public CursorAdapter (Context context, Cursor c)

Api11开始被弃用

public CursorAdapter (Context context, Cursor c, boolean autoRequery)

autoequery作用: 当数据库更新,cursor是否更新
      例子说明

public CursorAdapter (Context context, Cursor c, int flags)

Flags used to determine the behavior of the adapter; may be any combination of  FLAG_AUTO_REQUERY  and FLAG_REGISTER_CONTENT_OBSERVER .



3. newView  与 bindView

public abstract void bindView (View view, Context context, Cursor cursor)

Bind an existing view to the data pointed to by cursor  //重用一个已有的view,使其显示当前cursor所指向的数据。

参数

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.


public abstract View newView (Context context, Cursor cursor, ViewGroup parent)

Makes a new view to hold the data pointed to by cursor. //新建一个视图来保存cursor指向的数据

参数

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.
parent The parent to which the new view is attached to
返回值

  • the newly created view.



4.例子:

CursorAdapter:

 public MyCursorAdapter(Context context, Cursor c, boolean autoRequery) {
        super(context, c, autoRequery);
    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from (parent.getContext())
                .inflate(R.layout.list_item, parent, false);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.textView = (TextView) view.findViewById(R.id.name);
        viewHolder.textView.setText(cursor.getString(cursor
                .getColumnIndex(Contacts.DISPLAY_NAME)));
        view.setTag(viewHolder);
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        final ViewHolder viewHolder = (ViewHolder)view.getTag();
        viewHolder.textView.setText(cursor.getString(cursor
                .getColumnIndex(Contacts.DISPLAY_NAME)));
        viewHolder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(),viewHolder.textView.getText().toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }

 private class ViewHolder {
        public  TextView textView;
    }



Activity:

private static final String[] PROJECTION = new String[] { Contacts._ID,
			Contacts.DISPLAY_NAME };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Cursor cursor = getContentResolver().query(Contacts.CONTENT_URI,
				PROJECTION, null, null, null);
		startManagingCursor(cursor);
		setListAdapter(new MyCursorAdapter(getApplicationContext(), cursor,false));
	}



5.扩展



    if(cursor.getCount() == cursor.getPosition()){


        }else{
            String name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            viewHolder.editeText.setText(name);
        }
cursor.getPosition()  获得当前下标(0开始)
cursor.moveToLast()  移动到最末
cursor.getCount()   获得数量
cursor.isLast()   判断是否最后一个,false:不再最后一个,包括超出范围



 if(cursor.getCount() =< cursor.getPosition()){}     判断是否超出末端


        }



6.代码

注意:demo为android studio工程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值