2016-11-02 3 views
0

私は3つのタブとそれぞれのListFragmentを持つTabLayoutを持っています。これらは、SectionsPagerAdapterによって処理されます。画面を回転させると、彼らがロードされているため、彼らは彼らのListAdaptersを失っているように思えるが、何も起こりません: example imageListFragmentはスクリーンを回転するときに傾きます - Android

実は私は断片に関するライフサイクルについては多くのものを赤ているが、まだ私は私のためにそれを実装することができませんでした問題。

画面を回転すると、アクティビティが破棄され、再びonCreate()が呼び出されます。だから私はそこに次のものを置く:

  1. はアダプタ

を設定するだからここで私の誤謬があるCursorAdapterの新しいインスタンスを作成しますListFragment

  • の新しいインスタンスを作成します。 ?

    EDIT: 私はonSaveInstanceStateメソッドをオーバーライドする問題を解決しました。しかし、記述された問題に至るところに正確に何が保存されていますか?

    MainActivity.java:

    public class MainActivity extends AppCompatActivity { 
    
        private SectionsPagerAdapter mSectionsPagerAdapter; 
        private ViewPager mViewPager; 
        FragmentManager mFragmentManager; 
    
        private ProgressDialog pDialog; 
    
        // Fragments for the three Tabs 
        public Contact fragment_all; 
        public Contact fragment_official; 
        public Contact fragment_alumni; 
    
        // Handler for SQLite DB Access 
        public DBHandler dbhandler; 
    
        private MemberListAdapter ca_all; 
        private MemberListAdapter ca_alu; 
        private MemberListAdapter ca_off; 
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_members); 
    
         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
         setSupportActionBar(toolbar); 
    
         mFragmentManager = getSupportFragmentManager(); 
         mSectionsPagerAdapter = new SectionsPagerAdapter(mFragmentManager); 
    
         // Set up the ViewPager with the sections adapter. 
         mViewPager = (ViewPager) findViewById(R.id.container); 
         mViewPager.setAdapter(mSectionsPagerAdapter); 
    
         TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs); 
         tabLayout.setupWithViewPager(mViewPager); 
    
         fragment_all = new Contact(); 
         fragment_alumni = new Contact(); 
         fragment_official = new Contact(); 
    
         dbhandler = new DBHandler(this); 
         ca_all = new MemberListAdapter(this); 
         ca_alu = new MemberListAdapter(this); 
         ca_off = new MemberListAdapter(this); 
    
         ca_all.ID = 1; 
         ca_alu.ID = 3; 
         ca_off.ID = 2; 
    
         fragment_all.setListAdapter(ca_all); 
         fragment_alumni.setListAdapter(ca_alu); 
         fragment_official.setListAdapter(ca_off); 
    
         updateAdapters();    
        } 
    
        @Override 
        protected void onDestroy() { 
         super.onDestroy(); 
         dbhandler.close(); 
        } 
    
        @Override 
        public boolean onCreateOptionsMenu(Menu menu) { 
         // Inflate the menu; this adds items to the action bar if it is present. 
         getMenuInflater().inflate(R.menu.menu_activity_main, menu); 
         return true; 
        } 
    
        @Override 
        public boolean onOptionsItemSelected(MenuItem item) { 
         // ...unimportant code 
        } 
    
    
        /** 
        * A {@link FragmentPagerAdapter} that returns a fragment corresponding to 
        * one of the sections/tabs/pages. 
        */ 
        public class SectionsPagerAdapter extends FragmentPagerAdapter { 
    
         public SectionsPagerAdapter(FragmentManager fm) { 
          super(fm); 
         } 
    
         @Override 
         public Fragment getItem(int position) { 
    
          if (position == 0){ 
           return fragment_all; 
          }else if (position == 1){ 
           return fragment_official; 
          }else{ 
           return fragment_alumni; 
          } 
         } 
    
         @Override 
         public int getCount() { 
          // Show 3 total pages. 
          return 3; 
         } 
    
         @Override 
         public CharSequence getPageTitle(int position) { 
          switch (position) { 
           case 0: 
            return getResources().getString(R.string.title_section1); 
    
           case 1: 
            return getResources().getString(R.string.title_section2); 
           case 2: 
            return getResources().getString(R.string.title_section3); 
          } 
          return null; 
         } 
        } 
    
        private void updateAdapters() { 
         ca_all.changeCursor(dbhandler.query()); 
         ca_alu.changeCursor(dbhandler.query()); 
         ca_off.changeCursor(dbhandler.query()); 
        } 
    } 
    

    layout_main:

    <?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/apk/res-auto" 
        xmlns:tools="http://schemas.android.com/tools" 
        android:id="@+id/main_content" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:fitsSystemWindows="true" 
        android:background="@color/colorSecond"> 
    
        <android.support.design.widget.AppBarLayout 
         android:id="@+id/appbar" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:paddingTop="@dimen/appbar_padding_top" 
         android:theme="@style/AppTheme.AppBarOverlay"> 
    
         <android.support.v7.widget.Toolbar 
          android:id="@+id/toolbar" 
          android:layout_width="match_parent" 
          android:layout_height="?attr/actionBarSize" 
          android:background="@color/colorContrast" 
          app:layout_scrollFlags="scroll|enterAlways" 
          app:popupTheme="@style/AppTheme.PopupOverlay"> 
    
         </android.support.v7.widget.Toolbar> 
    
         <android.support.design.widget.TabLayout 
          android:id="@+id/tabs" 
          app:tabMaxWidth="0dp" 
          app:tabGravity="fill" 
          app:tabMode="fixed" 
          android:background="@color/colorContrast" 
          android:fillViewport="false" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" /> 
    
        </android.support.design.widget.AppBarLayout> 
    
        <android.support.v4.view.ViewPager 
         android:id="@+id/container" 
         android:layout_weight="1" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
    
        <android.support.design.widget.FloatingActionButton 
         android:id="@+id/fab" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="end|bottom" 
         android:layout_margin="@dimen/fab_margin" 
         android:src="@drawable/updatedb" /> 
    
    </android.support.design.widget.CoordinatorLayout> 
    

    ListFragment Contact.java:

    public class Contact extends ListFragment { 
        private CursorAdapter ca; 
        private MemberListAdapter mla; 
    
        @Override 
        public void onListItemClick(ListView l, View v, int position, long id){ 
         mla = (MemberListAdapter) getListAdapter(); 
         String myid = Integer.toString(mla.ID); 
    
         Log.i("Position: ", Integer.toString(position)); 
         Log.i("ID: ", myid); 
        } 
    
        public Contact(){ 
         super(); 
    
        } 
    
        @Override 
        public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
         super.onActivityCreated(savedInstanceState); 
        } 
    } 
    

    CursorAdapter MemberListAdapter.java:

    public class MemberListAdapter extends CursorAdapter { 
        private LayoutInflater inflator; 
        public int ID; 
    
        public MemberListAdapter(Context context) { 
         super(context, null, 0); 
         inflator = LayoutInflater.from(context); 
        } 
    
        @Override 
        public int getViewTypeCount() { 
         return 3; 
        } 
    
        @Override 
        public void bindView(View view, Context context, Cursor cursor) { 
    
         int ciName = cursor.getColumnIndex(DBHandler.USERS_NAME); 
         int ciSurname = cursor.getColumnIndex(DBHandler.USERS_SURNAME); 
         int ciMail = cursor.getColumnIndex(DBHandler.USERS_MAIL); 
    
         String mail = cursor.getString(ciMail); 
         String name = cursor.getString(ciName); 
         String surname = cursor.getString(ciSurname); 
    
         ImageView image = (ImageView) view.findViewById(R.id.icon); 
         TextView textview1 = (TextView) view.findViewById(R.id.text1); 
         TextView textview2 = (TextView) view.findViewById(R.id.text2); 
    
         image.setImageResource(R.drawable.ksatlogo); 
         textview1.setText(name + ", " + surname); 
         textview2.setText(mail); 
        } 
    
        @Override 
        public View newView(Context context, Cursor cursor, ViewGroup parent) { 
         return inflator.inflate(R.layout.icon_text_text, null); 
        } 
    } 
    
  • +1

    エラーログを表示できますか? –

    +0

    実際には存在しません。単にロードされません。 – spaceengineer

    +0

    アクティビティのonCreateメソッドとonDestroyメソッドにデバッグログメッセージを追加します。あなたはデバイスを回転させるとどのような出力が見えますか?それはあなたの活動の生涯に関するあなたの前提に合っていますか? –

    答えて

    0

    マニフェストのアクティビティにandroid:configChanges = "orientation"を追加してみてください。

    関連する問題