2

我正在尝试在滑动视图片段(名为 BROWSE)中添加标签片段。当 BROWSE 被访问时,与远程服务器的连接完成,数据被检索并显示在选项卡内的 gridview 中,当单击不同的选项卡时,建立新连接,并且 gridview 填充新数据。

问题是我无法保存选项卡的状态,我不想每次都建立连接,只有在第一次单击选项卡时才建立与数据库的连接,如果再次单击选项卡我想要保留状态的gridview。

这是 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);
                }
        }

这是与 gridview 一起创建选项卡的 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 变量被传递给onViewCreated方法,该方法在onCreateView之后立即调用。

您应该尝试仅在 onCreateView(gridView、mTabHost 等)中膨胀您的布局并初始化您的视图字段,并在 onViewCreated 中恢复状态(或设置默认状态)

于 2013-02-04T12:56:01.710 回答