2013-09-16 19 views
0

我想在Java中使用共享首選項來存儲一個變量,以便任何時候我運行該程序它將保留其計數。我只是不知道如何使用它。我是否需要創建共享首選項類,或者我可以使用它,例如我的代碼如下所示。在Java中使用共享首選項來存儲一個int變量

if(action.equals ("insert") 
{ 
int booking_id = (initially be zero); 
booking_id += 1; 
// I want booking Id to retain its value and not become zero the next time I run it. 

} 
+0

存儲你的文件或數據庫bookindId。 – Marcelo

+0

@tieTY哇謝謝你的鏈接..從來不知道有一個像'java.util.prefs'這樣的軟件包:) – exexzian

回答

0

我假設您的意思是共享首選項在Android中可用。

你將要問的SharedPreferences爲booking_id值:如果你想這樣做

SharedPreferences pref = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE); 
int booking_id = pref.getInt("booking_id", 0); // 0 is the default value if the preferences do not find the "booking_id"