Android实现Excel数据导入SQLlite,并用ListView展示。实现模糊查询
前言
今天朋友提出一个需求,有一个Excel表格,需要实现把每项数据展示出来,并且可以用某一项来进行查询筛选。首先我们应该思考如何把Excel上的数据存入数据库?我的思路是直接用工具转成数据库文件,复制到项目里面去,然后再查出来展示,这个时候就可以用查询语句来控制我们需要的数据了。
一、怎么把Excel转成数据库文件?
1、把Excel另存为.csv文件
2、然后用记事本打开,另存时选择编码为UTF-8
3、下载 SQLite Expert Professional 4 数据库管理工具
点击直接下载
4、新建数据库
5、选中数据库,右键选择 Import Text File
6、数据库文件在这里,直接复制到Android项目的assets目录下
7、补充 没有Assets文件夹,在Android studio中选中project结构下创建
二、代码实现
1、数据库文件
在android开发时,很多时候我们会使用可视化界面创建数据库,或者拿到别人的数据库使用,这时就需要我们将db文件手动加入到assets文件是夹中并读取。但是当我们把应用的apk部署到真机上的时候,已经创建好的数据库及其里边的数据是不能随着apk一起安装到真机上的。
解决方法很简单,只需要把assets目录下的db文件复制一份到SDCard中的”/data/data/” + packName + “/”目录下就可以了。
public class SQLdm {
//数据库存储路径
//com.example.myapplication是你的包名
//test.dbtest.db是你的数据库文件名称,一定要改成你自己的
String filePath = "data/data/com.example.myapplication/test.db";
//数据库存放的文件夹 data/data/com.example.myapplication 下面
String pathStr = "data/data/com.example.myapplication";
SQLiteDatabase database;
public SQLiteDatabase openDatabase(Context context){
System.out.println("filePath:"+filePath);
File jhPath=new File(filePath);
//查看数据库文件是否存在
if(jhPath.exists()){
Log.i("test", "存在数据库");
//存在则直接返回打开的数据库
return SQLiteDatabase.openOrCreateDatabase(jhPath, null);
}else{
//不存在先创建文件夹
File path=new File(pathStr);
Log.i("test", "pathStr="+path);
if (path.mkdir()){
Log.i("test", "创建成功");
}else{
Log.i("test", "创建失败");
};
try {
//得到资源
AssetManager am= context.getAssets();
//得到数据库的输入流
InputStream is=am.open("test.db");
Log.i("test", is+"");
//用输出流写到SDcard上面
FileOutputStream fos=new FileOutputStream(jhPath);
Log.i("test", "fos="+fos);
Log.i("test", "jhPath="+jhPath);
//创建byte数组 用于1KB写一次
byte[] buffer=new byte[1024];
int count = 0;
while((count = is.read(buffer))>0){
Log.i("test", "得到");
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、创建和数据库字段对应的实体类
代码如下(示例):
public class Test {
private String code;
private String name;
private String car;
private Integer num;
private Double price;
public Test( String code, String name, String car, Integer num, Double price) {
this.code = code;
this.name = name;
this.car = car;
this.num = num;
this.price = price;
}
public Test() {
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCar() {
return car;
}
public void setCar(String car) {
this.car = car;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
3、查询数据并展示
代码如下(示例):
public class MainActivity extends AppCompatActivity {
//展示数据的listview
private ListView lv;
//用来装查询到的数据
private ArrayList<Test> testlist;
//输入查询条件的输入框
private EditText searchertextView;
//查询按钮
private Button find;
//给模糊查询准备的字符串变量
String s1 = "*";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取到输入框
searchertextView = (EditText) findViewById(R.id.text);、
//获取到查询按钮
find = findViewById(R.id.findtext);
//程序启动先调用一次查询,先展示数据给用户
zhanshi();
//点击查询后调用
find.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//若用户什么都没输入,直接全部查出来,要是有输入,就会拼接到
//"select * from testt where Field_2 like '%"+ s1 +"%'" 进行查询
s1 = searchertextView.getText().toString();
zhanshi();
System.out.println("执行了");
}
});
}
//把查询和填充提出来写成单独的方法
@SuppressLint("Range")
public void zhanshi(){
testlist = new ArrayList<>();
SQLdm s = new SQLdm();
SQLiteDatabase db = s.openDatabase(getApplicationContext());
Cursor cursor = db.rawQuery("select * from testt where Field_2 like '%"+ s1 +"%'",null);
System.out.println(cursor.getColumnIndex("name")+"___________________________________________________");
while(cursor.moveToNext()){
String code;
String name;
String car;
Integer num;
Double price;
code = cursor.getString(cursor.getColumnIndex("Field_2"));
name = cursor.getString(cursor.getColumnIndex("Field_3"));
car = cursor.getString(cursor.getColumnIndex("Field_4"));
num = cursor.getInt(cursor.getColumnIndex("Field_5"));
price = cursor.getDouble(cursor.getColumnIndex("Field_6"));
Test st = new Test(code,name,car,num,price);
testlist.add(st);
}
lv = (ListView)findViewById(R.id.zhanshi);
lv.setAdapter(new BaseAdapter() {
/*
* 为ListView设置一个适配器
* getCount()返回数据个数
* getView()为每一行设置一个条目
* */
@Override
public int getCount() {
return testlist.size();
}
@Override
public Object getItem(int position) {
// return testlist.get(position);
return null;
}
@Override
public long getItemId(int position) {
// return position;
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view ;
/**对ListView的优化,convertView为空时,创建一个新视图;
* convertView不为空时,代表它是滚出,
* 放入Recycler中的视图,若需要用到其他layout,
* 则用inflate(),同一视图,用fiindViewBy()
* **/
if(convertView == null )
{
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
view = inflater.inflate(R.layout.item,null);
//view = View.inflate(getBaseContext(),R.layout.item,null);
}
else
{
view = convertView;
}
//从studentlist中取出一行数据,position相当于数组下标,可以实现逐行取数据
Test st = testlist.get(position);
TextView code = (TextView)view.findViewById(R.id.code);
TextView name = (TextView)view.findViewById(R.id.name);
TextView car = (TextView)view.findViewById(R.id.car);
TextView num = (TextView)view.findViewById(R.id.num);
TextView price = (TextView)view.findViewById(R.id.price);
code.setText("编码:——" +st.getCode());
name.setText("名称:——" +st.getName());
car.setText("适用车型:——" +st.getCar());
num.setText("数量:——" +st.getNum());
price.setText("成本价格:——" +st.getPrice()+"");
return view;
}
});
}
}
4、最后给出xml文件参考
4.1、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<EditText
android:id="@+id/text"
android:layout_width="320dp"
android:layout_height="60dp"
android:hint="请输入要查询的编码"></EditText>
<Button
android:id="@+id/findtext"
android:layout_width="70dp"
android:layout_height="40dp"
android:text="查询">
</Button>
</LinearLayout>
<ListView
android:id="@+id/zhanshi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
</ListView>
</LinearLayout>
4.1、item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/code"
android:layout_width="match_parent"
android:layout_height="60dp">
</TextView>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="60dp">
</TextView>
<TextView
android:id="@+id/car"
android:layout_width="match_parent"
android:layout_height="60dp">
</TextView>
<TextView
android:id="@+id/num"
android:layout_width="match_parent"
android:layout_height="60dp">
</TextView>
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="60dp">
</TextView>
</LinearLayout>
5、总结
遇到问题不要害怕,先思考一个小时,如果一个小时都没有思路先放着,或者百度,或者请教别人。不管多难的问题,最终都会得到解决。