import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;
public class main extends Activity implements OnClickListener, TextWatcher {
private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()+ "/myDB";
private final String DATABASE_FILENAME = "etraffic.sqlite";
SQLiteDatabase database;
Button btnSelectWord;
AutoCompleteTextView actvWord;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
database = openDatabase();
btnSelectWord = (Button) findViewById(R.id.btnSelectWord);
actvWord = (AutoCompleteTextView) findViewById(R.id.actvWord);
btnSelectWord.setOnClickListener(this);
actvWord.addTextChangedListener(this);
}
public void onClick(View view)
{
String sql = "SELECT * FROM FreewayPoint_CCTV where cctv_id=?";
Cursor cursor = database.rawQuery(sql, new String[]{
actvWord.getText().toString()
});
String result = "找不到";
if(cursor.getCount()>0)
{
cursor.moveToFirst();
result=cursor.getString(cursor.getColumnIndex("roadsection"));
}
myClass cl =new myClass();
cl.Alert(result, "Title", this);
}
private SQLiteDatabase openDatabase() {
try {
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
if (!dir.exists())
dir.mkdir();
if (!(new File(databaseFilename)).exists()) {
InputStream is = getResources().openRawResource(
R.raw.etraffic);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
} catch (Exception e) {
}
return null;
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
沒有留言:
張貼留言