数据库读取方法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了数据库读取方法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4508字,纯文字阅读大概需要7分钟。
内容图文
![数据库读取方法](/upload/InfoBanner/zyjiaocheng/526/90383bb9744b49cd810e111f8ad6c9c0.jpg)
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());
}
}
数据库读取方法
标签:class oid gets 读取 art 方法 cto sequence private
本文系统来源:https://www.cnblogs.com/liumin-txgt/p/13261888.html
内容总结
以上是互联网集市为您收集整理的数据库读取方法全部内容,希望文章能够帮你解决数据库读取方法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。