从主片段调用片段

时间:2015-01-31 16:36:46

标签: android android-intent android-fragments android-fragmentactivity

我必须从这里调用片段(Page1)......

 protected void onPostExecute(Void args) {
        Intent intentx;
        intentx = new Intent(HomeFragment.this.getActivity(),Main.class);  
        intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intentx);

    }

我的程序我有Main class,看起来像这样

public class Main extends FragmentActivity implements
    ActionBar.TabListener {

private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Express(1 Day)", "Premium (2 Day)", "Normal (3+ Days)" };

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

    // 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) {
}

该类将调用Page1,Page2,Page3

内部页面1代码是这个

public class Page1 extends Fragment  {
public static ArrayList<String> strArr;
public static ArrayList<ArrayList<String>> content;
public static ArrayAdapter<String> adapter;
LinearLayout ll;
View format;
float days_to_del;
int[] no_of_count= new int[20];
int count;
int x;

@Override

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

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



    // super.onResume();
     //View rootView = inflater.inflate(R.layout.page1, container, false);
    for(int j=0;j<10;j++)
    {
        days_to_del=Float.parseFloat(""+Lib.result[j][0].toString());
        if(days_to_del==2.0)
        {
            no_of_count[x]=j;
            x++;
            count++;
        }

    }

    for(int m=0;m<count;m++)
    {


        ll = (LinearLayout) rootView.findViewById(R.id.parent);
      LayoutInflater vi = (LayoutInflater) Page1.this.getActivity().getApplicationContext().getSystemService(Page1.this.getActivity().LAYOUT_INFLATER_SERVICE);

          format = inflater.inflate(R.layout.page1, null);
          ll.addView(format);

          //Lib.ShowAlertMessage(Page1.this.getActivity().getApplicationContext(), "", ""+format.getId());

             if(Lib.result[no_of_count[m]][2].toString().equals("First Flight Couriers"))
             {
                 ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);    
                 String uri = "@drawable/first_flight";
                 int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
                 Drawable res = getResources().getDrawable(imageResource);
                 imageView1.setImageDrawable(res);
             }
             else if(Lib.result[no_of_count[m]][2].toString().equals("SkyNet WorldWide Express"))
             {
                 ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);    
                 String uri = "@drawable/skynet";
                 int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
                 Drawable res = getResources().getDrawable(imageResource);
                 imageView1.setImageDrawable(res);
             }
            else if(Lib.result[no_of_count[m]][2].toString().equals("The Professional Couriers"))
             {
                 ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);    
                 String uri = "@drawable/professional";
                 int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
                 Drawable res = getResources().getDrawable(imageResource);
                 imageView1.setImageDrawable(res);
             }
            else if(Lib.result[no_of_count[m]][2].toString().equals("The Professional Couriers"))
             {
                 ImageView imageView1=(ImageView)rootView.findViewById(R.id.imageView1);    
                 String uri = "@drawable/professional";
                 int imageResource = getResources().getIdentifier(uri, null, Page1.this.getActivity().getApplicationContext().getPackageName());
                 Drawable res = getResources().getDrawable(imageResource);
                 imageView1.setImageDrawable(res);
             }
             TextView textView2=(TextView)rootView.findViewById(R.id.textView2);

             textView2.setText("Rupees");

    }    
    if(format.getId()==1)
     {
         ll.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
         Intent intentx;
        intentx = new Intent(Page1.this.getActivity(), From.class);  //mContext is a Context variable.
        intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
         });
     }
     if(format.getId()==2)
     {
         ll.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
         Intent intentx;
        intentx = new Intent(Page1.this.getActivity(), To.class);
        intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
         });
     }

    return rootView;
}
public void onResume(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
}

}

我的问题  1.我在PostExecute中使用这些变量(Lib.results [] [])它们是可用的(我不需要这里)  2.在片段Page1中,我无法访问Lib.results [] [],一旦它进入Page1,result [] []的值为null,因为值没有更新(这是我猜想的已经hapened)  3.基于这些值的结果将转到相应的片段(Page1,Page2,Page3)

问题 我是否正在调用片段活动?意图以这种方式工作还是有任何其他方法。

1 个答案:

答案 0 :(得分:0)

我认为我已将数组用作String但将其更改为strArray使其工作