2015-11-02 54 views
0

我是一名Java初學者,學習了一些東西並決定嘗試一下。到目前爲止,我已經用C++編寫了一年以上的代碼,至今我注意到了java編碼方面的一些相似之處。我習慣於通過傳遞字符串的名稱和我想要顯示的元素(即:string [i] =「a」)來簡單地傳遞一個字符串的元素,這很簡單,但在java中,我遇到過將字符串及其位置傳遞給某些片段(使用viewpager)時出現的一些問題。 簡而言之,我似乎無法在我的片段上顯示任何文字。我知道我的編碼很混亂,但我非常感謝任何幫助。文字未顯示(初學者)

public class MainActivity extends FragmentActivity { 

    /** 
    * The {@link android.support.v4.view.PagerAdapter} that will provide 
    * fragments for each of the sections. We use a 
    * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which 
    * will keep every loaded fragment in memory. If this becomes too memory 
    * intensive, it may be best to switch to a 
    * {@link android.support.v4.app.FragmentStatePagerAdapter}. 
    */ 
    SectionsPagerAdapter mSectionsPagerAdapter; 

    /** 
    * The {@link ViewPager} that will host the section contents. 
    */ 
    ViewPager mViewPager; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     String [] sir = new String[500]; 
     for(int i = 0; i < 500; i++) { 
      sir[i] = "This is Fragment " + i; 
     } 

     mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), sir); 

     mViewPager = (ViewPager) findViewById(R.id.container); 
     mViewPager.setAdapter(mSectionsPagerAdapter); 

     mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
      @Override 
      public void onPageSelected(int position) { 
      } 

     }); 
    } 


    /** 
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
    * one of the sections/tabs/pages. 
    */ 
    public class SectionsPagerAdapter extends FragmentPagerAdapter { 
     private String [] sir; 

     public SectionsPagerAdapter(FragmentManager fm, String [] stringstodisplay) { 
      super(fm); 
      this.sir = stringstodisplay; 
     } 

     @Override 
     public Fragment getItem(int position) { 
      // getItem is called to instantiate the fragment for the given page. 
      // Return a DummySectionFragment (defined as a static inner class 
      // below) with the page number as its lone argument. 
      Fragment fragment = new DummySectionFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1); 
      fragment.setArguments(args); 
      return fragment; 
     } 


     @Override 
     public int getCount() { 
      // Show 3 total pages. 
      return 500; 
     } 


    } 

    /** 
    * A dummy fragment representing a section of the app, but that simply 
    * displays dummy text. 
    */ 
    public static class DummySectionFragment extends Fragment { 
     /** 
     * The fragment argument representing the section number for this 
     * fragment. 
     */ 
     private String mText; // display this text in your fragment 

     public static Fragment getInstance(String text) { 
      Fragment f = new Fragment(); 
      Bundle args = new Bundle(); 
      args.putString("sir", text); 
      f.setArguments(args); 
      return f; 
     } 

     public void onCreate(Bundle state) { 
      super.onCreate(state); 
      setmText(getArguments().getString("sir")); 
      // rest of your code 
     } 

     public static final String ARG_SECTION_NUMBER = "section_number"; 

     public DummySectionFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.dummyfragment, container, false); 
      TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label); 
      dummyTextView.setText(getArguments().getString(mText)); 
      return rootView; 
     } 

     public String getmText() { 
      return mText; 
     } 

     public void setmText(String mText) { 
      this.mText = mText; 
     } 
    } 
} 
+0

你有沒有遇到任何錯誤,或者你剛到沒有文字的虛擬片段? – SmiffyKmc

+0

不,我沒有得到任何錯誤,它編譯和運行順利。我的片段上沒有任何文字。 –

+0

嘗試測試以查看該字符串是否實際上使其成爲問題片段。您可以使用Simple debugging LOG或System.out.println(getmText())將字符串打印到控制檯。這隻會告訴你該字符串正確傳遞。 – SmiffyKmc

回答

1

替換

dummyTextView.setText(getArguments().getString(mText));

​​



dummyTextView.setText(getArguments().getString("sir"));

這應該工作。

+0

我已經想到了這兩個,然後再次嘗試它們。不幸的是,沒有工作。 (如果我不清楚,我想要顯示在我的片段上的文本將是「This is Fragmet」+片段的位置(例如,對於(int i = 0; i <500 ; i ++){sd [i] =「This is Fragment」+ i;) –

+0

我看到的問題是你沒有使用getInstance()來初始化片段,所以你必須使用'setmText(「」+ getArguments ).getInt(DummySectionFragment.ARG_SECTION_NUMBER));'in'onCreate'' – sha

+0

對,我可以通過參數setmText來查看屏幕上每個片段的位置,我應該如何替換setmText中的「」 (「」+ getArguments()。getInt(DummySectionFragment.ARG_SECTION_NUMBER));爲了能夠處理字符串數組sir?最後是我想要在屏幕上顯示的那個。 –