gpt4 book ai didi

Android - 异步任务不将数据传递到私有(private)数组中?

转载 作者:行者123 更新时间:2023-11-30 04:26:00 29 4
gpt4 key购买 nike

尝试使用 asynctask 进行延迟加载。它的 75% 工作正常;能够在 doInBackground() 中运行方法。但是加载后 UI 不会更新。我意识到内容没有存储在我声明的数组中(如果我没有使用 asynctask)。看到了 onProgressUpdatepublishUpdate 但不确定如何使用它们。运行 searchContent() 后,数据将存储在 mStrings[]dStrings[] 中,以便可以将其传递到我的适配器。有帮助吗?

你好类.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();
}


}

适配器.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());
}

编辑:

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;

}

最佳答案

您的适配器在内部保留自己的数据结构。这意味着如果你想改变它的状态,你必须直接在它上面操作。

在您的情况下,您应该在完成工作后在适配器中再次设置 mStrings 和 dStrings。

publishUpdate 和 onProgressUpdate 用于在任务运行时与 UI 交互,例如当您想要显示进度条时。

关于Android - 异步任务不将数据传递到私有(private)数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8516895/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com