由于最近项目需要写了一次简单的从本地文件夹下获取数据库文件,通过将数据库文件保存到手机内存中查询数据库的方式来获得全国省市县信息,废话说完,直接代码和工具类。
1. 将本地数据库文件打开并读取到手机内存中。
/**
* 将 assets 文件夹下的 数据文件写入 内存 方便获取
* 文件夹下 有的话 就打开 没有的话 就创建打开
* @author ymc
*
*/
public class OpenSQLiteUtils {
//数据库存储路径
String filePath = "data/data/com.zcsoft.zhichengsoft/districtsdatabase.db";
// 文件夹
String pathStr = "data/data/com.zcsoft.zhichengsoft";
SQLiteDatabase database;
public SQLiteDatabase openDatabase(Context context){
System.out.println("filePath:"+filePath);
File jhPath=new File(filePath);
//查看数据库文件是否存在
if(jhPath.exists()){
//存在则直接返回打开的数据库
return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
}else{
//不存在先创建文件夹
File path=new File(pathStr);
try {
//得到资源
AssetManager am= context.getAssets();
//得到数据库的输入流
InputStream is=am.open("districtsdatabase.db");
//用输出流写到SDcard上面
FileOutputStream fos=new FileOutputStream(jhPath);
//创建byte数组 用于1KB写一次
byte[] buffer=new byte[1024];
int count = 0;
while((count = is.read(buffer))>0){
fos.write(buffer,0,count);
}
//最后关闭就可以了
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
//如果没有这个数据库 我们已经把他写到SD卡上了,然后在执行一次这个方法 就可以返回数据库了
return openDatabase(context);
}
}
}
以上代码 简单的将工程包下的数据库文件读取并保存。
2.主界面 (布局就不写了。就一个列表来呈现信息就可以)
/**
* 选择 地区界面
* @author ymc
*
*/
public class AreaActivity extends BaseActivity implements OnItemClickListener{
static String CLICK_CITY = "com.zcsoft.client.clickcity";
static String CLICK_DISTRICTS = "com.zcsoft.client.clickdistricts";
@ViewInject(R.id.listView)
ListView lv;
private List<AreaBean> provinces = new ArrayList<AreaBean>(); // 存放省份的集合
private List<AreaBean> citys = new ArrayList<AreaBean>(); // 存放城市的集合
private List<AreaBean> districts = new ArrayList<AreaBean>(); // 存放省份的集合
private int lable = 0; // 省 = 0 市 = 1 县 =2
String name = null;
private AreaBean bean = new AreaBean(); // 选中的 信息
private OpenSQLiteUtils utils;
private SQLiteDatabase db;
private String sql;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initContentView(R.layout.activity_lv_detail);
ViewUtils.inject(this);
initView();
initData();
}
private void initView() {
rlTitle.setVisibility(View.VISIBLE);
mTextViewTitle.setText("选择省份");
mRadioGroup.setVisibility(View.GONE);
lv.setOnItemClickListener(this);
}
private void initData() {
utils = new OpenSQLiteUtils();
db =utils.openDatabase(getApplicationContext());
//查询数据库中testid=1的数据
sql = "select * from provinces order by province_id";
cursor = db.rawQuery(sql,null);
if(cursor.moveToFirst()){
do {
String name=cursor.getString(cursor.getColumnIndex("province_name"));
String code = cursor.getString(cursor.getColumnIndex("province_id"));
AreaBean areaBean= new AreaBean();
areaBean.setProvinceId(code);
areaBean.setProvinceName(name);
provinces.add(areaBean);
} while (cursor.moveToNext());
}
lv.setAdapter(new AreaAdapter(provinces, 0, this));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ib_baseactivity_back:
finish();
break;
}
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
switch (lable) {
case 0: // 选择省份 查询 城市
mTextViewTitle.setText("选择城市");
lable = 1;
String provinceName = provinces.get(position).getProvinceName();
bean.setProvinceName(provinceName);
sql = "select * from cities where province_id ='"+
provinces.get(position).getProvinceId()+"'";
cursor = db.rawQuery(sql,null);
if(cursor.moveToFirst()){
do {
String name=cursor.getString(cursor.getColumnIndex("city_name"));
String code = cursor.getString(cursor.getColumnIndex("city_id"));
AreaBean areaBean= new AreaBean();
areaBean.setCityId(code);
areaBean.setCityName(name);
citys.add(areaBean);
} while (cursor.moveToNext());
lv.setAdapter(new AreaAdapter(citys, 1, this));
}
break;
case 1: // 选择城市 显示 县
mTextViewTitle.setText("选择地区");
lable = 2;
String cityName = citys.get(position).getCityName();
bean.setCityName(cityName);
String sql = "select * from districts where city_id ='"+
citys.get(position).getCityId()+"'";
Cursor cursor = db.rawQuery(sql,null);
if(cursor.moveToFirst()){
do {
String name=cursor.getString(cursor.getColumnIndex("district_name"));
String code = cursor.getString(cursor.getColumnIndex("district_id"));
AreaBean areaBean= new AreaBean();
areaBean.setDistrictId(code);
areaBean.setDistrictName(name);
districts.add(areaBean);
} while (cursor.moveToNext());
lv.setAdapter(new AreaAdapter(districts, 2, this));
}else{
Intent intent = new Intent(CLICK_CITY);
Bundle bundle = new Bundle();
bundle.putSerializable("Address", bean);
intent.putExtras(bundle);
sendBroadcast(intent);
finish();
}
break;
case 2: // 选择县城
String districtsName = districts.get(position).getDistrictName();
bean.setDistrictName(districtsName);
Intent intent = new Intent(CLICK_DISTRICTS);
Bundle bundle = new Bundle();
bundle.putSerializable("Address", bean);
intent.putExtras(bundle);
sendBroadcast(intent);
finish();
break;
}
}
}
就这么简单。(可以将BaseActivity 改为其他。 本人开发工具为 eclipse。菜鸡一个,互相学习。)
数据库文件地址 http://download.youkuaiyun.com/detail/qq_27948659/9658153