2010-11-02 15 views
14

मैं स्क्रीन से वरीयता हटाने की कोशिश कर रहा हूं, इसलिए यदि उपयोगकर्ता 2.2 से अधिक एसडीके का उपयोग कर रहा है तो यह दिखाई नहीं दे रहा है। मुझे कई जवाब मिल गए हैं कि getPreferenceScreen()। RemovePreference (thePreference) काम करेगा, लेकिन जब भी मैं इसे कोशिश करता हूं तो मुझे FALSE लौटा रहा है। क्या मैं इसे गलत जगह पर उपयोग कर रहा हूं? मेरे कोड से कोई सुराग?स्क्रीन से एंड्रॉइड वरीयताओं को कैसे हटाएं

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { 

private static final String POLLING_PREFERENCE = "update_frequency_list"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Load the preferences from an XML resource 
    addPreferencesFromResource(R.xml.preferences); 

    // Get a reference to the preferences 
    mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE); 

    //If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead) 
    if(Build.VERSION.SDK_INT > 7) { 
     getPreferenceScreen().removePreference(mPollPref); 
    } 
} 
.... 
} 

उत्तर

45

ठीक है मेरे मामले में यह मुद्दा था कि मेरी वरीयताओं को श्रेणियों में विभाजित किया गया था और जब वरीयताओं को वर्गीकृत किया गया है तो आप बस ".removePreference" नहीं कर सकते जैसे हम करने की कोशिश कर रहे थे। मैं पहली वरीयता मैं दूर करने के लिए और उसके बाद तो जैसे उस वर्ग से है कि वरीयता को दूर करना चाहता था युक्त श्रेणी को संदर्भित करने के ..

public class Preferences extends PreferenceActivity implements OnSharedPreferenceChangeListener { 

private static final String POLLING_PREFERENCE = "update_frequency_list"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

// Load the preferences from an XML resource 
addPreferencesFromResource(R.xml.preferences); 

// Get a reference to the preferences 
mPollPref = getPreferenceScreen().findPreference(POLLING_PREFERENCE); 

//If the SDK is 2.2 or greater do not display polling preferences (using C2DM instead) 
if(Build.VERSION.SDK_INT > 7) { 
    PreferenceCategory notificationsCategory = (PreferenceCategory) findPreference("notifications_category"); 
    notificationsCategory.removePreference(mPollPref); 
} 
} 
.... 
} 

मुझे लगता है यह एक ही साथ कई प्राथमिकताएं होती हैं करने की क्षमता के साथ कुछ है था कुंजी (सही एक को हटाने के लिए)। वैसे भी मेरे साथ चिपके रहने के लिए व्लादिमीर धन्यवाद। निश्चित रूप से आपके बिना जवाब नहीं मिला होगा।

+1

धन्यवाद इसने मेरे लिए समस्या हल की! इसकी वरीयता श्रेणी से वरीयता को हटाकर ठीक काम करता है! – stealthcopter

+1

जब तक मैं इसे नहीं देखता, तब तक मेरे सिर को खरोंच कर रहा था, कोई आश्चर्य नहीं कि मैं वरीयता श्रेणी जोड़ने के बाद सब कुछ काम करने में विफल रहता है! उन्हें कहीं कहीं दस्तावेज करना चाहिए था .. – Bruce

1

कोड सही लगता है। आपको बता दूँ कि निम्नलिखित कोड काम करता है, बस इसे परीक्षण किया: SDK के लिए आपका चेक

package com.lid.ps.screens.edit_activity; 

import android.app.AlertDialog; 
import android.app.DatePickerDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.preference.*; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.*; 
import com.lid.ps.bean.AbstractData; 
import com.lid.ps.bean.Action; 
import com.lid.ps.bean.ActionFactory; 
import com.lid.ps.bean.CommonAlertDialog; 
import com.lid.ps.bean.utils.DateTools; 
import com.lid.ps.model.Activity; 
import com.lid.ps.model.ActivityImpl; 
import com.lid.ps.screens.R; 

import java.util.Calendar; 

/** 
* User: Vladimir Ivanov 
* Date: 04.07.2010 
* Time: 9:58:17 
*/ 
public class EditActivityScreen extends PreferenceActivity 
    implements DatePickerDialog.OnDateSetListener, AbstractData<Activity> { 

private static final int MENU_DELETE_ACTIVITY = 0; 

private boolean create = true; 

public boolean isCreate() { 
    return create; 
} 

private Activity dataContainer; 

private EditTextPreference activityNamePref; 
private EditTextPreference activityDescPref; 
private Preference startDatePref; 
private CheckBoxPreference hiddenPref; 

private int year; 
private int monthOfYear; 
private int dayOfMonth; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    addPreferencesFromResource(R.xml.activity_prefs); 
    //this.setContentView(R.layout.edit_screen); 

    activityNamePref = (EditTextPreference) findPreference("name"); 

    activityDescPref = (EditTextPreference) findPreference("desc");   

    // We have to do this to get the save/cancel buttons to highlight on 
    // their own. 
    getListView().setItemsCanFocus(true); 

    // Grab the content view so we can modify it. 
    FrameLayout content = (FrameLayout) getWindow().getDecorView() 
      .findViewById(android.R.id.content); 

    // Get the main ListView and remove it from the content view. 
    ListView lv = getListView(); 
    content.removeView(lv); 

    // Create the new LinearLayout that will become the content view and 
    // make it vertical. 
    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 

    // Have the ListView expand to fill the screen minus the save/cancel 
    // buttons. 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 
    lp.weight = 1; 
    ll.addView(lv, lp); 

    // Inflate the buttons onto the LinearLayout. 
    View v = LayoutInflater.from(this).inflate(
      R.layout.edit_screen, ll); 

    startDatePref = findPreference("time"); 

    hiddenPref = (CheckBoxPreference) findPreference("hidden"); 

    // Initialize buttons 
    Button cancelButton = (Button) v.findViewById(R.id.cancel_activity); 
    Button saveButton = (Button) v.findViewById(R.id.save_activity); 

    cancelButton.setOnClickListener(new CancelOnClickListener()); 
    saveButton.setOnClickListener(new SaveOnClickListener()); 

    setContentView(ll); 

    // if edit mode... 
    if (getIntent() != null && getIntent().getExtras() != null) { 
     // some stuff 
} 

@Override 
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, 
            Preference preference) { 
    if (preference == startDatePref) { 
     new DatePickerDialog(this, this, year, monthOfYear, dayOfMonth).show(); 
    } 

    return super.onPreferenceTreeClick(preferenceScreen, preference); 
} 

private void popAlarmSetToast(Context context) { 
    String toastText;  
    //... 
    final Toast toast = Toast.makeText(context, toastText, Toast.LENGTH_LONG); 
    toast.show(); 
} 

/** 
* updates the preference summary. 
* 
* @param datePicker date picker 
* @param i   year 
* @param i1   month 
* @param i2   day 
*/ 
@Override 
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { 
    year = i; 
    monthOfYear = i1; 
    dayOfMonth = i2; 
    updateDate(); 
} 

private void updateDate() { 
    startDatePref.setSummary(
      new StringBuilder() 
        // Month is 0 based so add 1 
        .append(year).append("-") 
        .append(monthOfYear + 1).append("-") 
        .append(dayOfMonth).append(" ")); 
} 


private class CancelOnClickListener implements View.OnClickListener { 

    public void onClick(View view) { 
     // go back to the previous page 
     final Action action = ActionFactory.createAction("back_edit_activity"); 
     if (action != null) { 
      action.doAction(EditActivityScreen.this); 
     } 
    } 
} 

private class SaveOnClickListener implements View.OnClickListener { 

    public void onClick(View view) { 

     // validation 
     if (activityNamePref.getSummary() == null || activityNamePref.getSummary().length() == 0) { 
      AlertDialog dialog = new AlertDialog.Builder(EditActivityScreen.this).create(); 
      dialog.setTitle(R.string.validation_failed); 
      dialog.setMessage(EditActivityScreen.this.getResources().getString(R.string.activity_create_fail)); 
      dialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialogInterface, int i) { 
       } 
      }); 
      dialog.show(); 
     } else { 
      final Action action = ActionFactory.createAction("save_edit_activity"); 
      if (action != null) { 
       action.doAction(EditActivityScreen.this); 
      } 
      popAlarmSetToast(EditActivityScreen.this); 
     } 
    } 
} 

}

के बारे में क्या निर्माण? क्या कोड वास्तव में इस स्थिति में जाता है?

और कृपया, अपनी वरीयता का नाम देखें। हो सकता है कि आपको findPreference विधि के साथ नहीं मिला हो।

+0

आपकी त्वरित प्रतिक्रिया के लिए धन्यवाद। हां, पोस्ट करने से पहले मुझे वास्तव में हटाने का परिणाम प्रिंट करने वाला कोड था (जो एक बुलियन लौटाता है) और यह मुझे झूठा दे रहा है। मैंने किसी भी चर को खत्म करने के लिए if-statement को हटा दिया और मैंने दोहराया और उसी परिणाम के साथ अलग-अलग प्राथमिकताओं को आजमाया। किसी भी मौके पर मैं आपकी पूरी कक्षा देख सकता हूं जिसका आपने परीक्षण किया था? मुझे नहीं पता कि यह मेरे लिए क्यों काम नहीं कर रहा है। – honeal

+0

उत्तर संपादित किया गया। –

+0

धन्यवाद दोस्त, मदद की टन। आपके एक्सएमएल को पहले देखे बिना वास्तव में क्या गलत हो रहा था, यह जानने का कोई तरीका नहीं था। एक बार फिर धन्यवाद। यदि मैं 15 वर्ष से ऊपर था तो मैं आपके उत्तर को बढ़ावा दूंगा। – honeal

4
//remove all preferences 
    mPollPref = getPreferenceScreen(); 
    mPollPref.removeAll(); 

//Change language 
    setApplicationLanguage(); 

//and rebuild 
    addPreferencesFromResource(R.xml.preferences); 
संबंधित मुद्दे