0

I have an activity which looks somewhat like: Please dont copy it, It takes three variables from another class and calculates gcd of (a,b) , (b,c) , (c,d) , (a,b,c) and then factorizes the equation and gives five different textViews the string. also if you can help to improve x.square to x2, please help me, all suggestions welcome.

package com.aditya.blabla;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AlternateForms extends Activity {
AnotherClass ec = new AnotherClass ();
int aa = ec.geta();
int bb = ec.getb();
int cc = ec.getc();


int abhcf = HCF(aa, cc), bchcf = HCF(cc, cc), achcf = HCF(aa, cc),
        abchcf = HCF(abhcf, cc);
String altform1,altform2,altform3,altform4,altform5;
TextView t1, t2, t3, t4, t5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alternate);
    t1 = (TextView) findViewById(R.id.textView1);
    t2 = (TextView) findViewById(R.id.textView2);
    t3 = (TextView) findViewById(R.id.textView3);
    t4 = (TextView) findViewById(R.id.textView4);
    t5 = (TextView) findViewById(R.id.textView5);
    altform1 = abhcf + "x(" + aa / abhcf + "x+" + bb / abhcf + ")+"
            + cc;
    setProgress(20);
    altform2 = aa + "x.square" + bchcf + "(" + bb / bchcf + "x" + cc
            / bchcf + ")" + cc;
    setProgress(30);
    altform3 = achcf + "(" + aa / achcf + "x.square" + bb / achcf
            + ")" + cc + "x";
    setProgress(40);
    altform4 = abchcf + "(" + aa / abchcf + "x.square" + bb / abchcf
            + "x" + cc / abchcf + ")";
    setProgress(50);
    altform5 = abchcf + "(" + "x" + "(" + aa / abchcf + "x" + bb
            / abchcf + ")" + cc / abchcf + ")";
    if (abhcf != 1) {
        t1.setText(altform1);

    }
    if (bchcf != 1) {
        t2.setText(altform2);

    }
    if (achcf != 1) {
        t3.setText(altform3);

    }
    if (abchcf != 1) {
        t4.setText(altform4);
        t5.setText(altform5);

    }

}

private int HCF(int m, int n) {
    // TODO Auto-generated method stub
    int hcf = 0;
    while (n != 0) {
        hcf = n;
        n = m % n;
        n = hcf;
    }
    return hcf;
}

}

it has no errors but it takes much process and looks like hung and gives force close dialog any suggestion to process this?

RE60K
  • 621
  • 1
  • 7
  • 26
  • 1
    Just a heads up. By posting this here you've licenced it under [Creative-Commons Sharealike](http://creativecommons.org/licenses/by-sa/3.0/) Which explicitly states others are free to copy and use it...Find the link in the bottom right corner of this page to learn more. – FoamyGuy Dec 12 '12 at 14:57
  • never mind i can produce an army like that – RE60K Dec 12 '12 at 14:59
  • produce an army....?? What? – FoamyGuy Dec 12 '12 at 15:01
  • 1
    Use Thread / Handler or AsyncTask to do your work in the background. It might also be worth it to add in some logging with timestamps to figure out which section of code is actually taking the longest to complete. – FoamyGuy Dec 12 '12 at 15:22
  • if you can,edit the code or else give me link to desc. about the Thread / Handler or AsyncTask – RE60K Dec 12 '12 at 15:25
  • http://www.vogella.com/articles/AndroidPerformance/article.html This article is great for learning about both, and why they are important – FoamyGuy Dec 12 '12 at 15:28
  • No thank you. I have my own work to do. It's not terribly difficult, work through the examples on vogella blog. Once you do you'll be able to apply it to your own situation. – FoamyGuy Dec 12 '12 at 16:50

3 Answers3

1

Put all calls to HCF(m, n) in the doInBackground(Void... params) method in an AsyncTask. Put all of the altformN assignments from the onCreate(Bundle s) method in doInBackground(Void... params) as well. Put all the if statements where you setText(CharSequence s) for each TextView in the onPostExecute(Void nothing) method of that AsyncTask. As you may know, the onPostExecute(Void nothing) method in AsyncTasks gets executed on the UI thread while doInBackground(Void... params) gets executed on a separate thread.

Here is a skeleton implementation:

public class AlternateFormsTask extends AsyncTask<Void, Void, Void> {
    int abhcf, bchcf, achcf, abchcf;
    int aa, bb, cc;
    AnotherClass ec = new AnotherClass ();

    @Override
    public Void doInBackground(Void.... params){
        aa = ec.geta();
        bb = ec.getb();
        cc = ec.getc();
        // ...insert all your calls to HCF(m, n) here
        return null
    }

    // If all the processing in doInBackground really takes that long
    // you may want to intersperse some calls to AsyncTask's publishProgress
    // to update the user as to the progress of the app, otherwise it will appear
    // just as hung as before, just without the FC dialogs.

     @Override
     public void onPostExecute(Void nothing){
        //....do all your setText stuff here
     }
}
jsimon
  • 577
  • 6
  • 17
  • sorry the main reason was the hcf method it should be: – RE60K Dec 13 '12 at 09:31
  • `static int HCF(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; }` – RE60K Dec 13 '12 at 09:31
  • but this was correct for async task .. if you can help to improve x.square to x2, please help me – RE60K Dec 13 '12 at 09:39
  • You are just interested in formatting the text better? `.setText(Html.fromHtml("x2")`. http://stackoverflow.com/questions/3543454/subscript-and-superscript-a-string-in-android/3543625#3543625 – jsimon Dec 13 '12 at 12:39
  • thnx after i edited hcf function it worked smoothly in fraction of seconds – RE60K Dec 13 '12 at 12:45
  • Not to poke, but if you feel as though we've answered your questions, mark the answer accepted. – jsimon Dec 13 '12 at 20:00
0

It will always look "hung" without using Threads when it needs lot of time. Use Threads and update your GUI after it finished. You should do as less work as possible in the MainThread

Sprigg
  • 3,340
  • 1
  • 18
  • 26
0

The hcf method should be changed to

static int HCF(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
RE60K
  • 621
  • 1
  • 7
  • 26