Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-Asynctask不将数据传递到私有数组?_Android_Android Asynctask - Fatal编程技术网

Android-Asynctask不将数据传递到私有数组?

Android-Asynctask不将数据传递到私有数组?,android,android-asynctask,Android,Android Asynctask,尝试使用asynctask进行延迟加载。其75%的工作罚款;能够在doInBackground()中运行方法。但UI在加载后不会更新。我意识到这些内容并没有存储在我声明它们应该存储的数组中(如果我没有使用asynctask)。查看了onProgressUpdate和publishUpdate,但不确定如何使用它们。运行searchContent()后,数据将存储在mStrings[]和dStrings[]中,以便将其传递给我的适配器。有什么帮助吗 HelloClass.java public c

尝试使用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();           
    }


}
编辑:

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; 

}
受保护类任务扩展异步任务{
受保护的void onPreExecute(){
dialog=ProgressDialog.show(HelloClass.this,“正在检索信息”,“请等待几秒钟…”,true,false);
}
受保护的void doInBackground(字符串[]…参数){
searchContent();
MyResultClass结果=新建MyResultClass();
result.mStrings=mStrings;
result.dStrings=dStrings;
返回结果;
}   
受保护的void onPostExecute(字符串[]结果){
dialog.dismise();
}
}
类MyResultClass
{ 
公共字符串[]mStrings;
公共字符串[]个字符串;
}

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

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


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

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

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


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

您不应该在doInBackground中使用Classvars。构建一个结果对象并将其传递给在UI线程中运行的onPostExecute。您可以在那里设置字符串[]s或适配器或任何您想要的内容

格瓦说了些什么。我刚刚看到,我的“解决方案”只是一个“更好的实践”,我想。。。当然,您必须让适配器知道更改。也就是说,要么您告诉它,它的底层数据结构已经更改,要么您使用适配器的方法更改数据结构

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; 
}
受保护类任务扩展异步任务{
受保护的void onPreExecute(){
dialog=ProgressDialog.show(HelloClass.this,“正在检索信息”,“请等待几秒钟…”,true,false);
}
受保护的MyResultClass doInBackground(字符串[]…参数){
searchContent();
MyResultClass结果=新建MyResultClass();
result.mStrings=mStrings;
result.dStrings=dStrings;
返回结果;
}   
受保护的void onPostExecute(MyResultClass结果){
dialog.dismise();
//在此处设置新的适配器值。
}
}
类MyResultClass
{ 
公共字符串[]mStrings;
公共字符串[]个字符串;
}

我希望您了解有关AsyncTask的网站?

您不应该在doInBackground中使用ClassVar。构建一个结果对象并将其传递给在UI线程中运行的onPostExecute。您可以在那里设置字符串[]s或适配器或任何您想要的内容

格瓦说了些什么。我刚刚看到,我的“解决方案”只是一个“更好的实践”,我想。。。当然,您必须让适配器知道更改。也就是说,要么您告诉它,它的底层数据结构已经更改,要么您使用适配器的方法更改数据结构

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; 
}
受保护类任务扩展异步任务{
受保护的void onPreExecute(){
dialog=ProgressDialog.show(HelloClass.this,“正在检索信息”,“请等待几秒钟…”,true,false);
}
受保护的MyResultClass doInBackground(字符串[]…参数){
searchContent();
MyResultClass结果=新建MyResultClass();
result.mStrings=mStrings;
result.dStrings=dStrings;
返回结果;
}   
受保护的void onPostExecute(MyResultClass结果){
dialog.dismise();
//在此处设置新的适配器值。
}
}
类MyResultClass
{ 
公共字符串[]mStrings;
公共字符串[]个字符串;
}

我希望您了解有关AsyncTask的网站?

您可以在UI线程中运行的AsyncTask中重写onProgressUpdate。在doInBackground中,您将调用progressUpdate向该回调提供数据。您将在UI线程中运行的AsyncTask中重写onProgressUpdate。在doInBackground中,您将调用progressUpdate向该回调提供数据。是否可以传递到postexecute?当然,只需将自己设置为一个内部“ResultClass”。它可以像类ResultClass{public String val1;public String val2;}一样愚蠢,或者更简单:String[],如果它是您正在传递的两个字符串。对不起,所以我将内部类放在了我的doInBackground中?在postExecute中,我添加了return val1和val2?否。在您的类中,在AsyncTask之外,您声明了一个类。例如“类MyResultClass{String[]mStrings;String[]dStrings;}”,然后在AsyncTask实现中,将该类用作doInBackground的返回类型。因此,在doInBackground中,您可以编写类似“MyResultClass result=new MyResultClass();result.mStrings=Blah;result.dStrings=Blubb;return result”的内容;现在,您再次在onPostExecute中将该类型作为参数类型。因此签名是“受保护的void onPostExecute(MyResultType result)”。在那里你可以在UI线程上使用你的结果。谢谢你的例子。抱歉,我不知道我该为“废话”和“Blubb”写些什么,返回结果有错误;我试图删除尽可能多的错误,但仍然无法正常工作。我已经包括了我的