每当我正常运行我的应用程序时,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;
}
}