2015-12-01 57 views
0

我有從ArrayList中刪除項目的問題。我試過了它可能是100次,但我無法修復它。保存到列表不是問題,但很難刪除。如何從數組中刪除SharedPreferences

當我刪除SharedPrefs鍵(位置)這是第一次,但如果我第一次刪除第一個位置它從列表中刪除,但它仍然在首選項,所以當我嘗試刪除第一個位置第二次我不能刪除它,因爲有仍然保存偏好與價值「」,但我需要刪除這個偏好整個第一個位置必須包含偏好與第二個位置上的值不是「」。

我試圖製作一些圖片以便更好地理解。 那之前除去1號位: image1

,這是除去1號位後

image2

還有就是我CustomListAdapter類

public class CustomListAdapterInterests extends ArrayAdapter <String> { 

    private final Activity context; 
    private final ArrayList <String> mItemInterest; 

    public CustomListAdapterInterests(Activity context, ArrayList <String> itemInterest) { 
     super(context, R.layout.list_item_interests, itemInterest); 

     this.context = context; 
     this.mItemInterest = itemInterest; 
    } 

    @Override 
    public int getCount() { 
     return mItemInterest.size(); 
    } 

    public View getView(int position, View view, ViewGroup parent) { 

     LayoutInflater inflater = context.getLayoutInflater(); 
     View rowView = inflater.inflate(R.layout.list_item_interests, null, true); 

     TextView itemInterestTV = (TextView) rowView.findViewById(R.id.textInterest); 

     itemInterestTV.setText(mItemInterest.get(position)); 
     return rowView; 
    } 
} 

這裏是我的片段

public class InterestsFragment extends BaseFragment { 

    private ArrayList <String> mInterestList; 
    private static final int MAX_STORED_LINES_INTERESTS = 50; 
    private FloatingActionButton plusInterestsBTN; 
    private CustomListAdapterInterests adapterInterests; 
    private ListView listInterests; 
    private EditText interestET; 
    private Button confirmInterestBTN; 
    public SharedPreferences sharedPreferences; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment_interests, container, false); 

     plusInterestsBTN = (FloatingActionButton) v.findViewById(R.id.plusInterests); 
     sharedPreferences = getActivity().getSharedPreferences(Constants.PREFERENCES_INTERESTS, Context.MODE_PRIVATE); 
     mInterestList = new ArrayList <String>(); 
     loadInterestFromPreferences(mInterestList); 
     adapterInterests = new CustomListAdapterInterests(getActivity(), mInterestList); 

     listInterests = (ListView) v.findViewById(R.id.listViewInterests); 
     listInterests.setAdapter(adapterInterests); 

     listInterests.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
      public void onItemClick(AdapterView <? > arg0, View v, int position, long arg3) { 


       if (sharedPreferences.contains(Constants.INTEREST + position)) { 
        SharedPreferences.Editor editor = sharedPreferences.edit(); 
        mInterestList.remove(position); 
        adapterInterests.notifyDataSetChanged(); 
        editor.remove(Constants.INTEREST + position); 



        editor.commit(); 
       } 
      } 
     }); 

     listInterests.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {@Override 
      public boolean onItemLongClick(AdapterView <? > arg0, View arg1, 
      final int position, long id) { 
       onShowDialogSetItem(position); 
       return true; 
      } 
     }); 

     plusInterestsBTN.setOnClickListener(new View.OnClickListener() {@Override 
      public void onClick(View v) { 
       onShowDialogAddItem(); 
      } 

     }); 

     listInterests.setOnScrollListener(new AbsListView.OnScrollListener() {@Override 
      public void onScrollStateChanged(AbsListView view, int scrollState) { 

       int btn_initPosY = plusInterestsBTN.getScrollY(); 

       if (scrollState == SCROLL_STATE_TOUCH_SCROLL) { 
        plusInterestsBTN.animate().cancel(); 
        plusInterestsBTN.animate().translationXBy(350); 
       } else { 
        plusInterestsBTN.animate().cancel(); 
        plusInterestsBTN.animate().translationX(btn_initPosY); 
       } 
      } 

      @Override 
      public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

      } 
     }); 
     return v; 
    } 

    private void loadInterestFromPreferences(ArrayList <String> mInterestList) { 
     for (int x = 0; x < 5; x++) { 

      String interests = sharedPreferences.getString(Constants.INTEREST + x, Constants.DEFAULT); 

      Toast.makeText(getActivity(), interests, Toast.LENGTH_SHORT).show(); 

      if (interests != "") { 
       mInterestList.add(interests); 
      } 


     } 
    } 


    private void onShowDialogSetItem(final int position) { 
     final Dialog dialogInterest = new Dialog(getActivity()); 
     dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top; 
     dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName; 

     LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false); 

     dialogInterest.setCanceledOnTouchOutside(true); 
     dialogInterest.setContentView(view); 

     final EditText interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest); 
     Button confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest); 
     TextView title = (TextView) dialogInterest.findViewById(R.id.textView2); 
     title.setText("Edit Interest"); 
     interestET.setText(mInterestList.get(position)); 

     confirmInterestBTN.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Log.d("2", "" + position); 
       String interest = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT); 



       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putString(Constants.INTEREST + position, interestET.getText().toString()); 
       editor.commit(); 
       String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT); 

       mInterestList.set(position, interestET.getText().toString()); 


       Toast.makeText(getActivity(), "Upravené: " + interests, Toast.LENGTH_SHORT).show(); 
       adapterInterests.notifyDataSetChanged(); 
       dialogInterest.dismiss(); 

      } 
     }); 
     dialogInterest.show(); 
    } 
    private void onShowDialogAddItem() { 

     if (mInterestList.size() >= MAX_STORED_LINES_INTERESTS) { 
      return; 
     } 
     final Dialog dialogInterest = new Dialog(getActivity()); 
     dialogInterest.getWindow().getAttributes().windowAnimations = R.anim.abc_slide_in_top; 
     dialogInterest.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     dialogInterest.getWindow().getAttributes().windowAnimations = R.style.animationName; 

     LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.fragment_interests_add_event, null, false); 

     dialogInterest.setCanceledOnTouchOutside(true); 
     dialogInterest.setContentView(view); 

     interestET = (EditText) dialogInterest.findViewById(R.id.editTextInterest); 
     confirmInterestBTN = (Button) dialogInterest.findViewById(R.id.confirmInterest); 

     confirmInterestBTN.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       int position = listInterests.getAdapter().getCount(); 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       editor.putString(Constants.INTEREST + position, interestET.getText().toString()); 
       editor.commit(); 
       String interests = sharedPreferences.getString(Constants.INTEREST + position, Constants.DEFAULT); 
       Toast.makeText(getActivity(), "Přidané: " + interests, Toast.LENGTH_SHORT).show(); 
       mInterestList.add(interestET.getText().toString()); 
       //adapterInterests.notifyDataSetChanged(); 

       dialogInterest.dismiss(); 
      } 
     }); 
     dialogInterest.show(); 
     adapterInterests.notifyDataSetChanged(); 
    } 
} 

謝謝你的幫助。對不起我的英語不好。如果你會幫助我,我可以爲你或谷歌播放設計做任何材料設計應用程序圖標。謝謝。如果信息很少,請告訴我。

回答

0

我認爲,如果你所有的字符串列表保存到偏好的單一屬性將使其易於管理。

看到這個樣本:

//for save 
StringBuilder sb = new StringBuilder(); 
for (String interest : mInterestList) { 
    sb.append(interest).append(","); 
} 
prefsEditor.putString("MyInterests", sb.toString()); 
prefsEditor.commit(); 

//for read 
String [] interests= sharedPreferences.getString("MyInterests"); 
mInterestList = new ArrayList<String>(Arrays.asList(interests)); 

在每次更改您的mInterestList只是再次保存它。無需刪除和添加。更改您的mInterestList並再次保存在共享首選項中。

0

在我看來你的適配器運行mInterestList。 當您刪除首選項時,我沒有看到從mInterestsList中刪除數據項目?

+0

你好,我現在編輯我的代碼(下面刪除首選項) 我加了 'mInterestList.remove(position); adapterInterests.notifyDataSetChanged();' 但它並沒有改變任何東西 謝謝你的回答:)。 –

+0

您已經構建了適配器。嘗試調用adapterInterests.remove() – mjstam

+0

我試過了,但它再次無法正常工作。我認爲它從列表中刪除很好,但我認爲有偏好問題..或者我不知道我需要幫助 –

0

而不是檢查共享偏好是否包含,看它是否被設置,而不是或不爲空,也就是做,

if (sharedPreferences.getString(Constants.INTEREST + position)!=null) { 
       SharedPreferences.Editor editor = sharedPreferences.edit(); 
       mInterestList.remove(position); 
       adapterInterests.notifyDataSetChanged(); 
       editor.remove(Constants.INTEREST + position); 



       editor.commit(); 
      }