Reputation: 303
Folks.
I've designed a currency exchange application in which i got all exchange rates once at application start and create own SQLite DB then easily pull them into the converter interface. I've designed the change to be done in my Edittext onTextChanged listener with a textwatcher and all works perfectly. I've also have a listview in the same activity for Favorite exchange rates and its also has to be calculated every time the edittext changes. My main problem is that I've got a slow performance and freeze issues in my application. I've tried to implement Asynctask to process the calculations but it didn't help me and i still get the performance issues. Hereunder my code for your reference. Please Advice !!
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();
}
});
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();
}
Upvotes: 0
Views: 528
Reputation: 3236
What I can imagine of from your description is, you're most likely having 2 EditTexts, where user only input to first, and you will sync the converted value for second, is this true? If it is, may I suggest you not to perform the operation right after every text change?
You may want to do Calculate()
only after user finishes his input, perhaps one second delay of calculation is acceptable?
// Declare these as class variable
private Handler handler = new Handler();
private Runnable calculateRunnable = new Runnable() {
public void run() {
Calculate();
}
}
As for your TextWatcher, change this
public void afterTextChanged(final Editable s) {
handler.removeCallbacks(calculateRunnable);
handler.postDelayed(calculateRunnable, 1000);
}
Upvotes: 3