【问题标题】:ASyncTask and returning String array values from oneASyncTask 并从一个返回字符串数组值
【发布时间】:2012-08-21 02:40:56
【问题描述】:

您好,我正在尝试创建一个 ASyncTask,它将在后台解析 XML 文件中的数据,然后在 ListView 中显示该字符串数组数据。到目前为止,我不明白我做错了什么,或者如何将字符串数组值返回到我的 GUI。这是我到目前为止所拥有的代码,如果您需要其他任何内容,请告诉我。感谢您寻找并提供任何建议或地方以了解更多信息。

package com.lvlup.kikurself.scotttest;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import java.io.IOException;
import org.xml.sax.SAXException;

//import com.lvlup.kikurself.scotttest.WikiParser.Cm;

public class scottPlayers extends ListActivity {

public class PlayerGet extends AsyncTask<Void, Void, Void>{


@Override
protected void onPostExecute(Void result){

WikiParser p = new WikiParser();
ArrayList<String> titles = new ArrayList<String>();

try {
    p.parseInto(new URL("http://scottlandminecraft.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Players&cmlimit=500&format=xml"), titles);

} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (SAXException e) {}

    //String[] values = new String[50]; 
    //values = res;




    ArrayAdapter<String> adapter = new ArrayAdapter<String>(scottPlayers.this, R.layout.main, titles);

    setListAdapter(adapter);

    //final ListView playersList = getListView();


    /*playersList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long thisID)
    {
         Object o = (playersList.getItemAtPosition(position));
         String playerName_temp = (o.toString());

         Intent newIntent = new Intent(v.getContext(), playerDisp.class);
         newIntent.putExtra("tempN", playerName_temp);
         startActivity(newIntent);

    }
    });*/

}

 @Override
 protected void onPreExecute() {
  // TODO Auto-generated method stub
  Toast.makeText(scottPlayers.this,
    "onPreExecute \n: preload bitmap in AsyncTask",
    Toast.LENGTH_LONG).show();
 }


 @Override
 protected Void doInBackground(Void... params) {
  // TODO Auto-generated method stub

  return null;
 }


}


@Override
public void onCreate(Bundle icicle) {
 super.onCreate(icicle);

 setContentView(R.layout.main);

 new PlayerGet().execute();
}


    //get your data back again with: String fName = getIntent().getExtras().getInt("fname");




}

编辑:我在查看其他示例后添加了新代码,这是我想出的,但现在当我在模拟器中运行它时,它只显示我的 Main.xml 的背景,列表没有填充,如果我将此导入运行 ICS 的手机,它说出现错误并强制关闭...由于某种原因,我无法在模拟器中发生这种情况,并且模拟器的 LogCat 中没有错误。

【问题讨论】:

    标签: android arraylist android-asynctask return arrays


    【解决方案1】:
    public class scottPlayers extends ListActivity {
    
        private ArrayAdapter<String> mAdapter;
    
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
    
            mAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, new ArrayList<String>());
    
            setListAdapter(mAdapter);
    
            new PlayerGet().execute();
        }
    
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            Object o = l.getItemAtPosition(position);
            String playerName_temp = o.toString();
    
            Intent newIntent = new Intent(v.getContext(), playerDisp.class);
            newIntent.putExtra("tempN", playerName_temp);
            startActivity(newIntent);
        }
    
    
        private class PlayerGet extends AsyncTask<Void, Void, ArrayList<String>> {
    
            WikiParser p = new WikiParser();
            ArrayList<String> titles = new ArrayList<String>();
    
            @Override
            protected ArrayList<String> doInBackground(Void... urls) {
                try {
                    p.parseInto(new URL("http://scottlandminecraft.wikia.com/api.php?action=query&list=categorymembers&cmtitle=Category:Players&cmlimit=500&format=xml"), titles);
                } catch (MalformedURLException e) {
                } catch (IOException e) {
                } catch (SAXException e) {}
                return titles;
            }
    
            @Override
            protected void onPostExecute(ArrayList<String> result) {
                for (String item : result) {
                    mAdapter.add(item);
                }
            }
        }
    }
    

    要更改的代码太多,请阅读AsyncTask docs。尤其是execute()onPostExecute()

    【讨论】:

      【解决方案2】:

      请养成在寻求帮助之前阅读文档的习惯。您需要的一切都是here

      具体来说,您需要在您的 PlayerGet 类中实现 onPostExecute。它会在后台任务完成时调用,并将 ArrayList 作为回调的参数返回给您。

      还有一些有用的提示。始终遵循标准的 Java 命名约定。你的类名“scottPlayers”应该以大写字母开头。除非绝对必要,否则请尽量避免使用 Object。类型转换和类型检查将在以后为您省去一个痛苦的世界。

      【讨论】:

      • 谢谢你,很抱歉我在阅读更多关于这个主题之前就跳到这里,我有一点 ADD,有时只是觉得这一切很愚蠢,但我想学习尽我所能。我根据我读过的内容和我发现的其他示例更新了我的代码,但它仍然给我带来了问题。可以说,我需要输入我正在做的事情,这样我才能改变。谢谢西蒙。
      • 我相信 SO 适合学习者和经验丰富的程序员,由社区方法驱动。作为学习者,您唯一需要做的就是表明您已经为解决问题付出了一些努力。毕竟,只有在要求人们花时间提供帮助时才公平。因此,请针对您遇到的问题提出一个新问题。显示您的代码,如果您遇到异常,则显示 logcat 输出。如果你这样做,你会发现很多人愿意提供帮助,因为帮助学习者是提高你的代表的好方法。祝你好运!
      猜你喜欢
      • 2015-04-30
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 1970-01-01
      • 2012-01-07
      相关资源
      最近更新 更多