2012-01-12 10 views
0
コードのこの部分で
public class SettingsActivity extends PreferenceActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    /* Some initializations */ 
    LinearLayout layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 

    ListView listView = new ListView(this); 
    listView.setId(android.R.id.list); 
    listView.setLayoutParams(new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT, 1)); 
    layout.addView(listView); 

    this.setContentView(layout); 
    /* Preferences time! (we build the preferences) */ 
    Preference version = getPreference("My School Manager", "Version 2.0", 
      null); 
    Preference author = getPreference("Author", "Simone Casagranda", null); 
    Preference marketLink = getPreference("Android market", 
      "View all my apps :)", 
      new Intent(Intent.ACTION_VIEW, Uri 
        .parse("http://market.android.com/details?id=" 
          + "it.trento.alchemiasoft.casagranda.simone"))); 

    CheckBoxPreference check = new CheckBoxPreference(this); 
    check.setTitle("Checkbox"); 
    check.setSummary("Example of checkbox"); 

    DialogPreference license = new MyDialogPreference(this, "License", 
      "This is the license for...bla bla"); 

    /* Now we add the preferences to the preference screen */ 
    PreferenceScreen preferenceScreen = this.getPreferenceManager() 
      .createPreferenceScreen(this); 
    addPreferenceCategory(preferenceScreen, "Preferences Tutorial", 
      version, author, marketLink, check, license); 
    this.setPreferenceScreen(preferenceScreen); 
} 

private boolean addPreferenceCategory(PreferenceScreen preferenceScreen, 
     String titleCategory, Preference... preferences) { 
    boolean addPreference = false; 
    for (Preference preference : preferences) { 
     if (preference != null) 
      addPreference = true; 
    } 
    if (addPreference) { 
     PreferenceCategory preferenceCategory = new PreferenceCategory(this); 
     preferenceCategory.setTitle(titleCategory); 
     preferenceScreen.addPreference(preferenceCategory); 
     for (Preference preference : preferences) { 
      if (preference != null) 
       preferenceCategory.addPreference(preference); 
     } 
     return true; 
    } else 
     return false; 
} 

private Preference getPreference(String title, String summary, Intent intent) { 
    Preference pref = new Preference(this); 
    pref.setTitle(title); 
    pref.setSummary(summary); 
    if (intent != null) 
     pref.setIntent(intent); 
    return pref; 
} 

public class MyDialogPreference extends DialogPreference { 
    public MyDialogPreference(Context context, String title, String text) { 
     super(context, null); 
     this.setTitle(title); 
     this.setDialogMessage(text); 
    } 
} 

}はPreferenceActivity

の理解、好ましくは、リストビューに記載されています。アダプターは必要ないのですか?私は、データがリストビューに供給されているのを見ません。この設定画面はどうですか?環境設定には必要ですか?

答えて

1

PreferenceActivityは、すべてプリファレンスを単独で処理します。アダプターはPreferenceActivityに組み込まれているので、気にする必要はありません。また、プログラムでプリファレンスを作成する代わりに、XMLを使用して同じことを行うこともできます。 this tutorialを確認してください。

嗜好アクティビティには多くの苦労があります。ダイアログの表示、設定の状態の変更、保存など、すべてのUIアクションを配線するためのコードを記述することを想像してみてください。嗜好活動はその痛みをあなたから遠ざけます。

+0

OK、上から、リストビューにフィードするデータソースは何ですか?それはPreferenceScreen、PreferenceCategory、Preference ...などですか? – lilzz

+0

すべて3 :)、各アイテムは、異なる順序でレンダリングされます。 –

関連する問題