我是android新手。我遵循此tutorial来实现Simple Rss Reader。在这里,在SplashActivity期间,它从net获取提要,并将其传递到下一个活动,即传递到`ListActivity'。

现在,我实现了带有3个选项卡的Swipe选项卡,我想从SplashActivity发送分发包以更新第二个选项卡中的列表视图。

这是一些代码,

SplashActivity.java

public class SplashActivity extends Activity {

private String RSSFEEDURL = "rss feed link goes here";
RSSFeed feed;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

    ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conMgr.getActiveNetworkInfo() == null
            && !conMgr.getActiveNetworkInfo().isConnected()
            && !conMgr.getActiveNetworkInfo().isAvailable()) {
        // No connectivity - Show alert
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "Unable to reach server, \nPlease check your connectivity.")
                .setTitle("TD RSS Reader")
                .setCancelable(false)
                .setPositiveButton("Exit",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                finish();
                            }
                        });

        AlertDialog alert = builder.create();
        alert.show();

    } else {
        // Connected - Start parsing
        new AsyncLoadXMLFeed().execute();

    }

}

private class AsyncLoadXMLFeed extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        // Obtain feed
        DOMParser myParser = new DOMParser();
        feed = myParser.parseXml(RSSFEEDURL);
        return null;

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        Bundle bundle = new Bundle();
        bundle.putSerializable("feed", feed);

        // launch List activity
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);

        // kill this activity
        finish();
    }

}

}


主要活动

public class MainActivity extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
RSSFeed feed;

    // Tab titles
private String[] tabs = { "Home", "Updates", "CastandCrew" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
//  actionBar.setHomeButtonEnabled(false);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // on tab selected
    // show respected fragment view
    viewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}

}


TabsPagerAdapter

 public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new TopRatedFragment();
    case 1:
        // Games fragment activity
        return new GamesFragment();
    case 2:
        // Movies fragment activity
        return new MoviesFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}


这是GamesFragment类

public class GamesFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_games, container, false);

    return rootView;
}
}

最佳答案

您可以通过两种方式

1种方式。

GamesFragment frag = new GamesFragment();
frag.setArguments(getIntent().getExtras());


第二种方式。

首先从包中读取对象。

Object feed;
Bundle extras = getIntent().getExtras();
    if (extras != null) {
      feed = extras.getParcelable("feed");
    }

mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),feed);


//首先如下更改TabsPagerAdapter构造函数。将对象发送到GamesFragment构造函数。

public class TabsPagerAdapter extends FragmentPagerAdapter {

public TabsPagerAdapter(FragmentManager fm, Object feed) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0:
        // Top Rated fragment activity
        return new TopRatedFragment();
    case 1:
        // Games fragment activity
        return new GamesFragment(feed);
    case 2:
        // Movies fragment activity
        return new MoviesFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}

}


//更改类似的构造函数。您可以从构造函数中获取对象。

public class GamesFragment extends Fragment {

        public GamesFragment(Object feed) {
            // TODO Auto-generated constructor stub
            }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_games, container, false);

            return rootView;
            }
        }

10-05 18:41