共享首选项是存储其先前的值而不是重置

时间:2017-11-18 16:26:10

标签: java android

这里我使用共享首选项来保存两个布局的宽度值,因为我没有在coin_shortage()方法中获取这些值。在show_coin()方法中,通过改变'tot'值我应该得到不同的宽度值。但问题是它没有改变,虽然我试图在完成后删除首选项。我还在清单中使用了android:allowBackup =“false”,但它以某种方式存储了它以前的值并显示出来。这里是活动java文件。

public class TabActivity_3 extends Activity {
    private LinearLayout l, l1, l2;
    private int layHeight, layWidth, layHeight1, layWidth1, existingCoin;
    SharedPreferences sp1,sp2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_points);

        sp1 = getSharedPreferences("totalPrefs", Activity.MODE_PRIVATE);
        sp2 = getSharedPreferences("existingPrefs", Activity.MODE_PRIVATE);


        if(layWidth>0)
        {
            Toast.makeText(getApplicationContext(), "width:" + layWidth,
                    Toast.LENGTH_SHORT).show();
        }
        if(layWidth1>0)
        {
            Toast.makeText(getApplicationContext(), "width1:" + layWidth1,
                    Toast.LENGTH_SHORT).show();
        }



        layWidth = sp1.getInt("total", -1);
        layWidth1 = sp2.getInt("existing", -1);


        sp1.edit().remove("totalPrefs");
        sp1.edit().commit();
        sp1.edit().clear();

        sp2.edit().remove("existingPrefs");
        sp2.edit().commit();
        sp2.edit().clear();



        l = (LinearLayout) findViewById(R.id.linear3);
        l1 = (LinearLayout) findViewById(R.id.linear4);
        l2 = (LinearLayout) findViewById(R.id.linear5);

        ViewTreeObserver observer = l.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                layHeight = l.getHeight();
                layWidth = l.getWidth();
                //Toast.makeText(getActivity,""+a+" "+b,3000).show();


                //init()
                SharedPreferences.Editor editor = sp1.edit();
                editor.putInt("total", layWidth);
                editor.commit();


                l.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);


            }
        });


        show_coin();

        //Toast.makeText(getApplicationContext(), "height:" + layHeight + "width:" + layWidth,
          //      Toast.LENGTH_SHORT).show();

        //Toast.makeText(getApplicationContext(), "height1:" + layHeight1 + "width1:" + layWidth1,
            //    Toast.LENGTH_SHORT).show();


    }


    protected void init() {
        layHeight = l.getHeight();
        layWidth = l.getWidth();
        //Toast.makeText(getActivity,""+a+" "+b,3000).show();
        Toast.makeText(getApplicationContext(), "height:" + layHeight + "width:" + layWidth,
                Toast.LENGTH_SHORT).show();
    }


    private void show_coin() {
        ImageView img_coin1;
        int tot = 5;
        for (int i = 0; i < tot; i++) {
            img_coin1 = new ImageView(getApplicationContext());
            img_coin1.setImageResource(R.drawable.dollar);
            int width = 70;
            int height = 70;

            LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width, height);
            img_coin1.setLayoutParams(parms);
            Log.i("i", i + "");
            l1.addView(img_coin1);

            ViewTreeObserver observer = l1.getViewTreeObserver();
            observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override
                public void onGlobalLayout() {
                    // TODO Auto-generated method stub
                    layHeight1 = l1.getHeight();
                    layWidth1 = l1.getWidth();
                    //Toast.makeText(getActivity,""+a+" "+b,3000).show();

                    SharedPreferences.Editor editor = sp2.edit();
                    editor.putInt("existing", layWidth1);
                    editor.commit();


                    l1.getViewTreeObserver().removeGlobalOnLayoutListener(
                            this);

                    coin_shortage();
                }


            });


        }

    }

    private void coin_shortage() {

        Toast.makeText(getApplicationContext(), "height:" + layHeight + "width:" + layWidth,
                Toast.LENGTH_SHORT).show();
        Toast.makeText(getApplicationContext(), "height1:" + layHeight1 + "width1:" + layWidth1,
                Toast.LENGTH_SHORT).show();

        existingCoin=layWidth/layWidth1;
        ImageView img_coin2;
        //int tot2 = 3;
        for (int j = 0; j < existingCoin; j++) {
            img_coin2 = new ImageView(getApplicationContext());
            img_coin2.setImageResource(R.drawable.dollar);
            int width = 70;
            int height = 70;

            LinearLayout.LayoutParams parms2 = new LinearLayout.LayoutParams(width, height);
            img_coin2.setLayoutParams(parms2);
            //Log.i("i", i + "");
            l2.addView(img_coin2);


        }





    }
}

1 个答案:

答案 0 :(得分:0)

你应该打电话给coin_shortage();在你的showCoin()中 - &gt; onGlobalLayout方法。

因为onGlobalLayout是在showCoin之后执行的。

<强>更新

更新您的show coint方法:

private void show_coin() {
    ImageView img_coin1;
    int tot = 5;
    for (int i = 0; i < tot; i++) {
        img_coin1 = new ImageView(getApplicationContext());
        img_coin1.setImageResource(R.drawable.dollar);
        int width = 70;
        int height = 70;



LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(width, height);
    img_coin1.setLayoutParams(parms);
    Log.i("i", i + "");
    l1.addView(img_coin1);
}

layHeight1 = l1.getMeasuredHeight();
            layWidth1 = l1.getMeasuredWidth();
            //Toast.makeText(getActivity,""+a+" "+b,3000).show();

            SharedPreferences.Editor editor = sp2.edit();
            editor.putInt("existing", layWidth1);
            editor.commit();

            coin_shortage();

}