Попытка использовать asynctask для отложенной загрузки. 75% работает нормально; возможность запустить метод в doInBackground()
. Но пользовательский интерфейс не обновляется после загрузки. Я понял, что содержимое не хранится в тех массивах, которые я объявил, для которых они должны (если я не использовал asynctask). Видел onProgressUpdate
и publishUpdate
, но не уверен, как их использовать. После выполнения searchContent()
данные сохраняются в mStrings[]
и dStrings[]
, чтобы их можно было передать моему адаптеру. Любая помощь?
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;
}