2017-11-18 6 views
-1

ここでは、coin_shortage()メソッドで値を取得していないため、2つのレイアウトの幅の値を保存するために共有設定を使用しました。 show_coin()メソッドでは、 'tot'値を変更することによって、私は異なる幅の値を取得するはずです。しかし、問題は、私はすべての後に、設定を削除しようとしても変更されません。また、マニフェストではアンドロイド: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); 


     } 





    } 
} 
+0

'(layWidth> 0)' layWidthがまだ初期化されていない場合。いくつかの行を初期化するだけです。 – greenapps

+0

実際には、この問題に干渉していません。私は以前にそれらをしました。 – Israt

答えて

0

あなたはcoin_shortage()を呼び出す必要があります。あなたのshowCoin() - > onGlobalLayoutメソッドの中で。

onGlobalLayoutはshowCoinの後に実行されるためです。

更新

更新あなたのショーの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(); 

}

+0

適用されていますが、正しい値が得られません – Israt

+0

質問でコードを更新できますか? – diegoveloper

+0

コードを更新しました – Israt

関連する問題