2012-08-26 115 views
0

您好我正在尝试创建一个ASyncTask,它将解析来自后台XML文件的数据,然后在ListView中显示该字符串数组数据。我不明白到目前为止我做错了什么,或者如何将字符串数组值返回给我的GUI。这里是我迄今为止所用的代码,如果你需要其他的东西让我知道请。感谢您查看并提供任何建议或地点,以了解更多信息。ASyncTask和从一个返回字符串数组值

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中没有出现错误模拟器。

回答

1

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

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

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

+0

谢谢你,我很抱歉,在阅读更多关于这个主题之前,我跳到了这里,我有一点ADD,有时只是觉得这么笨,但我想尽我所能去学习。我更新了我的代码,这些代码是基于我已阅读的内容以及我找到的其他示例,但它仍然影响了我的问题。如果您有任何意见,我会很乐意听到它,即使它是最负面的东西可以说,我需要对我正在做的事情提供投入,所以我可以改变。谢谢西蒙。 – kikurself

+0

我相信,SO是针对学习者和经验丰富的程序员的,由社区方法驱动。作为一名学习者,您唯一需要做的就是证明您已经为解决问题做出了一些努力。毕竟,要求人们花时间帮助时,这是公平的。因此,回到你的问题带来的新问题。显示你的代码,如果你得到一个异常,logcat输出。如果你这样做,你会发现许多愿意帮助的人,因为帮助学习者是改善你的代表的好方法。祝你好运! – Simon

2
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()

相关问题