2012-01-21 41 views
0

我正在構建一個應用程序&這是我的應用程序的設置頁面(活動視圖)是this
我用一個Toggle Button & Spinner活動微調和切換按鈕

當切換按鈕被選中時,該應用程序的所有其他用戶都可以看到用戶配置文件&未選中時處於隱藏狀態。

從Spinner選項中,用戶可以通過選擇距離來搜索他/她周圍的其他用戶。

我的問題是什麼 - >當應用程序正在運行這些設置工作,但是當我重新啓動我的應用程序用戶的以前的設置迷路(即,這不是持久&再次我必須做這些設置)...如何我可以從這個問題中解脫出來。我代碼是 - >

public class Settings extends Activity implements OnCheckedChangeListener, OnItemSelectedListener { 
ToggleButton tgb; 
Spinner redai; 

final String[] dis_option= new String[]{"1 mile","5 miles","10 miles","All"}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.settings); 

    //Toggle Button 
    tgb = (ToggleButton) findViewById(R.id.settings_visibility); 
    tgb.setOnCheckedChangeListener(this); 

    //Spinner 
    redai=(Spinner)findViewById(R.id.settings_redai); 
    ArrayAdapter<String> sel_dis=new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, dis_option); 
    sel_dis.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    redai.setAdapter(sel_dis); 
    redai.setOnItemSelectedListener(this); 

} 

@Override 
public void onItemSelected(AdapterView<?> parent, View v, int position, 
     long id) { 
    // TODO Auto-generated method stub 
    String temp_nearBy=parent.getItemAtPosition(position).toString(); 

    /** preference that write the selected distance for further references in other Activities */ 
    SharedPreferences nearByDistanceWrite = getSharedPreferences("nearBy", MODE_WORLD_READABLE); 
    SharedPreferences.Editor edit = nearByDistanceWrite.edit(); 
    edit.putString("upToDistance", temp_nearBy); 
    edit.commit(); 
    Log.i("Settings_Spinner", "onItemSelected_pref_edited..."+temp_nearBy); 
} 

@Override 
public void onNothingSelected(AdapterView<?> arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
    // TODO Auto-generated method stub 
    if (!isChecked) { 
     buttonView.getContext(); 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setMessage("Are you sure you want to Hide Your Profile?") 
       .setCancelable(false) 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 


        //here, I pass Visibility as '0' which means profile of fan is Hidden & not seen to other fans! 
         setVisibility(0);//Method which set Connection with sever & set the visibility of user profile accordingly 

         /** Here you have to write code to set device token as '0' to hide the Profile of Fan! */ 
         final ProgressDialog pg= ProgressDialog.show(Settings.this, "Progress...", "please wait updating settings!",true); 
         Toast.makeText(Settings.this,"Your Profile Set Hidden Successfully!", Toast.LENGTH_LONG).show(); 
        } 
       }) 
       .setNegativeButton("No", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         //setting toggle button to unchecked 
         ToggleButton tg=(ToggleButton)findViewById(R.id.settings_visibility);//buttonView.setChecked(false); 
         tg.setChecked(true); 

         dialog.cancel(); 

        } 
       }); 
     AlertDialog alert = builder.create(); 
     alert.show(); 
    } else { 
     //here, I pass Visibility as '1' which means profile of fan is visible to other fans 
     setVisibility(1); 
     Toast.makeText(Settings.this,"Your Profile get Visible Successfully!", Toast.LENGTH_SHORT).show(); 
    } 
} 


@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 


    Log.i("Settings_onResume", "onresume_after(new)..."+nearBy); 

} 

/** Method that set connection with server & Set the visibility of Fan Profile to either Visible(1) or Hidden(0) 
* @param visibility : The int value which indicate to set profile of a Fan to be Visible or Hidden. */ 
private void setVisibility(int visibility) { 
//...more code ... 

} 

} 

我怎樣才能得到這些Settings(舊的設置還是我以前的設置),當我重新啓動我的應用程序... ??? &

我怎樣才能使Spinner視圖更醒目&好看(系統微調並不好看)???

高興pointerscode snippets來解決這個問題!

回答

0

如果這是一個應用程序,您應該使用您的SharedPreferences返回這些值。這正是他們應該使用的。訪問與此特定應用相關的數據。

+0

我使用SharedPreferences,但問題是,當我重新啓動我的應用程序的默認設置替換我以前的設置。爲前。如果我設置我的個人資料隱藏和距離5英里,然後當我重新啓動我的應用程序這些設置會丟失&再次我的個人資料設置爲可見&距離1英里?這是我面臨的問題...任何解決方案或代碼請 –

+0

這是一個問題,你的代碼如何寫入值,解決這個問題,然後你就可以解決問題。除非用戶在設置中輸入它,否則您不應該對首選項進行任何操作。如果你是,你不遵循SharedPreferences的標準配置。 – JoxTraex

+0

我新來的android。我沒有得到如何設置切換按鈕和微調到先前的設置可能我有任何示例代碼或教程鏈接,它指導我解決這個問題@jox –