因为要和项目对接,所以今天修改了查询界面,并完成了查询功能的核心代码,下面附上代码。数据库连接出现了问题,准备明天解决。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Main" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#FFFFFF"> <Spinner android:id="@+id/province" android:layout_width="fill_parent" android:layout_height="45px" android:layout_marginLeft="75px" android:layout_marginRight="20px" android:layout_marginTop="7px" android:drawSelectorOnTop="true" android:layout_alignParentRight="true" /> <Spinner android:id="@+id/city" android:layout_width="fill_parent" android:layout_height="45px" android:layout_marginLeft="75px" android:layout_marginRight="20px" android:layout_marginTop="3px" android:drawSelectorOnTop="true" android:layout_alignParentRight="true" android:layout_below="@id/province" /> <Button android:id="@+id/SearchFloor" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <TableLayout android:id="@+id/BuildingTable" android:layout_width="match_parent" android:layout_height="wrap_content"> </TableLayout> </ScrollView> </LinearLayout>
package com.example.superman.test; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; public class SearchEmptyClassroom extends Activity { private String[] teaching_building = new String[] {"基教", "第一教学楼","第二教学楼"}; private String[] default_floor = new String[]{"一楼","二楼","三楼","四楼","五楼","六楼"}; private String[][] floor = new String[][]{{"一楼","二楼","三楼","四楼","五楼","六楼"},{"一楼","二楼","三楼","四楼"},{"一楼","二楼","三楼","四楼","五楼"}}; private Spinner sp; private Spinner sp2; private Context context; private String Building; private String Floor; private Button SearchFloor; private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private final int MP = ViewGroup.LayoutParams.MATCH_PARENT; private TableLayout tableLayout; ArrayAdapter<String> adapter ; ArrayAdapter<String> adapter2; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.searchemptyclassroom); context = this; SearchFloor=findViewById(R.id.SearchFloor); SearchFloor.setOnClickListener(SearchFloorListener); adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, teaching_building); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp = findViewById(R.id.province); sp.setAdapter(adapter); sp.setOnItemSelectedListener(selectListener1); adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, default_floor); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sp2 = (Spinner) findViewById(R.id.city); sp2.setAdapter(adapter2); sp2.setOnItemSelectedListener(selectListener2); } private AdapterView.OnItemSelectedListener selectListener1 = new AdapterView.OnItemSelectedListener(){ public void onItemSelected(AdapterView parent, View v, int position, long id){ int pos = sp.getSelectedItemPosition(); Building=sp.getSelectedItem().toString(); Toast.makeText(SearchEmptyClassroom.this,"当前选中的教学楼是"+Building,Toast.LENGTH_SHORT).show(); adapter2 = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, floor[pos]); sp2.setAdapter(adapter2); } public void onNothingSelected(AdapterView arg0){ } }; private AdapterView.OnItemSelectedListener selectListener2=new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Floor=sp2.getSelectedItem().toString(); Toast.makeText(SearchEmptyClassroom.this,"当前选中的教学楼是"+Floor,Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } }; private View.OnClickListener SearchFloorListener=new View.OnClickListener() { @Override public void onClick(View v) { GetDATA(); } }; public void GetDATA() { // com.test.db 是程序的包名,请根据自己的程序调整 // /data/data/com.test.db/ // databases 目录是准备放 SQLite 数据库的地方,也是 Android 程序默认的数据库存储目录 // 数据库名为 test.db String DB_PATH = "/data/data/com.example.superman.test/databases/"; String DB_NAME = "course.db"; // 检查 SQLite 数据库文件是否存在 if ((new File(DB_PATH + DB_NAME)).exists() == false) { // 如 SQLite 数据库文件不存在,再检查一下 database 目录是否存在 File f = new File(DB_PATH); // 如 database 目录不存在,新建该目录 if (!f.exists()) { f.mkdir(); } try { // 得到 assets 目录下我们实现准备好的 SQLite 数据库作为输入流 //InputStream is = getBaseContext().getAssets().open(DB_NAME); InputStream is = getBaseContext().getResources().openRawResource(R.raw.course); // 输出流 OutputStream os = new FileOutputStream(DB_PATH + DB_NAME); // 文件写入 byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } // 关闭文件流 os.flush(); os.close(); is.close(); } catch (Exception e) { e.printStackTrace(); } } test(DB_PATH,DB_NAME); } public void test(String DB_PATH,String DB_NAME) { // 下面测试 /data/data/com.test.db/databases/ 下的数据库是否能正常工作 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(DB_PATH + DB_NAME, null); Cursor cursor = database.rawQuery("select * from course where teacher='基教'", null); ArrayList<JavaBean> list=new ArrayList<JavaBean>(); if(cursor.moveToFirst()) { do{ String name=cursor.getString(cursor.getColumnIndex("name")); String teacher=cursor.getString(cursor.getColumnIndex("teacher")); double price=cursor.getDouble(cursor.getColumnIndex("price")); list.add(new JavaBean(name,teacher,price)); }while (cursor.moveToNext()); } // if (cursor.getCount() > 0) { // cursor.moveToFirst(); // try { // // 解决中文乱码问题 // byte test[] = cursor.getBlob(0); // String strtest = new String(test, "utf-8").trim(); // // 看输出的信息是否正确 // Log.d("是否能够查找到本地数据库的信息",strtest); // System.out.println(strtest); // } catch (UnsupportedEncodingException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } cursor.close(); ArrayList<String> arrayList=new ArrayList<String>(); String string=null; for(int i=0;i<list.size();i++) { string=list.get(i).getName()+"-"+list.get(i).getTeacher()+"-"+list.get(i).getPrice(); arrayList.add(string); } tableLayout=findViewById(R.id.BuildingTable); //清除表格所有行 tableLayout.removeAllViews(); //全部列自动填充空白处 tableLayout.setStretchAllColumns(true); for(int i=0;i<arrayList.size();i++) { TableRow tableRow=new TableRow(this); //tv用于显示 TextView tv=new TextView(this); tv.setHeight(300); tv.setText(arrayList.get(i).toString()); tableRow.addView(tv); tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC,1)); } } }