2016-02-12 74 views
0

我有兩個活動。和一個稱爲計數器的靜態整數。靜態值Android Studio

所以,如果我在活動'A'然後counter = counter + 1按下按鈕。

下面是活動代碼:

public static int counter = 0; 
cmdOk.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     counter = counter + 1; 
     if (counter == 5) 
     { 
      tagihan.txtShip1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtNilai1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtSupir1.setTextColor(Color.parseColor("#000000")); 
     } 
} 

這裏,它是從活動B:

cmdSuccess.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     a.counter = a.counter + 1; 
     if (a.counter == 5) 
     { 
      tagihan.txtShip1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtNilai1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtSupir1.setTextColor(Color.parseColor("#000000")); 
     } 
} 

我的問題是,當我試圖從活動按一個按鈕3次它完美地工作。所以現在的值是3。

但是,當我嘗試按下活動b的按鈕,價值將重新啓動爲0.其實我沒有銷燬活動a。

所以我想要的是價值會持續不斷,即使我按活動a或b。

任何想法?

編輯:

我編輯的代碼。塔吉汗的活動就是我試圖完成的事情。所以當計數器是5時,tagihan活動正在改變。

+0

在活動中共享靜態動態變量不是首選,因爲您幾乎無法控制活動生命週期。你試圖完成什麼問題? –

+0

你必須顯示使用這兩種活動的完整來源。 –

+0

如何將活動a的實例傳遞給b。你可以請秀嗎? – KDeogharkar

回答

1

不使用靜態數據,這是一種糟糕的方法,不是一種常見的OOP開發方式,而是嘗試在活動之間傳遞數據...

ACT1

Intent intent = new Intent(activity2.this, activity1.class); 
intent.putExtra("message", message); 
startActivity(intent); 

ACT2:

Bundle bundle = getIntent().getExtras(); 
String message = bundle.getString("message"); 

Android開發網站是給介紹這個: http://developer.android.com/training/basics/firstapp/starting-activity.html

1

你的編輯後,我可以看到你需要一個「全局變量」,可以b e讀/寫的所有活動:

解決方案: 所有活動都嵌入到應用程序,所以如果你HABE在應用領域/成員可以訪問到它們與stadard 的setter /吸氣

您需要:

定義應用程序

public class MyApplication extends Application { 

    private int counterVariable; 

    public int counterVariable() { 
     return this.counterVariable; 
    } 

    public void setCounterVariable(int someVariable) { 
     this.counterVariable = someVariable; 
    } 
} 

添加到應用程序清單:

在活動
<application 
    android:name="MyApplication" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name"> 

然後獲取和設置變量,像這樣:

// cast to Application and call the setter 
((MyApplication) this.getApplication()).counterVariable(1); 

// cast to Application and call the getter 
int counter = ((MyApplication) this.getApplication()).getCounterVariable(); 
0

請使用下面的代碼:

//廣義避免靜態值保持形式:

public class SPDataHandler { 


private Context mContext; 
private SharedPreferences mPreference; 
public SPDataHandler(Context context) { 
     this.mContext = context; 
     this.mPreference = mContext.getSharedPreferences("SAMPLE_SP", Context.MODE_PRIVATE); 
    } 
    /** 
    * COMMON SETTER FOR INTEGER DATA 
    */ 
    private void setIntegerData(String key, int value) { 
     SharedPreferences.Editor editor = mPreference.edit(); 
     editor.putInt(key, value); 
     editor.commit(); 
    } 
    /** 
    * COMMON GETTER FOR INTEGER SP DATA 
    */ 
    private int getIntegerSpData(String key, int defaultValue) { 
     return this.mPreference.getInt(key, defaultValue); 
    } 


    // Your Getter and Setter 

    public int getmCount() { 
     return this.getIntegerSpData("Count", 1); 
    } 

    public void setmCount(int cont) { 
     this.setIntegerData("Count", cont); 
    } 
} 

// Your Activity A 

SPDataHandler handler = new SPDataHandler(this); 
int count = handler.getmCount(); 
cmdOk.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     count = count + 1; 
     handler.setmCount(count); // Change the logic based on your requirement 
     if (count == 5) 
     { 
      tagihan.txtShip1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtNilai1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtSupir1.setTextColor(Color.parseColor("#000000")); 
     } 
} 


// Your Activity B 
SPDataHandler handler = new SPDataHandler(this); 
int count = handler.getmCount(); 
cmdSuccess.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     count = count + 1; 
     handler.setmCount(count); // Change the logic based on your requirement 
     if (count == 5) 
     { 
      tagihan.txtShip1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtNilai1.setTextColor(Color.parseColor("#000000")); 
      tagihan.txtSupir1.setTextColor(Color.parseColor("#000000")); 
     } 
}