Folks。复杂操作的性能下降
我设计了一个货币兑换应用程序,在应用程序启动时我获得了所有汇率,并创建自己的SQLite DB,然后轻松地将它们拖入转换器界面。我使用textwatcher设计了EditTextTextChanged监听器中的更改,并且所有工作都完美无缺。我也在收藏汇率的同一活动中有一个列表视图,每次edittext更改时也必须计算它。我的主要问题是我的应用程序运行缓慢并冻结了问题。我试图实施Asynctask来处理计算,但它没有帮助我,我仍然遇到性能问题。以下我的代码供您参考。请指教 !!
文本守望者:
valval.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(final Editable s)
{
Calculate();
}
});
计算:
private void Calculate()
{
curs = mDb.query(MyDbHelper.TABLE_NAME, columns, MyDbHelper.COL_Common
+ "=" + "?", new String[] { From[xxxto] + From[xxxfrom] },
null, null, null);
cursD = mDb.query(MyDbHelper.TABLE_NAME, columns, MyDbHelper.COL_Common
+ "=" + "?", new String[] { From[xxxfrom] + From[xxxto] },
null, null, null);
curs.moveToFirst();
cursD.moveToFirst();
double selection = curs.getDouble(curs
.getColumnIndex(MyDbHelper.COL_Currone));
double selection2 = cursD.getDouble(cursD
.getColumnIndex(MyDbHelper.COL_Currone));
Long myNum = Long.parseLong(valval.getText().toString().trim());
double myNum3 = Double.parseDouble(new DecimalFormat("#.######").format(myNum * selection2));
valval2.setText(String.valueOf(myNum3));
Cursor B = mDb.query(MyDbHelper.TABLE_NAME, columns,
MyDbHelper.COL_CurrFavor + " LIKE ? And "
+ MyDbHelper.COL_Currsecond + " LIKE ?", new String[] {
"YES", "EUR" }, null, null, null);
for (int s = 0; s < B.getCount() - 1; s++)
{
B.moveToPosition(s);
String ZVZV = B.getString(0);
int BSBS = B.getInt(9);
Cursor curcur = mDb.query(MyDbHelper.TABLE_NAME, columns, MyDbHelper.COL_Common
+ "=" + "?", new String[] { From[xxxfrom] + From[BSBS-1] },
null, null, null);
curcur.moveToFirst();
double calcal = curcur.getDouble(6);
ContentValues args = new ContentValues();
double formattedNumber = Double.parseDouble(new DecimalFormat("#.######").format(myNum * calcal));
args.put(MyDbHelper.COL_Currsum,formattedNumber);
mDb.update(MyDbHelper.TABLE_NAME, args, "_id =" + ZVZV, null);
}
cursm.requery();
}
你真的需要使用此数据的基础上?我确信一位SQL专家会告诉你,你的查询是低效的,但为什么不使用共享首选项呢? – IanB
因为我根据汇率在我的数据库中有大约4700行,我不认为我在共享偏好部分有这样的经验。任何帮助,将不胜感激 ! –
你可以把它分解一下吗? 470种货币,每种47种货币10种,每种100种?其他应用程序是否使用您的数据库? – IanB