2015-12-01 55 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(); 
      }