Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:49:51+00:00 2026-06-09T00:49:51+00:00

I am trying to programmatically delete one of the options in a list view

  • 0

I am trying to programmatically delete one of the options in a list view from SharedPreferences. I have included simplified code below.

I have read through a ton of similar questions but it just will not seem to work for me. I must be misunderstanding a key concept. Please help.

I have tried using clear() and apply(). I have also tried adding an a line instead of deleting all without success.

I think I may be misunderstanding the concept of which keys to use and have tried alternates, but nothing seems to work.

my preferences.java

package il.preferences;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;

public class Preferences extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        SharedPreferences preferences = getSharedPreferences(
                "myKey", MODE_PRIVATE);
        preferences.edit().remove("1").commit();
    }
}

my preference.xml

 <?xml version="1.0" encoding="utf-8" standalone="no"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Cat X" >
        <ListPreference
            android:defaultValue="1"
            android:entries="@array/myOptions"
            android:entryValues="@array/myValues"
            android:key="myKey"
            android:summary="Select an item"
            android:title="Choose" />
    </PreferenceCategory>

</PreferenceScreen>

my strings.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<resources>
    <string-array name="myOptions">
        <item>A</item>
        <item>B</item>
        <item>C</item>
    </string-array>
    <string-array name="myValues">
        <item>1</item>
        <item>2</item>
        <item>3</item>
    </string-array>
</resources>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:49:53+00:00Added an answer on June 9, 2026 at 12:49 am

    My problem was that I conceptualised the preference framework wrong. I will share my wrong turns to help any other lost newbies.

    I had confused the PreferenceScreen which I defined in my preferences.xml above and the file where the preferences are stored which can be seen on the DDMS perspective my.package.name_preferences.xml – use file manager – data – data – project name – shared_prefs to find and pull this file

    <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
    <map>
    <string name="myKey">1</string>
    </map>
    

    I then incorrectly proceeded to try use the preference editor to alter the PreferenceScreen

    SharedPreferences preferences = getSharedPreferences("myKey", MODE_PRIVATE);
    preferences.edit().remove("1").commit();
    

    Eventually I found the sample code in the Google APIs
    com.example.android.apis.ApiDemos
    com/example/android/apis/preference
    which have all the permutations I needed, in order to finally understand.

    my eventual solution was to use a combination of
    setPreferenceScreen(createPreferenceHierarchy());
    addPreferencesFromResource(R.xml.preferences);

    to incorporate both my static and dynamic preferences. The code for my dynamic preferences I add below although, (thanks to Google). I recommend looking at the google api rather than trying to find tutorials online or using books.

    import java.util.List;
    import android.os.Bundle;
    import android.preference.ListPreference;
    import android.preference.PreferenceActivity;
    import android.preference.PreferenceCategory;
    import android.preference.PreferenceScreen;
    
    public class SetPreferenceActivityGB extends PreferenceActivity {
        private static final String LENGTH_DEFAULT = "4";
        private static final String LEVEL_DEFAULT = "level1";
    
        @SuppressWarnings("deprecation")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setPreferenceScreen(createPreferenceHierarchy());
            addPreferencesFromResource(R.xml.preferences);
        }
    
    
        private PreferenceScreen createPreferenceHierarchy() {
            PreferenceScreen root = getPreferenceManager().createPreferenceScreen(
                    this);
    
    
            // Dialog based preferences
            PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
            root.addPreference(dialogBasedPrefCat);
    
            // List preference
            ListPreference listPref = new ListPreference(this);
    
    // This is where I get my dynamic list of levels and use it as a character sequence to
    // To make my list view dynamic.     
            List<String> myList ; 
    
            myList = Playsound.getAvailableLevels(); // Function which is Dynamic
            CharSequence[] cs = myList.toArray(new CharSequence[al.size()]);
    
            listPref.setEntries(cs);
            listPref.setEntryValues(cs);
            // listPref.setEntries(R.array.levels);
            // listPref.setEntryValues(R.array.levelsValues);
            listPref.setDialogTitle(R.string.level_title);
            listPref.setKey("level");
            listPref.setTitle(R.string.level_title);
            listPref.setValueIndex(0);
            listPref.setDefaultValue(LEVEL_DEFAULT);
            listPref.setSummary(R.string.level_summary);
            dialogBasedPrefCat.addPreference(listPref);
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to programmatically add options to a SELECT drop down in IE Windows Mobile.
I'm trying to programmatically add an Execute SQL task from within a script task
I am trying to programmatically create a PowerPoint from graphs in Access. Ideally, when
I'm trying to programmatically extract information from an Enterprise Architect model (saved in an
I'm trying to programmatically add dots to a radar screen. The code runs without
i am trying to programmatically attach an image to an email body from my
I have a repeater control that generates a list of links from a SqlReader.
I'm trying to programmatically close a Facebox modal with JavaScript code that's called within
I'm simply trying to delete a row from a DataGridView. Basically, the first column
I'm trying to programmatically set the constructor sting of a COM+ component from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.