
1、下载的数据库 db_weather.db 放到sdcard/weather 目录下面 方便后续 *** 作
2、使用 SQLite Database Browser 可以打开数据库 查看数据 和表等信息
3、了解了表的构成可以实现 *** 作了androidManifest.xml配置文件声明 添加 *** 作sdcard 权限
4、布局文件main.xml主要使用两个 spinner 分别实现城市 省份的选择
Android中ListView的A-Z字母排序和过滤搜索功能并且实现汉字转成拼音的功能,一般对联系人,城市列表等实现A-Z的排序,因为联系人和城市列表可以直接从数据库中获取它的汉字拼音,而对于一般的数据,实现A-Z的排序,基实只需要将汉字转换成拼音就行了。
以下为步骤:
SortModel 一个实体类,里面一个是ListView的name,另一个就是显示的name拼音的首字母。
2.SideBar类就是ListView右侧的字母索引View,需要使用setTextView(TextView mTextDialog)来设置用来显示当前按下的字母的TextView,以及使用setOnTouchingLetterChangedListener方法来设置回调接口,在回调方法onTouchingLetterChanged(String s)中来处理不同的 *** 作。
3.CharacterParser 这个类是将汉字转换成拼音的类,该拼音没有声调的,该类是单例类,其中定义了三个方法,在这个demo中用到的是getSelling(String chs)方法,将词组转换成拼音。
4.ClearEditText类是自定义的一个在右侧有删除图片的EditText,当然也可以用Android原生的EditText,这里就不贴上代码了Android 带清除功能的输入框控件ClearEditText,仿IOS的输入框。
5.SortAdapter 数据的适配器类,该类需要实现SectionIndexer接口,该接口是用来控制ListView分组的。
6.最后运行效果
// 检查数据库是否有效private boolean checkDataBase() {
// SQLiteDatabase checkDB = null
// String myPath = DB_PATH + DB_NAME
File file = new File(DB_PATH, DB_NAME)
return file.exists()
}
public DBCtrl(Context context) {
super()
synchronized (lock) {
this.context = context
DB_PATH = context.getResources().getString(R.string.dbpath)
boolean dbExist = checkDataBase()
if (dbExist) {
// 数据库已存在,do nothing.
} else {
// 创建数据库
try {
File dir = new File(DB_PATH)
if (!dir.exists()) {
dir.mkdirs()
}
File dbf = new File(DB_PATH + DB_NAME)
if (dbf.exists()) {
dbf.delete()
}
// SQLiteDatabase.openOrCreateDatabase(dbf, null)
// 复制asseets中的db文件到DB_PATH下
copyDataBase()
} catch (IOException e) {
throw new Error("数据库创建失败")
}
}
}
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getResources()
.openRawResource(R.raw.db)
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName)
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024]
int length
while ((length = myInput.read(buffer)) >0) {
myOutput.write(buffer, 0, length)
}
// Close the streams
myOutput.flush()
myOutput.close()
myInput.close()
// }
}
我用的这个 构造 helper时候没有数据库就复制了
我觉得你失败的原因不是复制有问题而是数据太大了
android2.3以前不能直接读大于1m的资源的
你把数据库分割成小块复制过来时拼起来吧
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)