与发行的onSaveInstanceState使用和onRestoreInstanceState的 [英] Issue with use of onSaveInstanceState and onRestoreInstanceState

查看:199
本文介绍了与发行的onSaveInstanceState使用和onRestoreInstanceState的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林用我的购物车活动以下code。当我添加的项目到购物车,它显示了购物车中的物品。但是当我打开另一个活动,回到车它显示为空。然后我发现使用的onSaveInstanceState onRestoreInstanceState ..即使我用这个code将其显示为空。任何人都可以指出什么出了错在这个code。

是林希望利用这个code,当我加入的项目,以车会被保存在的onSaveInstanceState,当我再次打开购物车,然后它会使用onRestoreInstanceState并显示在车中的物品。

 公共类ShoppingCartActivity延伸活动{        ProductAdapter mCartList;
        ExpandableListView expListView;
        清单<串GT; listDataHeader;
        清单<串GT; listDataHeaderPrice;
        清单<串GT; listDataHeaderQty;
        HashMap的<字符串列表<串GT;> listDataChild;
        私人ExpandableListView mExpandableList;
        字符串描述;
        串价格;
        串数量;
        ArrayList的<串GT; myList中=新的ArrayList<串GT;();        @覆盖
        公共无效的onSaveInstanceState(捆绑savedInstanceState){
            savedInstanceState.putString(说明,说明);
            savedInstanceState.putString(价格,价格);
            savedInstanceState.putString(量,数量);
            super.onSaveInstanceState(savedInstanceState);
        }        @覆盖
        公共无效onRestoreInstanceState(捆绑savedInstanceState){
          super.onRestoreInstanceState(savedInstanceState);
          //从savedInstanceState恢复UI状态。
          //这个包也被传递到的onCreate。          说明= savedInstanceState.getString(说明);
          量= savedInstanceState.getString(量);
          价格= savedInstanceState.getString(价格);
        }        @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.cart_activity);如果(savedInstanceState!= NULL){
            Toast.makeText(getApplicationContext(),1,Toast.LENGTH_SHORT)
                    。显示();
        }其他{
            Toast.makeText(getApplicationContext(),2,Toast.LENGTH_SHORT)
                    。显示();
        }            量= getIntent()getStringExtra(量)。
            价格= getIntent()getStringExtra(价格)。
            说明= getIntent()getStringExtra(说明)。            myList.add(介绍);
            myList.add(价);
            myList.add(数量);
            动作条动作条= getActionBar();
            getActionBar()的setIcon(
                    新ColorDrawable(getResources()。的getColor(
                            android.R.color.transparent)));
            getWindow()。setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            //启用返回导航操作栏图标
            actionBar.setDisplayHomeAsUpEnabled(真);            //获取列表视图
            expListView =(ExpandableListView)findViewById(R.id.lvExp);            // preparing列表数据
            prepareListData();            mCartList =新ProductAdapter(这一点,listDataHeader,
                    listDataHeaderPrice,listDataHeaderQty,listDataChild);            //设置列表适配器
            expListView.setAdapter(mCartList);            //列表视图组单击监听器
            expListView.setOnGroupClickListener(新OnGroupClickListener(){                @覆盖
                公共布尔onGroupClick(ExpandableListView父视图V,
                        INT groupPosition,长ID){
                    Toast.makeText(getApplicationContext(),
                            集团已点击+ listDataHeader.get(groupPosition)
                            Toast.LENGTH_SHORT).show();
                    返回false;
                }
            });


解决方案

我觉得onRestoreInstanceState是在onStart后调用和code刷新你的UI是的onCreate。因此,该数据可能是有,但你不要在UI中显示它。试试行之后更新的ListView /适配器价格= savedInstanceState.getString(价格);

Im using the below code in my shopping cart activity. when i added the items to cart, it shows the items in the cart. but when i open another activity and go back to the cart it shows null. then i found use of onSaveInstanceState and onRestoreInstanceState.. even after i use this code it shows null. can anyone point out what have gone wrong in this code.

what im expecting by use of this code is when i added the items to cart it will save in onSaveInstanceState, and when i open the cart again then it will use onRestoreInstanceState and show the items on the cart.

    public class ShoppingCartActivity extends Activity {

        ProductAdapter mCartList;
        ExpandableListView expListView;
        List<String> listDataHeader;
        List<String> listDataHeaderPrice;
        List<String> listDataHeaderQty;
        HashMap<String, List<String>> listDataChild;
        private ExpandableListView mExpandableList;
        String description;
        String price;
        String quantity;
        ArrayList<String> myList = new ArrayList<String>();

        @Override
        public void onSaveInstanceState(Bundle savedInstanceState) {
            savedInstanceState.putString("description", description);
            savedInstanceState.putString("price", price);
            savedInstanceState.putString("quantity", quantity);
            super.onSaveInstanceState(savedInstanceState);
        }

        @Override
        public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);
          // Restore UI state from the savedInstanceState.
          // This bundle has also been passed to onCreate.

          description = savedInstanceState.getString("description");
          quantity = savedInstanceState.getString("quantity");
          price = savedInstanceState.getString("price");
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.cart_activity);

if (savedInstanceState != null) {
            Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_SHORT)
                    .show();
        } else {
            Toast.makeText(getApplicationContext(), "2", Toast.LENGTH_SHORT)
                    .show();
        }

            quantity = getIntent().getStringExtra("quantity");
            price = getIntent().getStringExtra("price");
            description = getIntent().getStringExtra("description");

            myList.add(description);
            myList.add(price);
            myList.add(quantity);


            ActionBar actionBar = getActionBar();
            getActionBar().setIcon(
                    new ColorDrawable(getResources().getColor(
                            android.R.color.transparent)));
            getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            // Enabling Back navigation on Action Bar icon
            actionBar.setDisplayHomeAsUpEnabled(true);

            // get the listview
            expListView = (ExpandableListView) findViewById(R.id.lvExp);

            // preparing list data
            prepareListData();

            mCartList = new ProductAdapter(this, listDataHeader,
                    listDataHeaderPrice, listDataHeaderQty, listDataChild);

            // setting list adapter
            expListView.setAdapter(mCartList);

            // Listview Group click listener
            expListView.setOnGroupClickListener(new OnGroupClickListener() {

                @Override
                public boolean onGroupClick(ExpandableListView parent, View v,
                        int groupPosition, long id) {
                    Toast.makeText(getApplicationContext(),
                            "Group Clicked " + listDataHeader.get(groupPosition),
                            Toast.LENGTH_SHORT).show();
                    return false;
                }
            });

解决方案

I think onRestoreInstanceState is called after onStart and the code for refreshing your UI is in onCreate. So the data is probably there but you don't show it in the UI. Try updating your ListView/Adapter after the line price = savedInstanceState.getString("price");

这篇关于与发行的onSaveInstanceState使用和onRestoreInstanceState的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆