我收集了大约 500 封信件和演讲,其中一些非常大(大约 49,000 个字符)。我从 MySQL 导出这些信件和演讲,并将它们导入到 sqlite 中,以便在 Android 应用程序中使用它们。
应用程序工作正常,并且对于小写字母速度很快。然而,当用户选择其中一个大字母(大约有 49,000 个字符的字母)时,将需要很长时间才能打开该字母。我所说的“打开”信件是指使用 setText
函数将信件内容插入到 TextView 中,如下所示:
myDbHelper2 = new DataBaseHelpername(this.getApplicationContext());
myDbHelper2= new DataBaseHelpername(this);
try {
myDbHelper2.createDataBase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper2.openDataBase();}
catch (SQLException sqle) {
sqle.printStackTrace();
}
db = myDbHelper2.getReadableDatabase();
mCursor= db.rawQuery("SELECT field_translation_fa_value,my_id FROM name WHERE my_id ="+id+" ;",null);
mCursor.moveToFirst();
String ff=mCursor.getString(0);
txtmatnfa.setText(ff);
我可以做什么来优化它?
问题出在哪里?从插入(setText)还是从选择(sqlite)?
最佳答案
尝试从另一个线程读取数据库,例如使用 AsyncTask :
new AsyncTask<Void,Void,String>(){
@Override
protected String doInBackground(Void... params) {
myDbHelper2 = new DataBaseHelpername(YourActivity.this.getApplicationContext());
myDbHelper2= new DataBaseHelpername(YourActivity.this);
try {
myDbHelper2.createDataBase();
}
catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper2.openDataBase();}
catch (SQLException sqle) {
sqle.printStackTrace();
}
db = myDbHelper2.getReadableDatabase();
mCursor= db.rawQuery("SELECT field_translation_fa_value,my_id FROM name WHERE my_id ="+id+" ;",null);
mCursor.moveToFirst();
String ff=mCursor.getString(0);
return ff;
}
@Override
protected void onPostExecute(String ff) {
super.onPostExecute(s);
txtmatnfa.setText(ff);
}
}.execute();
关于java - 向 TextView 和低速应用程序插入 49.000 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30212750/