Android SharedReferences删除不起作用
希望有人能帮忙,处理这件事连续3天不走运,头发掉了!!。 我可以从共享首选项中保存和加载对象,但我不能删除它们,这不起作用。我没有错误 它通常从SharedReferences加载对象并将其添加到SharedReferences中,但当我调用remove方法时,它会重新加载数组列表并提取一个与原始加载不同的新对象数组,从而丢失位置。不确定是否有意义;希望有人能帮忙Android SharedReferences删除不起作用,android,sharedpreferences,android-context,Android,Sharedpreferences,Android Context,希望有人能帮忙,处理这件事连续3天不走运,头发掉了!!。 我可以从共享首选项中保存和加载对象,但我不能删除它们,这不起作用。我没有错误 它通常从SharedReferences加载对象并将其添加到SharedReferences中,但当我调用remove方法时,它会重新加载数组列表并提取一个与原始加载不同的新对象数组,从而丢失位置。不确定是否有意义;希望有人能帮忙 public class FavFragment extends Fragment { Context context; priva
public class FavFragment extends Fragment {
Context context;
private ListView lv;
private ArrayList<VocabCatModel> vocabCatList;
FavVocabAdapter adapter;
SharedPreference sharedPreference;
public static FavFragment newInstance() {
FavFragment fragment = new FavFragment();
return fragment;
}
public FavFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreference=new SharedPreference(getActivity());
System.out.println("Loading Favorites new SHaredpreference object");
try {
vocabCatList = sharedPreference.loadFavorites(getActivity());
System.out.println("Current Favs: " + vocabCatList);
System.out.println("Current context after loading first time:" + getActivity());
} catch (NullPointerException e){
e.printStackTrace();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.vocab_search, container, false);
lv = (ListView) rootView.findViewById(R.id.list_view);
return rootView;
}
@Override
public void onResume() {
super.onResume();
Log.e("onResume", "onResume Called");
if(vocabCatList != null ) {
try {
Log.e("adapter", "new adapter and setting up to listview");
adapter = new FavVocabAdapter(getActivity(), vocabCatList);
lv.setAdapter(adapter);
System.out.println("Favs after lv.setadapter: "+ vocabCatList);
} catch (NullPointerException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long arg3){
sharedPreference.removeFavorite(getActivity(), vocabCatList.get(position));
adapter.notifyDataSetChanged();
Toast.makeText(arg0.getContext(), vocabCatList.get(position) + "removed", Toast.LENGTH_SHORT)
.show();
vocabCatList.remove(vocabCatList.get(position));
return true;
}
});
}
public class FavVocabAdapter extends BaseAdapter {
Context context;
ArrayList<VocabCatModel> vocabCatList;
public FavVocabAdapter(Context context, ArrayList<VocabCatModel> vocabCatList){
this.context = context;
this.vocabCatList = vocabCatList;
}
public int getCount(){return vocabCatList.size();}
public Object getItem(int position){return vocabCatList.get(position);}
public long getItemId(int position){return position;}
private class ViewHolder {
TextView tvName,tvTranslation;
ImageView ivImage;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
final ViewHolder holder;
//convertView = null;
LayoutInflater mInflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
convertView = mInflater.inflate(R.layout.categorylistviewitem, parent, false);
holder = new ViewHolder();
holder.tvName = (TextView)convertView.findViewById(R.id.itemTextView);
holder.tvTranslation = (TextView)convertView.findViewById(R.id.translationTextView);
holder.ivImage = (ImageView)convertView.findViewById(R.id.itemImage);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
holder.tvName.setText(vocabCatList.get(position).getName());
holder.tvTranslation.setText(vocabCatList.get(position).getTranslation());
holder.ivImage.setImageResource(R.drawable.ic_favorite);
holder.ivImage.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
sharedPreference.removeFavorite(context, vocabCatList.get(position));
vocabCatList.remove(vocabCatList.get(position));
holder.ivImage.setImageResource(R.drawable.directions);
notifyDataSetChanged();
}
});
return convertView;
}
}
}
公共类FavFragment扩展片段{
语境;
私有ListView lv;
私人ArrayList词汇词典;
favv适配器;
SharedReference SharedReference;
公共静态FavFragment newInstance(){
FavFragment片段=新的FavFragment();
返回片段;
}
公共FavFragment(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
SharedReference=新的SharedReference(getActivity());
System.out.println(“加载收藏夹新SharedReference对象”);
试一试{
vocabCatList=SharedReference.loadFavorites(getActivity());
System.out.println(“当前FAV:+vocabCatList”);
System.out.println(“第一次加载后的当前上下文:+getActivity());
}捕获(NullPointerException e){
e、 printStackTrace();
}
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.vocab_搜索,容器,false);
lv=(ListView)rootView.findViewById(R.id.list\u视图);
返回rootView;
}
@凌驾
恢复时公开作废(){
super.onResume();
Log.e(“onResume”、“onResume被称为”);
如果(vocabCatList!=null){
试一试{
Log.e(“适配器”、“新适配器和listview设置”);
adapter=新的favvocable适配器(getActivity(),vocabCatList);
低压设置适配器(适配器);
System.out.println(“lv.setadapter:+vocabCatList之后的FAV”);
}捕获(NullPointerException e){
e、 printStackTrace();
}
adapter.notifyDataSetChanged();
}
lv.setOnItemLongClickListener(新的AdapterView.OnItemLongClickListener(){
@凌驾
公共布尔值长单击(AdapterView arg0、视图arg1、整型位置、长arg3){
removeFavorite(getActivity(),vocabCatList.get(position));
adapter.notifyDataSetChanged();
Toast.makeText(arg0.getContext(),vocabCatList.get(position)+“removed”,Toast.LENGTH\u SHORT)
.show();
移除(vocabCatList.get(position));
返回true;
}
});
}
公共类FAVvacable适配器扩展了BaseAdapter{
语境;
ArrayList词汇词典;
公共FAVvocable适配器(上下文上下文,ArrayList vocabCatList){
this.context=上下文;
this.vocabCatList=vocabCatList;
}
public int getCount(){return vocabCatList.size();}
公共对象getItem(int位置){return vocabCatList.get(position);}
公共长getItemId(int位置){return position;}
私有类视窗持有者{
文本视图tvName,tvTranslation;
图像视图;
}
@凌驾
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
最终持票人;
//convertView=null;
LayoutInflater mInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
if(convertView==null){
convertView=mInflater.inflate(R.layout.categorylistviewitem,父项,false);
holder=新的ViewHolder();
holder.tvName=(TextView)convertView.findViewById(R.id.itemTextView);
holder.tvTranslation=(TextView)convertView.findViewById(R.id.translationTextView);
holder.ivImage=(ImageView)convertView.findViewById(R.id.itemImage);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.tvName.setText(vocabCatList.get(position.getName());
holder.tvTranslation.setText(vocabCatList.get(position.getTranslation());
holder.ivImage.setImageResource(R.drawable.ic_收藏夹);
holder.ivImage.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
SharedReference.removeFavorite(上下文,vocabCatList.get(位置));
移除(vocabCatList.get(position));
holder.ivImage.setImageResource(R.drawable.directions);
notifyDataSetChanged();
}
});
返回视图;
}
}
}
////////////////////////我使用的共享首选项类
public class SharedPreference {
private Context context;
public static final String PREFS_NAME = "VOCAB_APP";
public static final String FAVORITES = "Favorite";
public SharedPreference(Context context){
super();
}
public void storeFavorites(Context context, List<VocabCatModel> favorites){
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
Gson gson = new Gson();
String jsonFavorites = gson.toJson(favorites);
System.out.println(favorites);
editor.putString(FAVORITES, jsonFavorites);
System.out.println("commit()");
editor.apply();
}
public ArrayList<VocabCatModel> loadFavorites(Context context) {
SharedPreferences settings;
List<VocabCatModel> favorites;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
if (settings.contains(FAVORITES)) {
String jsonFavorites = settings.getString(FAVORITES, null);
Gson gson = new Gson();
System.out.println("loading favorites");
VocabCatModel[] favoriteItems = gson.fromJson(jsonFavorites,VocabCatModel[].class);
favorites = Arrays.asList(favoriteItems);
favorites = new ArrayList<>(favorites);
} else
return null;
return (ArrayList<VocabCatModel>) favorites;
}
public void addFavorite(Context context, VocabCatModel vocabCatModel) {
List<VocabCatModel> favorites = loadFavorites(context);
if (favorites == null)
favorites = new ArrayList<>();
favorites.add(vocabCatModel);
storeFavorites(context, favorites);
}
public void removeFavorite(Context context, VocabCatModel vocabCatModel) {
ArrayList<VocabCatModel> favorites = loadFavorites(context);
System.out.println("Removing favorite");
System.out.println("new favs" + favorites);
if (favorites != null) {
System.out.println("object from click" + vocabCatModel);
favorites.remove(vocabCatModel);
System.out.println(context);
storeFavorites(context, favorites);
}
}
}
公共类共享引用{
私人语境;
公共静态最终字符串PREFS_NAME=“VOCAB_APP”;
公共静态最终字符串FAVORITES=“Favorite”;
公共共享引用(上下文){
超级();
}
公共void存储收藏夹(上下文、列表收藏夹){
共享引用设置;
编辑;
设置=context.getSharedReferences(PREFS\u NAME,context.MODE\u PRIVATE);
editor=settings.edit();
Gson Gson=新的Gson();
字符串jsonFavorites=gson.toJson(收藏夹);
System.out.println(收藏夹);
putString(收藏夹,jsonFavorites);
System.out.println(“commit()”);
editor.apply();
}
公共ArrayList loadFavorites(上下文){
共享引用设置;
列出收藏夹;
设置=context.getSharedReferences(PREFS\u NAME,context.MODE\u PRIVATE);
如果(设置.包含(收藏夹)){
String jsonFavorites=settings.getString(收藏夹,null);
Gson Gson=新的Gson();
System.out.println(“加载收藏夹”);
VocabCatModel[]favoriteItems=g
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME,Context.MODE_PRIVATE);
editor = settings.edit();
try{
System.out.println("PREFS_NAME:"+settings.getString(PREFS_NAME,""));
editor.remove(PREFS_NAME);
editor.commit();
}catch(Exception e){
System.out.println("Cart Error");
}