Domanda

I am using a TabActivity with 4 separate Activities - one for each tab.

One of the Activities is a ListView that has a custom ArrayAdapter.

The issue is that when I press the Tab to change to this view, the Activity loads the content in before the view changes, this appears as though nothing happens for a couple of seconds until the xml is loaded and parsed etc.

I have looked for an example but this is my first Android appllication and I am having difficulty in understanding the flow.

Can anyone point me to some code that will allow me to instantly change the view (I can inform user content is loading) while loading the content in the background thread

thank you

EDIT - I am porting code over from an existing iOS app - I wasn't able to better articulate the problem as I didn't realise how the UI thread could be blocked in this situation, and due to the complexity of the existing code and deadline I didn't want to change the structure too much.

I narrowed down the issue before I saw your code Jennifer but it is the solution I used so Ill mark yours as right.

here is what I used if it helps anyone else, I had to put the function I called to trigger the data load onto a background thread and then display the content when that thread had done its work

This class was declared within my

public class TableView extends ListActivity

Which was hard for me to get my head around having not done this before ;)

public class GetContentTask extends AsyncTask<Void, Void, Void> {


    private ProgressDialog pdialog;

    @Override
    protected void onPreExecute(){ 
       super.onPreExecute();
       pdialog = new ProgressDialog(TableView.this);
       pdialog.setTitle(progressDialogTitle);
       pdialog.setMessage(progressDialogMessage);
       pdialog.show();    
    }

    @Override
    protected void onPostExecute(Void result){
       super.onPostExecute(result);
       setUpAndLoadList(); // the function to display the list and fill it with content
       pdialog.dismiss();
    }


    @Override
    protected Void doInBackground(Void... params) {
        doInitialLoad(); // The function to load any xml data from server
        return null;
    }
 }
È stato utile?

Soluzione

You can use a progress Dialog (can inform user content is loading)

ProgressDialog dialog;

private class XMLOperation extends AsyncTask<String, Void, String> {

/*
     * (non-Javadoc)
     * 
     * @see android.os.AsyncTask#onPreExecute()
     */
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        System.out.println("onPreExecute");

        dialog= ProgressDialog.show(mContext, "", "Loading Content....");
              dialog.setCancelable(false);


    }




        @Override
    protected String doInBackground(String... urls) {

            //do your Background task

    }




protected void onPostExecute(String result) {   //dismiss dialog
        try {
if(dialog.isShowing()){
        dialog.dismiss();
        }



        } catch (Exception exception) {
        dialog.dismiss();
        }
        }

Altri suggerimenti

Use AsyncTask, or (possibly) a separate thread.

http://developer.android.com/reference/android/os/AsyncTask.html

I would also throw in my 2 cents and say don't use TabActivity. Just have your own buttons that look like tabs, but that's not really critical to this topic.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top