アプリを通常どおり実行すると、bnfフラグメントのlistfragmentは常に空白になります。ただし、fetch storiesメソッドの前にブレークポイントを設定し、コードを1行ずつステップ実行すると、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;
}
}