数据库读取方法

数据库读取方法,第1张

概述packagecom.example.e18;importjava.io.File;importjava.io.FileOutputStream;importjava.io.InputStream;importandroid.os.Bundle;importandroid.app.Activity;importandroid.content.Context;importandroid.database.Cursor;importandroid.database.sqlite.SQLi

package com.example.e18;

import java.io.file;
import java.io.fileOutputStream;
import java.io.inputStream;

import androID.os.Bundle;
import androID.app.Activity;
import androID.content.Context;
import androID.database.Cursor;
import androID.database.sqlite.sqliteDatabase;
import androID.support.v4.Widget.CursorAdapter;
import androID.text.Editable;
import androID.text.TextWatcher;
import androID.vIEw.LayoutInflater;
import androID.vIEw.Menu;
import androID.vIEw.VIEw;
import androID.vIEw.VIEw.OnClickListener;
import androID.vIEw.VIEwGroup;
import androID.Widget.autoCompleteTextVIEw;
import androID.Widget.button;
import androID.Widget.TextVIEw;

public class MainActivity extends Activity implements OnClickListener,TextWatcher{
private TextVIEw tv;
private final String DATABASE_PATH=androID.os.Environment.
getExternalStorageDirectory().getabsolutePath()+"/dictionary";
private autoCompleteTextVIEw word;
private final String DATABASE_filename="dictionary.db";
private sqliteDatabase database;
private button searchWord;
private TextVIEw showResult;

@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.activity_main);
// tv=(TextVIEw)findVIEwByID(R.ID.textVIEw1);
// tv.setText(DATABASE_PATH);
database=openDatabase();
searchWord=(button)findVIEwByID(R.ID.searchWord);
word=(autoCompleteTextVIEw)findVIEwByID(R.ID.word);
searchWord.setonClickListener(this);
word.addTextChangedListener(this);
showResult=(TextVIEw)findVIEwByID(R.ID.result);
}

public class DictionaryAdapter extends CursorAdapter{
private LayoutInflater layoutInflater;

public CharSequence convertToString(Cursor cursor){
return cursor==null ? "" : cursor.getString(cursor.getColumnIndex("_ID"));
}

private voID setVIEw(VIEw vIEw,Cursor cursor){
TextVIEw tvWordItem=(TextVIEw)vIEw;
tvWordItem.setText(cursor.getString(cursor.getColumnIndex("_ID")));
}

public DictionaryAdapter(Context context, Cursor c,boolean autoRequery) {
super(context, c,autoRequery);
layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@OverrIDe
public voID bindVIEw(VIEw arg0, Context arg1, Cursor arg2) {
setVIEw(arg0,arg2);

}

@OverrIDe
public VIEw newVIEw(Context arg0, Cursor arg1, VIEwGroup arg2) {
VIEw vIEw=layoutInflater.inflate(R.layout.word_List.item, null);
setVIEw(vIEw,arg1);
return vIEw;
}

}


private sqliteDatabase openDatabase(){
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.dictionary);
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;

}

@OverrIDe
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@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

}

@OverrIDe
public voID afterTextChanged(Editable s) {
String sql="select english as _ID from t_words where english like ?";

Cursor cursor=database.rawquery(sql, new String[]{s.toString()+"%"});
DictionaryAdapter dictionaryAdapter=new DictionaryAdapter(this,cursor,true);
word.setAdapter(dictionaryAdapter);

}

@OverrIDe
public voID onClick(VIEw v) {
String sql="select chinese from t_words where engList=?";
Cursor cursor=database.rawquery(sql, new String[]{word.getText().toString()});
String result="未找到该单词!";
if(cursor.getCount()>0){
cursor.movetoFirst();
result=cursor.getString(cursor.getColumnIndex("chinese")).replace("&", "&");
}
showResult.setText(word.getText()+"\n"+result.toString());
}

}

总结

以上是内存溢出为你收集整理的数据库读取方法全部内容,希望文章能够帮你解决数据库读取方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1059435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存