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;
}
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.
I hope you know this Website about AsyncTask?