尝试使用asynctask进行延迟加载。它的75%工作正常;能够在doInBackground()
中运行方法。但加载后UI未更新。我意识到内容不存储在我声明的数组中(如果我没有使用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;
}
答案 0 :(得分:1)
您的适配器在内部保留自己的数据结构。这意味着如果你想改变它的状态,你必须直接操作它。
在您的情况下,您应该在工作完成后再次在适配器中设置mStrings和dStrings。
当您希望在任务运行时与UI进行交互时,例如当您想要显示进度条时,可以使用publishUpdate和onProgressUpdate。
答案 1 :(得分:0)
您不应该在doInBackground中使用Classvars。构建一个结果对象并将其传递给onPostExecute,它在UI-Thread中运行。在那里你可以设置你的String []或适配器或任何你想要的。
和什么gwa sais。我刚才看到,我的“解决方案”只是一个“更好的练习”,我猜...当然你必须让适配器知道这个变化。或者你要告诉它,它的基础数据结构已经改变,或者你通过使用适配器的方法来改变数据结构。
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;
}
我希望您了解this有关AsyncTask的网站?