0

每当我正常运行我的应用程序时,bnf 片段的列表片段总是空白的。但是,如果我在 fetch stories 方法之前设置断点并逐行执行代码,则 listfragment 会更新!我粘贴的最后一个类是一个 AsycnTask,它获取用于更新列表视图的所有数据。我在想我在线程方面做错了什么,但我不确定发生了什么。对于它的价值,添加的第一个片段也是一个异步任务,它工作正常。

public class NasaAppActivity extends Activity implements ActionBar.TabListener{

private Bundle savedInstanceState;
private static View mainView;
private FragmentTransaction ft;
BreakingNewsFragment bnf; //a list fragment
NasaDailyImage ndi; //another fragment

@Override
public void onStart(){
    super.onStart(); 
    if(savedInstanceState==null){
        ndi=new NasaDailyImage(this);
        bnf=new BreakingNewsFragment(this);     
        ft=getFragmentManager().beginTransaction();
        ft.add(R.id.focused_view_container,ndi).commit();
        ft=getFragmentManager().beginTransaction();
        ft.add(R.id.focused_view_container,bnf).commit();
    }

public void onTabSelected(Tab tab, FragmentTransaction f) {

    switch(tab.getPosition()){ //switches which fragment is visible
    case 0:
        ft=getFragmentManager().beginTransaction();


            ft.hide(bnf);
            ft.show(ndi);
            ft.commit();

           getFragmentManager().executePendingTransactions();


        break;
    case 1:
            ft=getFragmentManager().beginTransaction();
            ft.hide(ndi);
            ft.show(bnf);

            ft.commit();
            bnf.fetchStories();
            bnf.updateList(); //updates the list fragment
          getFragmentManager().executePendingTransactions();

         break;
    }

}

}


@SuppressLint("ValidFragment")
public class BreakingNewsFragment extends ListFragment {
    private static Activity mainActivity;
    private static ArrayList<Story> stories=new ArrayList<Story>();
    private static ArrayList<String> storyTitles=new ArrayList<String>();

    public BreakingNewsFragment(){

    }
    public BreakingNewsFragment(Activity mainActivity){
        this.mainActivity=mainActivity;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        // TODO Auto-generated method stub
        super.onHiddenChanged(hidden);

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);



    }
    public void fetchStories(){
        RssNewsParser parser=new RssNewsParser(mainActivity);
        parser.execute("");
        stories=parser.getStories();
        for(Story story:stories){
            storyTitles.add(story.getTitle());
        }
        String result;
        result = storyTitles.toString();
    }
public void updateList(){
    String result;
    result = storyTitles.toString();
    setListAdapter(new ArrayAdapter<String>(mainActivity,android.R.layout.simple_list_item_1,storyTitles));
}
    @Override
    public void onStart() {
        // TODO Auto-generated method stub
        super.onStart();




    }

}

public class RssNewsParser extends AsyncTask<ArrayList<Story>,String,ArrayList<Story>>{
    private ProgressDialog dialog;
    URL newsURL;
    ArrayList<Story> stories=new ArrayList<Story>();
    int eventType;
    int storyCount= -1;
    private Activity mainActivity;

    RssNewsParser(Activity mainActivity){
        this.mainActivity=mainActivity;
    }
    public ArrayList<Story> getStories(){
        return stories;
    }





        @Override
        protected void onPostExecute(ArrayList<Story> result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            dialog.hide();
        //this.cancel(true);
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        dialog=ProgressDialog.show(mainActivity, "Loading", "loading news");
    }
    @Override
    protected ArrayList<Story> doInBackground(ArrayList<Story>... params) {
        try{

        newsURL = new URL("http://www.nasa.gov/rss/breaking_news.rss");//set URl                         
        BufferedReader in = new BufferedReader(new InputStreamReader(newsURL.openStream()));//get rss
        XmlPullParserFactory factory;
        factory = XmlPullParserFactory.newInstance();//new factory
        factory.setNamespaceAware(true);
        XmlPullParser xpp;
        xpp = factory.newPullParser();
        xpp.setInput(in);
        eventType = xpp.getEventType();//returns an int which mean different things (START_DOCUMENT,START_TAG,etc)



    while(eventType!=XmlPullParser.END_DOCUMENT){//while the document has words

     switch(eventType){

        case XmlPullParser.START_DOCUMENT://beginning of xml
            break;
        case XmlPullParser.START_TAG://case : at beginning of new tag
            String tagName=xpp.getName();

            if(tagName.equals("item")){
                Story story=new Story();
                stories.add(story);
                storyCount++;
                xpp.getDepth();
            }
            if(tagName.equals("title")&& !stories.isEmpty()){
                xpp.getDepth();
                stories.get(storyCount).setTitle(xpp.nextText());
            }
            if(tagName.equals("link") && !stories.isEmpty()){
                stories.get(storyCount).setURL(xpp.nextText());
                xpp.getDepth();
            }
            if(tagName.equals("description")&& !stories.isEmpty()){
                stories.get(storyCount).setDescription(xpp.nextText());
                xpp.getDepth();
            }

            break;

        }
        eventType=xpp.next();
    }//switch   
        in.close();//close BufferedReader
    } catch (MalformedURLException e){
        e.printStackTrace();
    }catch(XmlPullParserException e1){
        e1.printStackTrace();
    }catch(IOException e2){
        e2.printStackTrace();
    }     
        return stories;
    }

}
4

2 回答 2

1

我的列表视图仅在使用调试器时更新

这里

    parser.execute("");
    stories=parser.getStories(); //<<<<<<< here

您正试图AsyncTask在执行AsyncTask. 要么使用AsyncTask.get()获取结果的方法并等待doInBackground执行未完成,要么onPostExecute使用ListViewdoInBackground. 改变你onPostExecute的:

@Override
 protected void onPostExecute(ArrayList<Story> result) {
   // TODO Auto-generated method stub
    super.onPostExecute(result);
     // get data here
     stories=getStories();
    BreakingNewsFragment.this.setListAdapter(new ArrayAdapter<String>(mainActivity,
                         android.R.layout.simple_list_item_1,stories));
     dialog.hide();

    }
于 2013-03-08T06:39:00.187 回答
1

您将 fetchStories 方法视为阻塞调用。这意味着,您希望您的代码等到该方法完成,直到调用调用 updateStories 方法的下一行。但是,由于 fetchStories 是异步工作的,所以在您调用 updateStories 时它还没有完成。

解决此问题的方法是从 AsyncTask 的 onPostExecute 方法调用 updateStories。此方法的目的是允许您在任务完成其主要工作(您放入 doInBackground 的内容)后,在启动 AsyncTask 的线程(通常是 UI 线程,因此您可以进行更新)上执行代码

它在您调试时起作用的原因是因为您正在减慢代码的执行速度,以便在调用 updateStories 之前完成 fetchStories。在设备上,情况并非如此。

简短的回答,从 onPostExecute 更新您的列表。

于 2013-03-08T06:50:37.770 回答