XML : How to replace deprecated code in class derived from PreferenceActivity? [duplicate]

This question already has an answer here:

I have the following class in my android code:

  public class UserSettingActivity extends PreferenceActivity{        private Preference myPreference;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          addPreferencesFromResource(R.xml.preferences);          myPreference = findPreference("reset");          myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {              public boolean onPreferenceClick(Preference arg0) {                  AlertDialog.Builder alertDialog = new AlertDialog.Builder(UserSettingActivity.this);                  alertDialog.setMessage("Are you sure to delete the database?");                  alertDialog.setCancelable(true);                  alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {                      public void onClick(DialogInterface dialog, int which) {                          final DBAdapter db = new DBAdapter(getApplicationContext());                          db.open();                          db.resetDatabase();                        } });                  alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {                      public void onClick(DialogInterface dialog, int which) {                          dialog.cancel();                      } });                  alertDialog.show();                  return false;              }          });        }    }    

with the associated xml file as follows:

  <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">      <EditTextPreference android:title="Your Name"          android:key="username"          android:summary="Please provide your username"></EditTextPreference>      <CheckBoxPreference android:title="Application Updates"          android:defaultValue="false"          android:summary="This option if selected will allow the application to check for latest versions."          android:key="applicationUpdates" />        <Preference          android:key="reset"          android:title="Reset database"          android:summary="This will remove every entry in the database"           />  </PreferenceScreen>    

in which the methods addPreferencesFromResource and findPreference are marked as deprecated. I found a hint to follow the example given here. Does this mean, I have to completely rewrite my java class and the xml layout file as well? Or can I reuse the xml layout file? Or do I neeed to create another xml layout file? I see two of them in the example...

Also, I do not see any text in the xml layoutfiles in the example referencing the text fragmented_preferences. I am utterly confused! I cannot possible begin to translate my 'old' code to the 'new' code!

How to fix this problem, i.e. to replace the deprecated methods in my code?

No comments:

Post a Comment