我的电子辞典

  1 package zzuli.acmen;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.InputStream;
6
7 import android.app.Activity;
8 import android.content.Context;
9 import android.database.Cursor;
10 import android.database.sqlite.SQLiteDatabase;
11 import android.os.Bundle;
12 import android.os.Environment;
13 import android.text.Editable;
14 import android.text.TextWatcher;
15 import android.view.LayoutInflater;
16 import android.view.View;
17 import android.view.ViewGroup;
18 import android.view.View.OnClickListener;
19 import android.widget.AutoCompleteTextView;
20 import android.widget.Button;
21 import android.widget.CursorAdapter;
22 import android.widget.TextView;
23
24 public class Dictionary extends Activity {
25
26 private AutoCompleteTextView myAuto;
27 private Button btn;
28 private TextView text;
29 private SQLiteDatabase database;
30
31 private final String DATABASE_FILENAME = "dictionary.db";
32 private final String DATABASE_PATH = String.valueOf(Environment.getExternalStorageDirectory().getAbsolutePath())+"/dictionary";
33
34 @Override
35 protected void onCreate(Bundle savedInstanceState) {
36 // TODO Auto-generated method stub
37 super.onCreate(savedInstanceState);
38 setContentView(R.layout.main);
39
40 myAuto = (AutoCompleteTextView)this.findViewById(R.id.myauto);
41 btn = (Button)this.findViewById(R.id.btn);
42 text = (TextView)this.findViewById(R.id.text);
43
44 database = openDatabase();
45
46 myAuto.addTextChangedListener(new TextWatcher() {
47
48 @Override
49 public void onTextChanged(CharSequence s, int start, int before, int count) {
50 // TODO Auto-generated method stub
51
52 }
53
54 @Override
55 public void beforeTextChanged(CharSequence s, int start, int count,
56 int after) {
57 // TODO Auto-generated method stub
58
59 }
60
61 @Override
62 public void afterTextChanged(Editable s) {
63 // TODO Auto-generated method stub
64 String str[] = {myAuto.getText()+"%"};
65 Cursor cursor = database.rawQuery("select english as _id from t_words where english like ?",str);
66 if(cursor == null)
67 System.out.println("I'm exmpty");
68 String ss;
69 System.out.println(str[0]);
70 DictionaryAdapter myAdapter = new DictionaryAdapter(Dictionary.this,cursor,true);
71 Dictionary.this.myAuto.setAdapter(myAdapter);
72 }
73 });
74
75
76 btn.setOnClickListener(new OnClickListener() {
77
78 @Override
79 public void onClick(View v) {
80 System.out.println("开始出处");
81 System.out.println("_________________________________");
82 // TODO Auto-generated method stub
83 SQLiteDatabase clickdb = Dictionary.this.database;
84 String str = Dictionary.this.myAuto.getText().toString();
85 System.out.println(str);
86 Cursor localCursor = clickdb.rawQuery("select chinese from t_words where english=?", new String[]{str});
87 System.out.println(localCursor.getCount());
88 if(localCursor.getCount()>0){
89 localCursor.moveToFirst();
90 Dictionary.this.text.setText(localCursor.getString(localCursor.getColumnIndex("chinese")));
91 // Dictionary.this.text.setText("asdasd");
92 // System.out.println(localCursor.getString(localCursor.getColumnIndex("chinese")));
93 }
94 else
95 Dictionary.this.text.setText("未找到相应的单词@——@");
96 }
97 });
98
99 }
100
101 /**
102 * 把数据加到数据库当中!
103 */
104 private SQLiteDatabase openDatabase(){
105
106 Context con = null ;
107
108 System.out.println(this.DATABASE_PATH);
109 String databaseFilename = this.DATABASE_PATH + "/" + this.DATABASE_FILENAME;
110 File dir = new File(this.DATABASE_PATH);
111
112 if(!dir.exists())
113 dir.mkdir();
114
115 if(!(new File(databaseFilename).exists())){
116
117 InputStream in = con.getResources().openRawResource(R.raw.dictionary);
118 try {
119 FileOutputStream fileOut = new FileOutputStream(databaseFilename);
120 byte[] buffer = new byte[7168];
121 int count = 0;
122 // 开始复制dictionary.db文件
123 while ((count = in.read(buffer)) > 0)
124 {
125 fileOut.write(buffer, 0, count);
126 }
127 fileOut.close();
128 in.close();
129
130 } catch (Exception e) {
131 // TODO Auto-generated catch block
132 e.printStackTrace();
133 }
134
135 }
136
137 SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
138 if(database == null)
139 System.out.println("asd");
140 else
141 System.out.println("zzzzzzzzzzzzzzz");
142 return database;
143
144 }
145
146 class DictionaryAdapter extends CursorAdapter{
147
148 private LayoutInflater layoutInflater;
149 // private Cursor cc;
150
151 public DictionaryAdapter(Context context, Cursor c, boolean autoRequery) {
152 super(context, c, autoRequery);
153 this.layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
154 System.out.println("asdasdasd");
155 System.out.println(c.getColumnIndex("_id"));
156 }
157
158 @Override
159 public void bindView(View view, Context context, Cursor cursor) {
160 // TODO Auto-generated method stub
161 setView(view,cursor);
162 }
163
164 private void setView(View view, Cursor cursor) {
165 // TODO Auto-generated method stub
166 TextView textView = (TextView)view;
167 textView.setText(cursor.getString(cursor.getColumnIndex("_id")));
168 System.out.println(cursor.getColumnIndex("_id"));
169 }
170
171 @Override
172 public View newView(Context context, Cursor cursor, ViewGroup parent) {
173 View view = layoutInflater.inflate(R.layout.word, null);
174 setView(view, cursor);
175 return view;
176 }
177
178 @Override
179 public CharSequence convertToString(Cursor cursor) {
180 // TODO Auto-generated method stub
181 return cursor==null?"":cursor.getString(cursor.getColumnIndex("_id"));
182 }
183
184
185 }
186
187 }
posted @ 2011-12-05 10:20  Acmen  阅读(317)  评论(0编辑  收藏  举报