2016-05-18 8 views
0

今日、私は私の主張に賛同したいと思っています。私はStringTokenizerを使用して3つの文字列を共有環境設定に入れています。各テキストビューを親ビューから削除したり、長いクリックイベントを使用して共有プリファレンスから削除することができます。Androidの共有設定から保存したデータを削除する方法

ここに参考になるコードです。

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(); 

これは私が同じを取得し、ダイナミックtextviewsに結合しています場所です。

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); 

答えて

2

使用このことは

キー名あなたはを削除したい値である................あなたを助ける...... prefsCont.edit().remove("appName1").commit();

、あなたWA場合 - :あなたのケースの例では..

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

NT に....すべてが、これはあなたがクリックされた値を見つけるために、この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(); 
に配列を置くためにこれを使用します

コーディングを楽しむ...............

+0

こんにちはSishildlh、本当にありがとう、すべてのtextviewを削除することによって解決策が提供されましたが、実際には選択したtextviewの長いクリックで選択したtextviewのみを削除します。 – Adithya

+0

共有プリファレンスから個々の文字列トークンを削除したいSushildlh、。 – Adithya

+0

削除する個々の文字列トークンはどれですか? – sushildlh

0

特定の値を削除するには、以下を使用してください。

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

、共有環境設定から特定の値を削除したい場合は、以下の使用

あなたが使用することができます
--> SharedPreferences.Editor.clear() followed by a commit() 
+0

こんにちはVishal、Sishildlhソリューションのために以下に掲載されています。実際には、あなたと彼が提供するソリューションはすべてのテキストビューを削除しますが、実際には、選択したtextview – Adithya

0

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

の下に使用し、その後

preferences.edit().remove("key").commit(); 
をすべてのデータを削除するには

すべてのデータを削除する場合は

preferences.edit().clear().commit(); 
関連する問題