【最后的沖刺】android中excel表的導入和數據處理

【最后的沖刺】android中excel表的導入和數據處理?——學校課程的查詢和修改

?

1.編寫 The Class類把課程表courses.db當做一個實體類,hashcode和equals這兩個類是為了判斷輸入的查詢內容和Excel表中的內容是否一致。

并在java里面區別兩個對象是否一致

 1 public class TheClass {
 2     private String classname;
 3     private String type;
 4     private String teacher;
 5     private String classroom;
 6     public String getClassname() {
 7         return classname;
 8     }
 9     public void setClassname(String classname) {
10         this.classname = classname;
11     }
12     public String getType() {
13         return type;
14     }
15     public void setType(String type) {
16         this.type = type;
17     }
18     public String getTeacher() {
19         return teacher;
20     }
21     public void setTeacher(String teacher) {
22         this.teacher = teacher;
23     }
24     public String getClassroom() {
25         return classroom;
26     }
27     public void setClassroom(String classroom) {
28         this.classroom = classroom;
29     }
30     @Override
31     public int hashCode() {
32         final int prime = 31;
33         int result = 1;
34         result = prime * result
35                 + ((classname == null) ? 0 : classname.hashCode());
36         result = prime * result
37                 + ((classroom == null) ? 0 : classroom.hashCode());
38         result = prime * result + ((teacher == null) ? 0 : teacher.hashCode());
39         result = prime * result + ((type == null) ? 0 : type.hashCode());
40         return result;
41     }
42     @Override
43     public boolean equals(Object obj) {
44         if (this == obj)
45             return true;
46         if (obj == null)
47             return false;
48         if (getClass() != obj.getClass())
49             return false;
50         TheClass other = (TheClass) obj;
51         if (classname == null) {
52             if (other.classname != null)
53                 return false;
54         } else if (!classname.equals(other.classname))
55             return false;
56         if (classroom == null) {
57             if (other.classroom != null)
58                 return false;
59         } else if (!classroom.equals(other.classroom))
60             return false;
61         if (teacher == null) {
62             if (other.teacher != null)
63                 return false;
64         } else if (!teacher.equals(other.teacher))
65             return false;
66         if (type == null) {
67             if (other.type != null)
68                 return false;
69         } else if (!type.equals(other.type))
70             return false;
71         return true;
72     }
73     
74 }

這里必須注意的是hashset是個集合,必須兩者是不同的,那么怎么進行區分呢,就是通過hashcode和equals這兩個類

?

2.編寫Readfile類,導入Excle的類進入sqlite

 1 public class ReadFile {
 2 
 3     public static boolean read2DB(File f, Context con) {
 4         try {
 5             Workbook course = null;
 6             course = Workbook.getWorkbook(f);
 7             Sheet sheet = course.getSheet(0);
 8             HashSet<TheClass> subjects = new HashSet<TheClass>();
 9             Cell cell = null;
10             for (int i = 1; i < sheet.getRows(); i++) {
11                 TheClass tc = new TheClass();
12                 cell = sheet.getCell(2, i);
13                 tc.setClassname(cell.getContents());
14                 cell = sheet.getCell(10, i);
15                 tc.setType(cell.getContents());
16                 cell = sheet.getCell(12, i);
17                 tc.setTeacher(cell.getContents());
18                 cell = sheet.getCell(18, i);
19                 tc.setClassroom(cell.getContents());
20                 System.out.println(tc.getClassname() + tc.getType()
21                         + tc.getTeacher() + tc.getClassroom());
22                 subjects.add(tc);
23             }
24             SQLiteDatabase db = new SQLiteHelper(con, "courses.db")
25                     .getWritableDatabase();
26             for (TheClass tc : subjects) {
27                 ContentValues cv = new ContentValues();
28                 cv.put("classname", tc.getClassname());
29                 cv.put("type", tc.getType());
30                 cv.put("teacher", tc.getTeacher());
31                 cv.put("classroom", tc.getClassroom());
32                 db.insert("table1", null, cv);
33             }
34             return true;
35         } catch (Exception e) {
36             // TODO Auto-generated catch block
37             e.printStackTrace();
38             return false;
39         }
40     }
41 }

?

3.編寫sqlite幫助類,通過它可以比較快的創建數據對象,創建表,刪除表

 1 public class SQLiteHelper extends SQLiteOpenHelper {
 2 
 3     public SQLiteHelper(Context context, String name, CursorFactory factory,
 4             int version) {
 5         super(context, name, factory, version);
 6     }
 7     public SQLiteHelper(Context con, String name){
 8         this(con, name, null, 1);
 9     }
10     
11     @Override
12     public void onCreate(SQLiteDatabase db) {
13         // TODO Auto-generated method stub
14         db.execSQL("create table table1(classname varchar(20), type varchar(10), teacher varchar(20), classroom varchar(20))");
15     }
16 
17     @Override
18     public void onUpgrade(SQLiteDatabase db, int oldv, int newv) {
19         // TODO Auto-generated method stub
20         db.execSQL("drop table if exists table1");
21         onCreate(db);
22     }
23 
24 }

?

4.編寫主函數MainActivity,添加查詢課程,老師,修改教室,老師等點擊事件,還有剛開始加載Excel表的數據


?

  1 public class MainActivity extends Activity {
  2 
  3     private TextView hello;
  4     private Button b1;
  5     private EditText et;
  6     // private Spinner sp;
  7     private EditText et2;
  8     private SQLiteDatabase db = null;
  9     private TextView type;
 10     private TextView classroom;
 11     private Button editclassroom;
 12     private Button del;
 13     private String classname;
 14     private String teachername;
 15     private EditText edclassroom;
 16 
 17     @Override
 18     protected void onCreate(Bundle savedInstanceState) {
 19         super.onCreate(savedInstanceState);
 20         setContentView(R.layout.activity_main);
 21         db = new SQLiteHelper(this, "courses.db").getWritableDatabase();
 22         hello = (TextView) findViewById(R.id.hello);
 23         b1 = (Button) findViewById(R.id.button1);
 24         b1.setOnClickListener(new View.OnClickListener() {
 25 
 26             @Override
 27             public void onClick(View arg0) {
 28                 LayoutInflater li = LayoutInflater.from(MainActivity.this);
 29                 View view = li.inflate(R.layout.quer, null);
 30                 et = (EditText) view.findViewById(R.id.editText1);
 31                 // sp = (Spinner) findViewById(R.id.spinner1);
 32                 et2 = (EditText) view.findViewById(R.id.EditText01);
 33                 new AlertDialog.Builder(MainActivity.this)
 34                         .setTitle("查詢")
 35                         .setView(view)
 36                         .setPositiveButton("確定",
 37                                 new DialogInterface.OnClickListener() {
 38 
 39                                     @Override
 40                                     public void onClick(DialogInterface arg0,
 41                                             int arg1) {
 42                                         classname = et.getText().toString();
 43                                         teachername = et2.getText().toString();
 44                                         if (null != classname
 45                                                 && null != teachername) {
 46                                             Cursor c = db
 47                                                     .rawQuery(
 48                                                             "select type,classroom from table1 where classname = ? and teacher = ? ",
 49                                                             new String[] {
 50                                                                     classname,
 51                                                                     teachername });
 52                                             LayoutInflater li = LayoutInflater
 53                                                     .from(MainActivity.this);
 54                                             View view = li.inflate(
 55                                                     R.layout.show, null);
 56                                             type = (TextView) view
 57                                                     .findViewById(R.id.type);
 58                                             classroom = (TextView) view
 59                                                     .findViewById(R.id.classroom);
 60                                             editclassroom = (Button) view
 61                                                     .findViewById(R.id.button1);
 62                                             del = (Button) view
 63                                                     .findViewById(R.id.button2);
 64                                             c.moveToNext();
 65                                             type.setText(c.getString(c
 66                                                     .getColumnIndex("type")));
 67                                             classroom.setText(c.getString(c
 68                                                     .getColumnIndex("classroom")));
 69 
 70                                             new AlertDialog.Builder(
 71                                                     MainActivity.this)
 72                                                     .setTitle("查詢結果")
 73                                                     .setView(view)
 74                                                     .setPositiveButton("確定",
 75                                                             null).show();
 76                                             editclassroom
 77                                                     .setOnClickListener(new View.OnClickListener() {
 78 
 79                                                         @Override
 80                                                         public void onClick(
 81                                                                 View arg0) {
 82                                                             LayoutInflater li = LayoutInflater
 83                                                                     .from(MainActivity.this);
 84                                                             View editview = li
 85                                                                     .inflate(
 86                                                                             R.layout.editclassroom,
 87                                                                             null);
 88                                                             edclassroom = (EditText) editview
 89                                                                     .findViewById(R.id.editText1);
 90                                                             new AlertDialog.Builder(
 91                                                                     MainActivity.this)
 92                                                                     .setTitle(
 93                                                                             "新的教室:")
 94                                                                     .setView(
 95                                                                             editview)
 96                                                                     .setPositiveButton(
 97                                                                             "確定",
 98                                                                             new DialogInterface.OnClickListener() {
 99                                                                                 
100                                                                                 @Override
101                                                                                 public void onClick(DialogInterface arg0, int arg1) {
102                                                                                     ContentValues cv = new ContentValues();
103                                                                                     cv.put("classroom", edclassroom.getText().toString());
104                                                                                     db.update("table1", cv, "classname = ? and teacher = ?",
105                                                                                     new String[] {
106                                                                                             classname,
107                                                                                             teachername });
108                                                                                 }
109                                                                             })
110                                                                     .setNegativeButton(
111                                                                             "取消",
112                                                                             null)
113                                                                     .show();
114                                                         }
115                                                     });
116 
117                                             del.setOnClickListener(new View.OnClickListener() {
118 
119                                                 @Override
120                                                 public void onClick(View arg0) {
121                                                     new AlertDialog.Builder(
122                                                             MainActivity.this)
123                                                             .setTitle("警告")
124                                                             .setMessage(
125                                                                     "您正在刪除記錄,確定刪除?")
126                                                             .setPositiveButton(
127                                                                     "確定",
128                                                                     new DialogInterface.OnClickListener() {
129 
130                                                                         @Override
131                                                                         public void onClick(
132                                                                                 DialogInterface arg0,
133                                                                                 int arg1) {
134                                                                             db.delete(
135                                                                                     "table1",
136                                                                                     "classname = ? and teacher = ?",
137                                                                                     new String[] {
138                                                                                             classname,
139                                                                                             teachername });
140                                                                         }
141                                                                     })
142                                                             .setNegativeButton(
143                                                                     "取消", null)
144                                                             .show();
145                                                 }
146                                             });
147 
148                                         }
149                                     }
150 
151                                 }).show();
152             }
153         });
154         File sdpath = Environment.getExternalStorageDirectory();
155         File coursefile = new File(sdpath + File.separator + "courses.xls");
156         if (!coursefile.exists()) {
157             new AlertDialog.Builder(this).setTitle("錯誤").setMessage("未找到文件")
158                     .setPositiveButton("確定", null).show();
159             b1.setVisibility(View.INVISIBLE);
160         } else {
161             hello.setText("找到了文件!");
162             new Important().execute();
163             b1.setVisibility(View.VISIBLE);
164         }
165     }
166 
167     @Override
168     public boolean onCreateOptionsMenu(Menu menu) {
169         // Inflate the menu; this adds items to the action bar if it is present.
170         getMenuInflater().inflate(R.menu.main, menu);
171         return true;
172     }
173 
174     class Important extends AsyncTask<Integer, String, Boolean> {
175         private ProgressDialog pDialog = null;
176 
177         @Override
178         protected void onPreExecute() {
179             // TODO Auto-generated method stub
180             super.onPreExecute();
181             pDialog = new ProgressDialog(MainActivity.this);
182             pDialog.setMessage("正在導入課程,請稍候");
183             pDialog.setIndeterminate(false);
184             pDialog.setCancelable(true);
185             pDialog.show();
186         }
187 
188         @Override
189         protected void onPostExecute(Boolean imp) {
190             // TODO Auto-generated method stub
191             super.onPostExecute(imp);
192             pDialog.dismiss();
193             String result = "";
194             if (imp == true) {
195                 result = "讀取成功!";
196             } else {
197                 result = "讀取失敗!";
198             }
199             new AlertDialog.Builder(MainActivity.this).setTitle("提示")
200                     .setMessage(result).setPositiveButton("確定", null).show();
201 
202         }
203 
204         @Override
205         protected void onProgressUpdate(String... values) {
206             // TODO Auto-generated method stub
207             super.onProgressUpdate(values);
208         }
209 
210         @Override
211         protected Boolean doInBackground(Integer... params) {
212             File sdpath = Environment.getExternalStorageDirectory();
213             File coursefile = new File(sdpath + File.separator + "courses.xls");
214             return ReadFile.read2DB(coursefile, MainActivity.this);
215         }
216     }
217 
218 }

?

?

5.總結一下

?


?

整個過程不是太難,不過要記得導入jxl.jar這個包,整體技術方面就是用到了安卓本身自帶的Sqlite操作方法

轉載于:https://www.cnblogs.com/huiyuan/p/xuexiao.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/375836.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/375836.shtml
英文地址,請注明出處:http://en.pswp.cn/news/375836.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

詳解C++函數模板

函數模板屬于類屬&#xff0c;能夠處理不同的數據類型&#xff0c;當編譯器遇到函數調用是&#xff0c;將根據實際參數的類型產生特定的代碼&#xff0c;函數模板的定義形式是&#xff1a; template <類型參數表> 返回值類型 函數名&#xff08;形式參數表&#xff09;{…

計算機專業女兵,陳豪2010《點解阿Sir》劇照

0陳豪2010《點解阿Sir》劇照2012-07-21 08:24{"info": {"setname": "陳豪2010《點解阿Sir》劇照","imgsum_bk": 20,"imgsum": 20,"lmodify": "2012-07-21 08:24:00","prevue": " "…

ASP.NET MVC學習之Ajax(完結)

一.前言 通過上面的一番學習&#xff0c;大家一定收獲不少。但是總歸會有一個結束的時候&#xff0c;但是這個結束也意味著新的開始。 如果你是從事ASP.NET開發&#xff0c;并且也使用了第三方控件&#xff0c;那么一定會覺得ASP.NET開發ajax十分的簡單&#xff0c;而ASP.NET M…

認知計算機語言學,什么是認知語言學

文獻綜述&#xff1a;“語文素養”內涵研究綜述“語文素養”內涵研究綜述摘要&#xff1a;“語文素養”是新一輪語文課程改革所提出的一個重要概念&#xff0c;其作為語文課程改革的目標與核心理念&#xff0c;擠兌了“語文能力”的核心地位。目前&#xff0c;人們對“語文素養…

data URI scheme及其應用

data URI scheme通俗來講就是圖片直接塞到HTML而不是由HTTP。這樣從表面上看會降低一次HTTP的請求&#xff0c;實現了對于網頁的優化&#xff08;只是看了其它一些文章data URI由于將圖片採用了base 64的編碼方式進行表達&#xff0c;所以還是須要進行HTTP去下載內容&#xff0…

Linux 禁用觸摸板

1&#xff0c;首先需要查看觸摸板&#xff1a; 命令&#xff1a;xinput list 結果&#xff1a; ? Virtual core pointer         id2 [master pointer (3)]    ? ? Virtual core XTEST pointer      id4 […

大學新生學計算機推薦電腦,大學新生用什么電腦好呢?

科技的發展日新月異&#xff0c;數碼的yi巴為你資訊。今天是7月的開頭&#xff0c;我們正式邁入了2019下半年。7月開頭也正是許多大多數高考生快忙完志愿填報&#xff0c;開始考慮大學該選擇什么電腦的時候。今天yi巴就來跟大家聊聊該大學新生該怎么選擇電腦&#xff0c;并給予…

NewCode----句子反轉

題目描述 給定一個句子&#xff08;只包含字母和空格&#xff09;&#xff0c; 將句子中的單詞位置反轉&#xff0c;單詞用空格分割, 單詞之間只有一個空格&#xff0c;前后沒有空格。 比如&#xff1a; &#xff08;1&#xff09; “hello xiao mi”-> “mi xiao hello” …

mac boot2docker certs not valid with 1.7

摘自&#xff1a;https://github.com/boot2docker/boot2docker/issues/824 An error occurred trying to connect: Get https://192.168.59.103:2376/v1.19/containers/json: x509: certificate is valid for 127.0.0.1, 10.0.2.15, not 192.168.59.103 I come with the same p…

對象之間的交互

之前寫過一篇隨筆《剪刀剪紙》是給一些新同事講面向對象時用的&#xff0c;當時就感覺有些不順暢&#xff0c;不過用來給新同事入門足夠了就沒多想&#xff0c;最近看書時偶爾走神把這件事想起來了&#xff0c;順便群里討論時談到聚合之間的方法調用&#xff0c;于是決定寫一篇…

NewCode----數串

題目描述&#xff1a; 設有n個正整數&#xff0c;將他們連接成一排&#xff0c;組成一個最大的多位整數。 如:n3時&#xff0c;3個整數13,312,343,連成的最大整數為34331213。 如:n4時,4個整數7,13,4,246連接成的最大整數為7424613。 輸入描述: 有多組測試樣例&#xff0c…

計算機跨專業專插本學音樂,歡迎投稿丨專插本可以跨專業考,只要肯堅持!

點擊上方△藍字可關注我們昵稱E師姐性別女插本復習資料教材、小紅書、CB398、啟航等插本關注的公眾號、網站等介紹微信公眾號居多&#xff1a;專插本資料庫、專插本直通車、廣東省專插本、插本最前線等等……專科學校和專業廣州城市職業學院 會計插本學校和專業廣東財經大學華商…

Android,監控ContentProvider的數據改變

有時候應用中需要監聽ContentProvider的改變并提供響應&#xff0c;這時候就要利用ContentObserver類了 不管是ContentProvider中實現的,insert,delete,update方法中的任何一個&#xff0c;程序都會調用getContext().getContentResolver().notifyChange(uri,null); 這行代碼可用…

[leetcode]Sort List

題目要求&#xff1a;Sort a linked list in O(n log n) time using constant space complexity. 數據結構定義&#xff1a; 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : v…

北京市中 高英語聽說計算機考,2021年北京高考首次英語聽說機考時間確定,共五種題型...

從明年開始&#xff0c;北京市高考統考英語科目增加口語考試&#xff0c;連同之前的聽力考試一起&#xff0c;實行一年兩考。今天&#xff0c;北京教育考試院發布消息&#xff0c;2021年高考英語聽說計算機考試首考將于2020年12月12日進行。12月7日起&#xff0c;考生可登陸北京…

NewCode----彩色寶石項鏈

題目描述: 有一條彩色寶石項鏈&#xff0c;是由很多種不同的寶石組成的&#xff0c;包括紅寶石&#xff0c;藍寶石&#xff0c;鉆石&#xff0c;翡翠&#xff0c;珍珠等。有一天國王把項鏈賞賜給了一個學者&#xff0c;并跟他說&#xff0c;你可以帶走這條項鏈&#xff0c;但是…

插件開發-UI插件開發

1.新建類庫解決方案&#xff0c;引入命名空間,同時引入要添加UI Form的WebPart(在Portal\UILib目錄下)2.繼續UFSoft.UBF.UI.Custom.ExtendedPartBase&#xff0c;重寫AfterInit()方法&#xff0c;代碼如下&#xff0c;便于添加下拉列表按鈕&#xff0c;在原單據中UI先新增一下拉…

為博客園選擇一個小巧霸氣的語法高亮插件

博客園的語法高亮簡直蛋疼&#xff0c;于是乎就打算找一個靠譜的插件來改造下。各種百度谷歌&#xff0c;大致得到幾個推薦&#xff1a;SyntaxHighlighter&#xff0c;Snippet&#xff0c;Google Code Pretiffy&#xff0c;Highlight&#xff0c;SHJS。其實 SyntaxHighlighter …

計算器軟件設計和計算機軟件設計區別,求一個模擬計算器程序

# include# include# include# include# define MAX_OPERATOR_NUM 100//運算符棧數組長度# define MAX_DATA_NUM 100//運算數棧數組長度typedef struct OPStack//定義運算符棧{char opStack[MAX_OPERATOR_NUM];int top;}OPStack, *pOPStack;typedef struct DATAStack//定義操作…

python中print語句

學習鏈接1 學習鏈接2 1. 如果print語句后面什么符號都沒有是個換行語句&#xff0c;也就是是要另起一行。 2. 分號表示下次輸出是緊挨著這個光標位置輸出. 3. 而逗號是下次輸出與這次輸出有一定的空格之后接著輸出. namekk salutationMr. greetingHello, print greeting,sa…