1

My app sets up a list of football team names, and on clicking a textview, the corresponding class is called in a the intent method.

The classes that are called load an xml layout file which is approx 200kb. I would like to implement a progress/loading icon whilst the xml file loads in the background.

I've looked on this site and read about ASynchTask but I don't know what code I need to write in the doInBackground() method.

Here is my class:

I have an array of textviews that get initialised, and an array of classnames assigned to each textview when clicked.

Can anybody help me? I'm a noobie.

As an alternative, is there a way of loading all these classes in the background when the user first clicks on the app?

public class Transfers extends Activity {

public final int[] teams = { R.id.ars, R.id.ast, R.id.bir, R.id.bla,
        R.id.blp, R.id.bol, R.id.che, R.id.eve, R.id.ful, R.id.hul,
        R.id.lee, R.id.liv, R.id.mid, R.id.mnc, R.id.mnu, R.id.nor,
        R.id.nwu, R.id.por, R.id.qpr, R.id.sto, R.id.sun, R.id.swa,
        R.id.tot, R.id.wes, R.id.wig, R.id.wol };

public final String[] teamnames = { "ars", "ast", "bir", "bla", "blp",
        "bol", "che", "eve", "ful", "hul", "lee", "liv", "mid", "mnc",
        "mnu", "nor", "nwu", "por", "qpr", "sto", "sun", "swa", "tot",
        "wes", "wig", "wol" };

TextView tv;
Class classname;
String prefix = "ttj.android.ft.teams.";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.m_transfers);

    for (int i = 0; i < teams.length; i++) {
        final int j = i;
        tv = (TextView) findViewById(teams[i]);
        tv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    classname = Class.forName(prefix + teamnames[j]);
                    Intent open = new Intent(Transfers.this, classname);
                    startActivity(open);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    ;
}
tiptopjat
  • 499
  • 3
  • 13
  • 24
  • May be my [answer](http://stackoverflow.com/a/9078502/996493) help you :) – Lucifer May 20 '12 at 02:24
  • @Lucifer Thanks. I've looked at your solution. How do I implent this code into my class? Any help would be appreciated. Also, please move your reply to answers, so I can commend your knowledge! :-) – tiptopjat May 20 '12 at 13:54
  • when you are starting to call your next activity, just display the ProgressDialog, do the background process, and dismiss it in handler, very easy. – Lucifer May 20 '12 at 15:51
  • @Lucifer I've implemented your solution and the progress bar appears when clicking a text link, when the new activity is ready, is is displayed. However, When I press the back key, the progress bar appears again and it does not stop. Calling dismiss outside the for loop causes the app to crash. I'm not using the Handler methods anywhere, should I be? Can you move the comments into the answers section please? That way I can share my code. Thanks. – tiptopjat May 20 '12 at 20:17
  • @Lucifer Fixed it, got it working. I created the following method and called it in onresume(): public void hideProgressDialog() { if(this.pd != null) { this.pd.dismiss(); this.pd = null; } } – tiptopjat May 21 '12 at 12:12

1 Answers1

2

You should forget the progress bar for a moment and rethink using an array of TextView's.

I don't know how you have them on the screen but you should switch to using some sort of AdapterView (i.e. ListView, GridView etc..) And then load an Adapter with all of your teams. That will significantly reduce the total number of actual View objects that you are creating, which will cut down your load time some.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • Thanks. I've invested too much time in this code to play around with the logic! My java skills are at basic to say the least. Trying to work out how to implement the solution proposed by Lucifer – tiptopjat May 20 '12 at 14:24