2

スワイプ ビュー フラグメント (BROWSE という名前) 内にタブ フラグメントを配置しようとしています。BROWSE にアクセスすると、リモート サーバーへの接続が確立され、データが取得されてタブ内のグリッドビューに表示されます。別のタブがクリックされると、新しい接続が確立され、グリッドビューに新しいデータが入力されます。

問題は、タブの状態を保存できないことです。毎回接続を確立したくありません。タブが最初にクリックされたときにのみ、データベースへの接続が確立されます。タブが再度クリックされた場合は、状態を保持するグリッドビュー。

これは BROWSE フラグメントの onCreate メソッドです。このメソッドでは、リモート サーバーからデータを取得するためにローダーを初期化しています。

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




            if (t == 0) {
            Log.v("BROWSE","++++++++ Inside onCreate t = 0 ++++++++");

            tweets = Collections.emptyList();
            // This is our REST action.

            Uri twitterSearchUri = new Uri.Builder()
            .scheme("http")
            .authority(ip)
            .path("browse.php")
            .appendQueryParameter("SpecialOffer", "0")
            .build();

                Bundle params = new Bundle();
            //    params.putString("q", "android");
                params.get("salim slem");
                // These are the loader arguments. They are stored in a Bundle because
                // LoaderManager will maintain the state of our Loaders for us and
                // reload the Loader if necessary. This is the whole reason why
                // we have even bothered to implement RESTLoader.
                Bundle args = new Bundle();
                args.putParcelable(ARGS_URI, twitterSearchUri);
                args.putParcelable(ARGS_PARAMS, params);


            //For loading Images    
            //--------------------------------------------------    

                options = new DisplayImageOptions.Builder()
                .showStubImage(R.drawable.stub_image)
                .showImageForEmptyUri(R.drawable.image_for_empty_url)
                .cacheInMemory()
                .cacheOnDisc()
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();


            //--------------------------------------------------    



                // Initialize the Loader.
                getSupportLoaderManager().initLoader(LOADER_TWITTER_SEARCH, args, this);

            }

            if(t == 1) {

                Log.v("BROWSE","++++++++ Inside onCreate  t = 1 ++++++++");

                tweets = Collections.emptyList();
                // This is our REST action.

                Uri twitterSearchUri = new Uri.Builder()
                .scheme("http")
                .authority(ip)
                .path("browse.php")
                .appendQueryParameter("SpecialOffer", "1")
                .build();

                    Bundle params = new Bundle();
                //    params.putString("q", "android");
                    params.get("salim slem");
                    // These are the loader arguments. They are stored in a Bundle because
                    // LoaderManager will maintain the state of our Loaders for us and
                    // reload the Loader if necessary. This is the whole reason why
                    // we have even bothered to implement RESTLoader.
                    Bundle args = new Bundle();
                    args.putParcelable(TAB1_ARGS_URI, twitterSearchUri);
                    args.putParcelable(TAB1_ARGS_PARAMS, params);


                //For loading Images    
                //--------------------------------------------------    

                    options = new DisplayImageOptions.Builder()
                    .showStubImage(R.drawable.stub_image)
                    .showImageForEmptyUri(R.drawable.image_for_empty_url)
                    .cacheInMemory()
                    .cacheOnDisc()
                    .bitmapConfig(Bitmap.Config.RGB_565)
                    .build();

                //--------------------------------------------------    

                    // Initialize the Loader.
                    getSupportLoaderManager().initLoader(TAB1_LOADER_TWITTER_SEARCH, args, this);
                }
        }

これは、グリッドビューとともにタブが作成される onCreateView メソッドです。

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

        Log.v("BROWSE", "Inside OnCreateView");

        View view = inflater.inflate(R.layout.gridview, container, false);

        gridView = (GridView) view.findViewById(R.id.gridViewImage);

        gridView.setAdapter(new ImageTextAdapter(view.getContext(), LOADING));

        // When an item in the gridview is clicked
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                FragmentPosition = 2;
                Log.v("BROWSE: POSITION OF ITEM IN GRIDVIEW","## "+position+" ##");
                DialogFragment newFragment = new BrowseDialogFragment(position);
                newFragment.show(getSupportFragmentManager(), "missiles");

            }
        });

        // creating tabs
        mTabHost = (TabHost) view.findViewById(R.id.testtabhost1); 
        mTabHost.setup();
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(R.id.gridViewImage)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.gridViewImage)); 
        mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(R.id.gridViewImage)); 
        mTabHost.setCurrentTab(0); 



        mTabHost.setOnTabChangedListener(new OnTabChangeListener() {


            @Override
            public void onTabChanged(String tabId) {

            int i = mTabHost.getCurrentTab();
             Log.i("@@@@@@@@ ANN CLICK TAB NUMBER", "------" + i);

             if (i ==0) {
                 Bundle tempBundle = new Bundle();
                 t = 0;
                onCreate(tempBundle);
        }

             if (i ==1) {
                 Bundle tempBundle = new Bundle();     
                 t = 1;
                 onCreate(tempBundle);
                }

              }
            });

        return view;
    }

私の問題は、onCreateView から onCreate に「savedInstanceState」を渡すことができないことだと思います。私の質問は、タブに状態を保存するにはどうすればよいですか? onCreate に指定されたバンドルを使用している可能性がありますが、その方法が見つかりません!

よろしくお願いします!

それが私が話しているUIです:

ここに画像の説明を入力

タブをもう一度押したときの UI は次のとおりです。

ここに画像の説明を入力

4

2 に答える 2

0

その場合はデータベースを使用してみてください。それを理解するのはとても簡単です: チュートリアル

だからあなたがすべきこと:

  1. リモートデータを取得する
  2. SQLite DB に保存します。
  3. データベース内のデータの古さをチェックする Methode を作成します。
  4. たとえば、データが 10 分より古い場合、リモート データを取得してテーブルを更新する必要があります。

これが役立つことを願っています。

よろしくお願いします

サファリ

于 2013-02-04T12:37:39.663 に答える
0

savedInstanceState 変数は、 onCreateViewの直後に呼び出されるonViewCreatedメソッドに渡されます。

レイアウトのみを拡張し、onCreateView (gridView、mTabHost など) でビュー フィールドを初期化し、onViewCreated で状態を復元 (またはデフォルト状態を設定) するようにしてください。

于 2013-02-04T12:56:01.710 に答える