Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:46:51+00:00 2026-05-27T12:46:51+00:00

Trying to use asynctask for lazy loading. Its 75% working fine; able to run

  • 0

Trying to use asynctask for lazy loading. Its 75% working fine; able to run method in doInBackground(). But UI is not updated after loading. I realised that the contents are not stored in the arrays that I declared which they are supposed to(if I didn’t use asynctask). Saw onProgressUpdate and publishUpdate but not sure how to use them. After running searchContent(), data are being stored in mStrings[] and dStrings[] so that it can be passed to my adapter. Any help?

HelloClass.java

public class HelloClass extends Activity {

ListView list;
LazyAdapter adapter;

ProgressDialog dialog;
private String[] mStrings = {};
private String[] dStrings = {};

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    new TheTask().execute();        

    list=(ListView)findViewById(R.id.list);         
    adapter=new LazyAdapter(this, mStrings, dStrings);
    list.setAdapter(adapter);

}

protected class TheTask extends AsyncTask<Void, Void, Void>{

    protected void onPreExecute() {
        dialog = ProgressDialog.show(HelloClass.this, "Retrieving Information", "Please wait for few seconds...", true, false);
    }

    protected Void doInBackground(Void... params) {
        searchContent();
        return null;
    }

    protected void onPostExecute(Void result) {
        dialog.dismiss();
    }
}

public void searchContent()
{
    String imageC = "";
    String textC = "";


    try {

        URL url = new URL(targetURL);

        // Make the connection
        URLConnection conn = url.openConnection();
        BufferedReader reader = new BufferedReader(
         new InputStreamReader(conn.getInputStream()));

        String line = reader.readLine();

        while (line != null) {

            if(line.contains("../../"))
            {

                String xyz = line.substring(0,xyz.indexOf('"'));
                imageC = xyz +";";                  
                mStrings = imageC.split(";");
                line = reader.readLine();
            }

            if(line.contains("../../") == false)
            {
                line = reader.readLine();
            }

            if (line.contains("Nametag"))
            {
                int startIndex = line.indexOf("Gnametag") + 10;
                int endIndex = line.indexOf("<", startIndex + 1);
                String gname = line.substring(startIndex,endIndex);
                textC = textC.replaceAll("</span>", "");
                textC += "Name: "+gname+ "\n";
            }                   

                if (line.contains("Age"))
                {
                    textC += "Age: "+reader.readLine() + "\n" + ";";
                    textC = textC.replaceAll("                  ", "");
                dStrings = textC.split(";");
                }

            if (line.contains("Last Update"))
            {
                reader.close();
            }                               
        }           

        // Close the reader
        reader.close();

    } catch (Exception ex) {
        ex.printStackTrace();           
    }


}

Adapter.java

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private String[] text;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d, String[] t) {
    activity = a;
    data=d;
    text = t;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());
}

EDITTED:

protected class TheTask extends AsyncTask<Void, Void, Void>{

    protected void onPreExecute() {
        dialog = ProgressDialog.show(HelloClass.this, "Retrieving Information", "Please wait for few seconds...", true, false);
    }

    protected void doInBackground(String[]... params) {
        searchContent();
        MyResultClass result = new MyResultClass();
        result.mStrings = mStrings;
        result.dStrings = dStrings;
        return result;
    }   
    protected void onPostExecute(String[] result) {
        dialog.dismiss();
    }

}

class MyResultClass
{ 
    public String[] mStrings; 
    public String[] dStrings; 

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:46:52+00:00Added an answer on May 27, 2026 at 12:46 pm

    You should not use Classvars in doInBackground. Build up a result object and pass it to onPostExecute, which is run in UI-Thread. There you can set your String[]s or adapter or whatever you want.

    AND what gwa sais. I just saw, that my “solution” is just a “better practice”, I guess … of course you have to make the adapter aware of the change. That is either you tell it, that its underlying datastructure has changed or you change the datastructure by using the adapter’s methods.

    protected class TheTask extends AsyncTask<Void, Void, MyResultClass >{
    
    protected void onPreExecute() {
        dialog = ProgressDialog.show(HelloClass.this, "Retrieving Information", "Please wait for few seconds...", true, false);
    }
    
    protected MyResultClass doInBackground(String[]... params) {
        searchContent();
        MyResultClass result = new MyResultClass();
        result.mStrings = mStrings;
        result.dStrings = dStrings;
        return result;
    }   
    protected void onPostExecute(MyResultClass result) {
        dialog.dismiss();
    // Set new adapter-values here.  
    }
    }
    
    class MyResultClass
    { 
        public String[] mStrings; 
        public String[] dStrings; 
    }
    

    I hope you know this Website about AsyncTask?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was trying to use AsyncTask for lazy load of images in the adapter.
I am trying to use the AsyncTask class on android but I have a
I was trying use a set of filter functions to run the appropriate routine,
I am trying use a Java Uploader in a ROR app (for its ease
Trying to use a guid as a resource id in a rest url but
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
I am trying to use the following code to obtain the result value, but
I am trying to learn how to use the AsyncTask class to authenticate the
I am trying use std::copy to copy from two different iterator. But during course
I'm trying to use an AsyncTask-extended class to handle connecting to a URL, parsing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.