技术标签: CursorAdapter bindView chanageCursor newView 数据库 Android实战开发 App开发实战
CursorAdapter这个类是继承于BaseAdapter的它是一个虚类它为Cursor和ListView连接提供了桥梁
/**
* @see android.widget.ListAdapter#getCount()
*/
public int getCount() {
if (mDataValid && mCursor != null) {
return mCursor.getCount();
} else {
return 0;
}
}
/**
* @see android.widget.ListAdapter#getItem(int)
*/
public Object getItem( int position) {
if (mDataValid && mCursor != null) {
mCursor.moveToPosition(position);
return mCursor;
} else {
return null;
}
}
/**
* @see android.widget.ListAdapter#getItemId(int)
*/
public long getItemId( int position) {
if (mDataValid && mCursor != null) {
if ( mCursor.moveToPosition(position)) {
return mCursor.getLong( mRowIDColumn);
} else {
return 0;
}
} else {
return 0;
}
}
/**
* @see android.widget.ListAdapter# getView(int, View, ViewGroup)
*/
public View getView( int position, View convertView, ViewGroup parent) {
if (!mDataValid) {
throw new IllegalStateException( "this should only be called when the cursor is valid");
}
if (!mCursor.moveToPosition(position)) {
throw new IllegalStateException( "couldn't move cursor to position " + position);
}
View v;
if (convertView == null) {
v = newView( mContext, mCursor, parent);
} else {
v = convertView;
}
bindView(v, mContext, mCursor);
return v;
}
/**
* Makes a new view to hold the data pointed to by cursor.
* @param context Interface to application's global information
* @param cursor The cursor from which to get the data. The cursor is already
* moved to the correct position.
* @param parent The parent to which the new view is attached to
* @return the newly created view.
*/
public abstract View newView (Context context, Cursor cursor, ViewGroup parent);
/**
* Bind an existing view to the data pointed to by cursor
* @param view Existing view, returned earlier by newView
* @param context Interface to application's global information
* @param cursor The cursor from which to get the data. The cursor is already
* moved to the correct position.
*/
public abstract void bindView(View view, Context context, Cursor cursor);
/**
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be
* closed.
*
* @param cursor The new cursor to be used
*/
public void changeCursor (Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
swapCursor(cusor)的源码如下:
/**
* Swap in a new Cursor, returning the old Cursor. Unlike
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em>
* closed.
*
* @param newCursor The new cursor to be used.
* @return Returns the previously set Cursor, or null if there wasa not one.
* If the given new Cursor is the same instance is the previously set
* Cursor, null is also returned.
*/
public Cursor swapCursor (Cursor newCursor) {
if (newCursor == mCursor) {
return null;
}
Cursor oldCursor = mCursor;
if (oldCursor != null) {
if ( mChangeObserver != null) oldCursor.unregisterContentObserver(mChangeObserver );
if ( mDataSetObserver != null) oldCursor.unregisterDataSetObserver(mDataSetObserver );
}
mCursor = newCursor;
if (newCursor != null) {
if ( mChangeObserver != null) newCursor.registerContentObserver(mChangeObserver );
if ( mDataSetObserver != null) newCursor.registerDataSetObserver(mDataSetObserver );
mRowIDColumn = newCursor.getColumnIndexOrThrow("_id" );
mDataValid = true;
// notify the observers about the new cursor
notifyDataSetChanged();
} else {
mRowIDColumn = -1;
mDataValid = false;
// notify the observers about the lack of a data set
notifyDataSetInvalidated();
}
return oldCursor;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder viewHolder= new ViewHolder();
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View view=inflater.inflate(R.layout.item_contacts ,parent,false);
viewHolder. tv_name=(TextView) view.findViewById(R.id.tv_showusername );
viewHolder. tv_phonenumber=(TextView) view.findViewById(R.id.tv_showusernumber );
view.setTag(viewHolder);
Log. i("cursor" ,"newView=" +view);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Log. i("cursor" ,"bindView=" +view);
ViewHolder viewHolder=(ViewHolder) view.getTag();
//从数据库中查询姓名字段
String name=cursor.getString(cursor.getColumnIndex(PersonInfo.NAME));
//从数据库中查询电话字段
String phoneNumber=cursor.getString(cursor.getColumnIndex(PersonInfo.PHONENUMBER));
viewHolder. tv_name.setText(name);
viewHolder. tv_phonenumber.setText(phoneNumber);
}
private void setClickListener() {
btn_save.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
userName=et_name.getText().toString();
userPhoneNumber=et_phonenumber .getText().toString();
if( userName.equals( "")){
Toast. makeText(MainActivity.this, "用户名不能为空!",0).show();
return;
}
if( userPhoneNumber.equals( "")){
Toast. makeText(MainActivity.this,"电话不能为空", 0).show();
return;
}
ContentValues contentValues= new ContentValues();
contentValues.put(PersonInfo. NAME, userName);
contentValues.put(PersonInfo.PHONENUMBER ,userPhoneNumber );
//把EditText中的文本插入数据库
dataBase.insert(PersonInfo. PERSON_INFO_TABLE, null,contentValues);
//根据 _id 降序插叙数据库保证最后插入的在最上面
Cursor myCursor = dataBase.query(PersonInfo. PERSON_INFO_TABLE, null, null, null, null, null, orderBy);
//Cursor改变调用chanageCursor()方法
myCursorAdapter.changeCursor(myCursor);
}
});
}
转载自:http://blog.sina.com.cn/s/blog_71715bf8010166qf.html开篇大话: Object-C语言的异常处理符号和C++、JAVA相似。再加上使用NSException,NSError或者自定义的类,你可以在你的应用程序里添加强大的错误处理机制。异常处理机制是由这个四个关键字支持的:@try,@catch,@thorw,@finally。当代码
问题一:node-sass npm ERR! command failed解决:1、删除 npm uninstall node-sass2、安装 npm install node-sass问题二:npm ERR! gyp info it worked if it ends with oknpm ERR! gyp info using [email protected] ERR! gyp info using [email protected] | win32 | x64npm ERR! gyp ERR!
Android 连接USB默认选中MTP模式需求分析Android系统默认连接USB会显示:正在通过USB为此设备充电,并且无法在电脑查看存储内容。需要实现的效果:Android 连接USB默认选中MTP模式,连接USB显示:正在通过USB传输文件,选择USB的使用方式的弹框下MTP模式为选中状态,并且可以在电脑端可以访问和写入存储空间。解决方案diff --git a/frameworks/base/services/usb/java/com/android/server/usb/UsbD
2012-11-05 作者: 出处:互联网 责编:联想Yoga分期付款月供279元五年前的11月5日,谷歌不仅宣布成立“开放手机联盟”(Open Handset Alliance),表示要帮助创建移动通信的开放标准,而且推出了Android平台——一个基于Linux的智能手机平台。以下是一篇简短的图文介绍,回顾了谷歌手机操作系统的发展。转
案例环境代码#include <stdio.h>int max(int x, int y){ return x > y ? x : y;}int main(){ int (* p)(int ,int) = & max; int a, b, c, d; printf("请输入三个整数:\n"); scanf("%...
译者:赵菁菁(轩语轩缘) 审校:李笑达(DDBC4747)对于任何追求UE4性能最佳、同时又想保持极高质量视觉效果的人来说,本文有一些可遵循的一般性建议和原则。 局限性为了性能,你通常受CPU时间(通常和游戏设置相关)和GPU时间限制(渲染场景花费的时间)。CPU创建由GPU渲染的场景会耗费一些时间。通常情况下,当你发现游戏的运行速度不像你想要的那么快时,第一步是找出
题目来自于网络,答案是笔者整理的。仅供参考,欢迎指正来源: https://mp.weixin.qq.com/s?__biz=MzI1NDQ3MjQxNA==&amp;mid=2247485779&amp;idx=1&amp;sn=3b06b9923df7f40f887ead8b8a53e50e&amp;chksm=e9c5f0e2deb279f47fbfc3a12a70896bf95fa8c...
Add the content later...
一Homebrew安装git1.安装 Homebrew/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"2.安装gitbrew install git二xcode安装git1.在mac终端中直接输入git.提示是否安装开发者...
1.执行main方法时弹出 Error: A JNI error has occurred, please check your installation and try againe2.执行后控制台报的错误3.进入ClassLoader.java中,4.原因:在开始执行main方法时就已经加载了以java开头的包路径,所有类加载器在加载文件时会抛出异常5.解决方法:改包...
本文章是【opencv】goodFeaturesToTrack源码分析-1的后续,主要描述Shi-Tomasi角点检测算法原理及opencv实现。1、算法原理Shi-Tomasi算法是Harris算法的改进,在Harris算法中,是根据协方差矩阵M的两个特征值的组合来判断是否角点。而在Shi-Tomasi算法中,是根据较小的特征值是否大于阈值来判断是否角点。 这个判断依据是:较小的特征值表示在该特
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# Y