民间。
我设计了一个货币兑换应用程序,我在应用程序启动时获得了一次所有汇率,然后创建自己的SQLite DB,然后轻松将它们拉入转换器界面。我已经设计了使用textwatcher在我的Edittext onTextChanged监听器中完成的更改,并且所有操作都完美无缺。我也在最喜欢的汇率的同一活动中有一个列表视图,并且每次编辑文本更改时也必须计算它。我的主要问题是我的应用程序性能缓慢并冻结了问题。我试图实现Asynctask来处理计算,但它没有帮助我,我仍然得到性能问题。下面我的代码供您参考。请建议!!
Text Watcher:
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();
}
答案 0 :(得分:3)
我可以想象你的描述是,你很可能有2个EditTexts,用户只输入第一个,你将同步转换后的值,这是真的吗?如果是,我建议你不要在每次文字更改后立即执行操作吗?
您可能只想在用户完成输入后才能Calculate()
,也许可以接受一秒的计算延迟?
// Declare these as class variable
private Handler handler = new Handler();
private Runnable calculateRunnable = new Runnable() {
public void run() {
Calculate();
}
}
至于TextWatcher,请更改此
public void afterTextChanged(final Editable s) {
handler.removeCallbacks(calculateRunnable);
handler.postDelayed(calculateRunnable, 1000);
}