Assets的使用,外部sql文件,并显示在listview中
2015-04-05 15:43
253 查看
参考博客:http://blog.csdn.net/xiaanming/article/details/8679521
最近做毕设,一直在研究数据库方面的问题,学会了利用Assets,这个文件夹使用AssetManager类来进行访问。
Android要使用外部资源文件一般有两种方法,分别是:
不过Android系统提供了一个AssetManager工具类,它提供对应用程序的原始资源文件进行访问;这个类提供了一个低级别的API,它允许以简单的字节流的形式打开和读取和应用程序绑定在一起的原始资源文件。通过getAssets()方法获取AssetManager对象。
1. 先在Activity里面调用getAssets()来获取AssetManager引用。我们首先要做的是把数据库文件“jingdian_db”复制到代码工程的assets文件夹下,assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。
2. 再用AssetManager的open(String fileName)方法则指定读取的文件以及访问模式就能得到输入流InputStream。
3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close() 。
4.调用AssetManager.close()关闭AssetManager。
首先外部导入最主要的是,第一次使用的时候,判断是否已经存在了相应的文件,如果没有,再导入
同样,打开数据库文件,不是以前的SQLiteDatabase db = getWritableDatabase()/getReadableDatabase();
而是: SQLiteDatabase sqLite = SQLiteDatabase.openOrCreateDatabase("data/data/com.test.phonebooks/files/people_db", null);
path传值:data/data/包名/files/数据库名称
本次操作的数据库有四个字段,分别是_id,name,age,position
这是创建表stuff时的属性:"create table stuff(_id integer primary key autoincrement, name text not null, age integer, position text not null)"
MainActivity类:
ResultActivity类:
还有一个Person类:
源码下载
最近做毕设,一直在研究数据库方面的问题,学会了利用Assets,这个文件夹使用AssetManager类来进行访问。
Android要使用外部资源文件一般有两种方法,分别是:
1.第一种是res目录下存放的可编译的资源文件:
这种资源文件系统会在R.java里面自动生成该资源文件的ID,所以访问这种资源文件比较简单,通过R.XXX.ID即可;2.第二种是assets目录下存放的原生资源文件:
因为系统在编译的时候不会编译assets下的资源文件,所以我们不能通过R.XXX.ID的方式访问它们。我们不能通过该资源的绝对路径去访问它们,因为apk安装之后会放在/data/app/**.apk目录下,以apk形式存在,asset/res和被绑定在apk里,并不会解压到/data/data/YourApp目录下去,所以我们无法直接获取到assets的绝对路径,因为它们根本就没有。不过Android系统提供了一个AssetManager工具类,它提供对应用程序的原始资源文件进行访问;这个类提供了一个低级别的API,它允许以简单的字节流的形式打开和读取和应用程序绑定在一起的原始资源文件。通过getAssets()方法获取AssetManager对象。
1. 先在Activity里面调用getAssets()来获取AssetManager引用。我们首先要做的是把数据库文件“jingdian_db”复制到代码工程的assets文件夹下,assets文件夹里面的文件都是保持原始的文件格式,需要用AssetManager以字节流的形式读取文件。
2. 再用AssetManager的open(String fileName)方法则指定读取的文件以及访问模式就能得到输入流InputStream。
3. 然后就是用已经open file 的inputStream读取文件,读取完成后记得inputStream.close() 。
4.调用AssetManager.close()关闭AssetManager。
首先外部导入最主要的是,第一次使用的时候,判断是否已经存在了相应的文件,如果没有,再导入
private void importDB() { File file = new File(getFilesDir(), DbName); // String DbName = "people_db"; // 判断是否存在 if (file.exists() && file.length() > 0) { Log.i("import_db", "已存在"); } else { // 使用AssetManager类来访问assets文件夹 AssetManager asset = getAssets(); InputStream is = null; FileOutputStream fos = null; try { is = asset.open(DbName); fos = new FileOutputStream(file); int len = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
同样,打开数据库文件,不是以前的SQLiteDatabase db = getWritableDatabase()/getReadableDatabase();
而是: SQLiteDatabase sqLite = SQLiteDatabase.openOrCreateDatabase("data/data/com.test.phonebooks/files/people_db", null);
path传值:data/data/包名/files/数据库名称
本次操作的数据库有四个字段,分别是_id,name,age,position
这是创建表stuff时的属性:"create table stuff(_id integer primary key autoincrement, name text not null, age integer, position text not null)"
MainActivity类:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
// 定义两个按钮
private Button submit, look;
static String DbName = "people_db";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 根据id 获取到相对应的控件
submit = (Button) findViewById(R.id.button1);
look = (Button) findViewById(R.id.button2);
// 按钮设置点击监听
submit.setOnClickListener(this);
look.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
importDB();
break;
case R.id.button2:
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setClass(this, ResultActivity.class);
startActivity(intent);
break;
default:
break;
}
}
private void importDB() { File file = new File(getFilesDir(), DbName); // String DbName = "people_db"; // 判断是否存在 if (file.exists() && file.length() > 0) { Log.i("import_db", "已存在"); } else { // 使用AssetManager类来访问assets文件夹 AssetManager asset = getAssets(); InputStream is = null; FileOutputStream fos = null; try { is = asset.open(DbName); fos = new FileOutputStream(file); int len = 0; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
}
ResultActivity类:
import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; import com.test.mode.Person; public class ResultActivity extends Activity implements OnItemClickListener { private ListView listView; private DbAdapter adapter; // 打开数据库 static SQLiteDatabase sqLite = SQLiteDatabase.openOrCreateDatabase( "data/data/com.test.phonebooks/files/people_db", null); public static final String TABLE_NAME = "stuff"; List<Person> bookList = new ArrayList<Person>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result); // 获取ListView listView = (ListView) findViewById(R.id.listView1); bookList = queryData(); // 实例化DbAdapter adapter = new DbAdapter(getApplication(), bookList); listView.setAdapter(adapter); listView.setOnItemClickListener(this); } // 查询数据库,将每一行的数据封装成一个person 对象,然后将对象添加到List中 private List<Person> queryData() { List<Person> list = new ArrayList<Person>(); // 调用query()获取Cursor Cursor c = sqLite.query(TABLE_NAME, null, null, null, null, null, null, null); while (c.moveToNext()) { int _id = c.getInt(c.getColumnIndex("_id")); String name = c.getString(c.getColumnIndex("name")); int age = c.getInt(c.getColumnIndex("age")); String position = c.getString(c.getColumnIndex("position")); // 用一个Person对象来封装查询出来的数据 Person p = new Person(); p.set_id(_id); p.setName(name); p.setAge(age); p.setPosition(position); list.add(p); } return list; } // 自定义DbAdapter public class DbAdapter extends BaseAdapter { private List<Person> list; private Context context; private LayoutInflater layoutInflater; public DbAdapter(Context context, List<Person> list) { layoutInflater = LayoutInflater.from(context); this.context = context; this.list = list; } // 刷新适配器,更新数据 public void refresh(List<Person> list) { this.list = list; notifyDataSetChanged(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { Person p = list.get(position); ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = layoutInflater.inflate(R.layout.item, null); holder.name = (TextView) convertView .findViewById(R.id.textView1); holder.age = (TextView) convertView .findViewById(R.id.textView2); holder.position = (TextView) convertView .findViewById(R.id.textView3); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.name.setText(p.getName()); holder.age.setText("" + p.getAge()); holder.position.setText(p.getPosition()); return convertView; } public class ViewHolder { public TextView name; public TextView age; public TextView position; public TextView id; } } @Override public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { final Person p = bookList.get(position); final long temp = id; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("真的要删除该记录?") .setPositiveButton("是", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 查询数据库是否是关闭状态 Boolean bb = sqLite.isOpen(); if (bb == false) { // 如果是关闭状态,就打开数据库,否则会报错 sqLite = SQLiteDatabase .openOrCreateDatabase( "data/data/com.test.phonebooks/files/people_db", null); } // 查询完毕后,会关闭数据库,此时操作数据库删除条目,会报错 /* * 错误信息:java.lang.illegalstateexception * android.database. * sqlite.SQLiteDatabase.acquireference() */ sqLite.delete(TABLE_NAME, "_id=?", new String[] { String.valueOf(p.get_id()) }); // 重新刷新适配器,并重新查询 adapter.refresh(queryData()); } }).setNegativeButton("否", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create().show(); // 关闭数据库 sqLite.close(); } }
还有一个Person类:
public class Person { private String name; private int age; private String position; private int _id; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } public int get_id() { return _id; } public void set_id(int _id) { this._id = _id; } }
源码下载
相关文章推荐
- 访问外部存储中的数据库文件,使用CursorAdapter显示在ListView组件上
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 4 - 使用ViewHolder提升ListView的性能]
- ssm项目使用Mybatis动态拼接sql语句,生成的sql中文全部显示为???的问题(配置文件sql语句中文解析问题)
- 本地接收百度等外部文件显示结果 使用TcpClient可避免HttpWebRequest的常见错误
- 搞定使用MySQL导入外部的SQL文件执行
- Json文件放入Assets文件,读取解析并且放入listview中显示。
- Python下使用help(dict),显示'more'不是内部或外部命令,也不是可运行的程序或批处理文件,该如何处理?
- mysql导入导出数据中文乱码解决方法小结(1、navicat导入问题已解决,创建连接后修改连接属性,选择高级->将使用Mysql字符集复选框去掉,下拉框选择GBK->导入sql文件OK;2、phpmyadmin显示乱码的问题也解决,两步:1.将sql文件以utf8的字符集编码另存,2.将文件中sql语句中的字段字符集编码改成utf8,导入OK)
- 使用TreeView和listView控件查看本机磁盘文件夹及文件(WinForm)
- 使用C#在进度条中显示复制文件的进度
- 使用C#在进度条中显示复制文件的进度
- Enterprise Library 2.0 技巧(1):如何使用外部配置文件
- 使用C#在进度条中显示复制文件的进度
- 在Enterprise Library for .NET Framework 2.0中使用外部的配置元数据的文件(my.config)
- Atlas入门-将普通控件升级为Atlas控件/使用listView显示数据库数据[视频]
- 使用C#在进度条中显示复制文件的进度
- [懵懂边缘]使用C#在进度条中显示复制文件的进度
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- VC6中使用CHtmlView在对话框控制中显示HTML文件