如何从Android中的共享首选项中删除已保存的数据

时间:2016-05-18 09:37:36

标签: android sharedpreferences

今天,我想在我的活动中寻求你的帮助,我在使用StringTokenizer在共享首选项中放入三个字符串。一切都很好,现在我想从父视图中删除每个textview以及从使用长按事件的共享首选项开始。

这是我的代码供您参考。

 This is the place where  I am storing the bitmap and other two string names in shared preferences

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();

            StringBuilder strCont = new StringBuilder();
            screenOnePreferences = PreferenceManager.getDefaultSharedPreferences(AppsActivity.this);
            String savedStringCont = screenOnePreferences.getString("appName1", "");
            StringTokenizer stCont = new StringTokenizer(savedStringCont, ",");
            String[] savedListCont = new String[stCont.countTokens()];
            for (int j = 0; j < savedListCont.length; j++) {
                savedListCont[j] =stCont.nextToken()+",";
                strCont.append(savedListCont[j]);
            }
            StringBuilder str1 = new StringBuilder();
            screenOnePreferences = PreferenceManager.getDefaultSharedPreferences(AppsActivity.this);
            String savedString1 = screenOnePreferences.getString("appPackName1", "");
            StringTokenizer st1 = new StringTokenizer(savedString1, ",");
            String[] savedList1 = new String[st1.countTokens()];
            for (int j = 0; j < savedList1.length; j++) {
                savedList1[j] =st1.nextToken()+",";
                str1.append(savedList1[j]);
            }
            StringBuilder strImg = new StringBuilder();
            screenOnePreferences = PreferenceManager.getDefaultSharedPreferences(AppsActivity.this);
            String savedImage = screenOnePreferences.getString("appImage1", "");
            StringTokenizer stimg = new StringTokenizer(savedImage, ",");
            String[] savedListImg = new String[stimg.countTokens()];
            for (int j = 0; j < savedListImg.length; j++) {
                savedListImg[j] =stimg.nextToken()+",";
                strImg.append(savedListImg[j]);
            }
            strCont.append(clickedAppName);
            str1.append(clickedApp);
            strImg.append(Base64.encodeToString(b, Base64.DEFAULT));
            PreferenceManager.getDefaultSharedPreferences(AppsActivity.this).edit().putString("appName1",strCont.toString()).commit();
            PreferenceManager.getDefaultSharedPreferences(AppsActivity.this).edit().putString("appPackName1",str1.toString()).commit();
            PreferenceManager.getDefaultSharedPreferences(AppsActivity.this).edit().putString("appImage1",strImg.toString()).commit();

这是我检索相同内容并绑定到动态文本视图的地方。

String[] array;
static SharedPreferences prefsCont;
static  String[] savedList2;
static List<String> list;
static StringBuilder strCont;
static String savedStringCont;
static String  savedString2;
static String savedStringImg;
static   StringTokenizer stCont;
static StringTokenizer st2;
static  StringTokenizer stImage;
static  String[] savedListCont;
static   String[] savedListImage;
static  List<String> numberList;
static Bitmap yourSelectedImage;
static  int numOfContacts;




    strCont  = new StringBuilder();
    prefsCont = PreferenceManager.getDefaultSharedPreferences(context);

    savedStringCont = prefsCont.getString("appName1", "");
    savedString2 = prefsCont.getString("appPackName1", "") ;
    savedStringImg = prefsCont.getString("appImage1", "");
    stCont = new StringTokenizer(savedStringCont, ",");
    st2 = new StringTokenizer(savedString2, ",");
    stImage = new StringTokenizer(savedStringImg, ",");
    savedListCont = new String[stCont.countTokens()];
    savedListImage = new String[stImage.countTokens()];
    savedList2 = new String[st2.countTokens()];

    numberList = Arrays.asList(stCont.toString().split(","));
    int prevTextViewId = 0;
    numOfContacts = stCont.countTokens();
    for ( int K = 0; K < numOfContacts; K++) {
        final int listenerI = K;
        savedListCont[K] = stCont.nextToken();
        savedList2[K] = st2.nextToken();
        savedListImage[K] = stImage.nextToken();


        byte[] b = Base64.decode(savedListImage[K], Base64.DEFAULT);
        InputStream is = new ByteArrayInputStream(b);
        yourSelectedImage = BitmapFactory.decodeStream(is);
        BitmapDrawable bd = new BitmapDrawable(context.getResources(), yourSelectedImage);


        for(int i = 0; i < 1; i++)
        {
            if(savedListCont[K] != null && savedList2[listenerI] != null && yourSelectedImage != null ){
                final TextView textView = new TextView(context);
                textView.setText(savedListCont[K]);
                textView.setCompoundDrawablesRelativeWithIntrinsicBounds(null, bd, null, null);
                textView.setPadding(0, 20, 0, 20);
                textView.setTextColor(Color.WHITE);
                textView.setMaxLines(1);
                textView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
                int curTextViewId = prevTextViewId + 1;
                textView.setId(curTextViewId);

                final RelativeLayout.LayoutParams params =
                        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                RelativeLayout.LayoutParams.WRAP_CONTENT);

                params.addRule(RelativeLayout.BELOW, prevTextViewId);
                textView.setLayoutParams(params);
                 textView.setOnLongClickListener(new View.OnLongClickListener() {

                    @Override
                    public boolean onLongClick(View v) {

                     //   Here I would like to delete individual textview from shared preferences.

                        return false;
                    }
                });

                prevTextViewId = curTextViewId;

                ll.addView(textView, params);

3 个答案:

答案 0 :(得分:2)

使用它可以帮助你................

keyname 是您想要删除 ........

的值

prefsCont.edit().remove("KeyName").commit();

在您的案例中: - prefsCont.edit().remove("appName1").commit();

如果您想删除所有,请使用此prefsCont.edit().clear().commit();

使用它来找出你点击的值....

array = ArrayUtils.removeElement(array, "your textview value");

并使用它将数组放入SharePrefernce

Set<String> set = new HashSet<String>();
set.addAll(listOfExistingScores);
prefsCont.putStringSet("key", set).commit();

享受编码...............

答案 1 :(得分:0)

要删除特定值,请使用以下

--> SharedPreferences.Editor.remove() followed by a commit()

要删除所有数据,请使用以下

--> SharedPreferences.Editor.clear() followed by a commit()

答案 2 :(得分:0)

你可以使用,

SharedPreferences preferences = getSharedPreferences("Mypref", 0);

如果您想从共享首选项中删除特定值,请使用下面的

preferences.edit().remove("key").commit(); 

如果要删除所有数据,

preferences.edit().clear().commit();