从CustomListAdapter问题中的SharedPreferences中删除值

时间:2017-10-30 13:26:43

标签: java android listview custom-lists

当用户添加注释时,此注释将保存到共享首选项中。但是,当用户单击图像以删除注释时,这将被正确删除并从共享首选项中正确删除。但是当用户关闭应用程序或更改标签并返回时,删除的笔记数量将替换为最后一个笔记,因此它会显示所有重复的笔记,而不是删除的内容。我无法弄明白这一点并且感到困惑:/

代码:

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import java.util.ArrayList;
import java.util.Map;
import static android.content.Context.MODE_PRIVATE;

public class CustomListAdapter extends ArrayAdapter<String> {

        private final Activity context;
        private ArrayList<String> notes = new ArrayList<>();
        private ImageView image;
        private int imageCross;
        TextView ruleNotesSet;
        final SharedPreferences FeedPref= PreferenceManager.getDefaultSharedPreferences(getContext());
        final SharedPreferences.Editor fd = FeedPref.edit();

        public CustomListAdapter(Activity context, ArrayList<String> notes, int imageCross) {
            super(context, R.layout.item,notes);
            this.context=context;
            this.notes = notes;
            this.imageCross = imageCross;
        }

        public View getView(final int position, View view, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            final View rowView = inflater.inflate(R.layout.item, null, false);
            ruleNotesSet = (TextView) rowView.findViewById(R.id.textView1);
            image = (ImageView) rowView.findViewById(R.id.icon);

            Glide.with(getContext())
                    .load(imageCross)
                    .skipMemoryCache(true)
                    .diskCacheStrategy(DiskCacheStrategy.NONE)
                    .into(image);


            image.setOnClickListener(new Button.OnClickListener(){
                @Override
                public void onClick(View v){
                    notes.remove(position);
                    fd.remove(Integer.toString(position));
                    fd.apply();
                    fd.commit();
                    notifyDataSetChanged();
                }
            });

            ruleNotesSet.setText(notes.get(position));
            fd.putString(Integer.toString(position), notes.get(position));
            fd.apply();
            fd.commit();

            return rowView;
        }

        public void addNote(String data) {
            notes.add(data);
        }
}

日志:

10-30 13:22:30.053 2538-2538/org.app.random E/Logs: Adding a note{}
10-30 13:22:35.437 2538-2538/org.app.random E/Logs: Adding a note{0=note1}
10-30 13:22:44.927 2538-2538/org.app.random E/Logs: Adding a note{0=note1}
10-30 13:22:49.422 2538-2538/org.app.random E/Logs: Adding a note{0=note1}
10-30 13:22:51.187 2538-2538/org.app.random E/Logs: Adding a note{0=note1}
10-30 13:22:51.200 2538-2538/org.app.random E/Logs: Adding a note{0=note1}
10-30 13:22:51.211 2538-2538/org.app.random E/Logs: Adding a note{0=note1, 1=note2}
10-30 13:22:51.222 2538-2538/org.app.random E/Logs: Adding a note{0=note1, 1=note2, 2=note3}
10-30 13:23:06.252 2538-2538/org.app.random E/Logs: Change tabs/Close app{0=note1, 1=note2, 2=note3, 3=note4}
10-30 13:23:06.299 2538-2538/org.app.random E/Logs: Adding a note{0=note1, 1=note2, 2=note3, 3=note4}
10-30 13:23:06.319 2538-2538/org.app.random E/Logs: Adding a note{0=note1, 1=note2, 2=note3, 3=note4}
10-30 13:23:26.709 2538-2538/org.app.random E/Logs: Removing a note{0=note1, 2=note3, 3=note4}
10-30 13:23:26.716 2538-2538/org.app.random E/Logs: Adding a note{0=note1, 2=note3, 3=note4}
10-30 13:23:26.721 2538-2538/org.app.random E/Logs: Adding a note{0=note1, 2=note3, 3=note4}
10-30 13:23:26.747 2538-2538/org.app.random E/Logs: Adding a note{0=note1, 1=note3, 2=note3, 3=note4}
10-30 13:23:29.795 2538-2538/org.app.random E/Logs: Removing a note{1=note3, 2=note4, 3=note4}
10-30 13:23:29.799 2538-2538/org.app.random E/Logs: Adding a note{1=note3, 2=note4, 3=note4}
10-30 13:23:29.815 2538-2538/org.app.random E/Logs: Adding a note{0=note3, 1=note3, 2=note4, 3=note4}
10-30 13:23:38.491 2538-2538/org.app.random E/Logs: Change tabs/Close app{0=note3, 1=note4, 2=note4, 3=note4}
10-30 13:23:38.506 2538-2538/org.app.random E/Logs: Adding a note{0=note3, 1=note4, 2=note4, 3=note4}
10-30 13:23:38.521 2538-2538/org.app.random E/Logs: Adding a note{0=note3, 1=note4, 2=note4, 3=note4}

我已经使日志非常具有描述性,基本上我添加了4个音符,按回去删除键盘,这会填充共享偏好,我更改标签,我回来删除音符和更改标签再次填充注释4与那些是除去。

编辑:

当我使用共享内容中的内容重新填充视图时,我需要的是:

Start off with : app{0=note1, 1=note2, 2=note3, 3=note4}
Remove notes1 and note2: {1=note3, 2=note4}
When repopulating the view repopulate with only : {1=note3, 2=note4}
Instead what happens currently: {1=note3, 2=note4, 3=note4, 4=note4}

编辑:

在下面的答案中代码更改后添加更多日志:

10-30 17:12:31.527 12984-12984/org.random.app E/Logs: Notes in addNote{0=note 1}
10-30 17:12:31.554 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1}
10-30 17:12:34.400 12984-12984/org.random.app E/Logs: Notes in addNote{0=note 1, 1=note 2}
10-30 17:12:34.415 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 1=note 2}
10-30 17:12:36.525 12984-12984/org.random.app E/Logs: Notes in addNote{0=note 1, 1=note 2, 2=note 3}
10-30 17:12:36.542 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 1=note 2, 2=note 3}
10-30 17:12:38.273 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 1=note 2, 2=note 3}
10-30 17:12:38.295 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 1=note 2, 2=note 3}
10-30 17:12:45.983 12984-12984/org.random.app E/Logs: Removing a note{0=note 1, 2=note 3}
10-30 17:12:45.991 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 2=note 3}
10-30 17:12:45.994 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 2=note 3}
10-30 17:12:50.951 12984-12984/org.random.app E/Logs: Change tabs/Close app{0=note 1, 2=note 3}
10-30 17:12:50.975 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 2=note 3}
10-30 17:12:50.978 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 2=note 3}
10-30 17:12:54.390 12984-12984/org.random.app E/Logs: Removing a note{0=note 1, 2=note 3}
10-30 17:12:54.401 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 2=note 3}
10-30 17:12:57.466 12984-12984/org.random.app E/Logs: Change tabs/Close app{0=note 1, 2=note 3}
10-30 17:12:57.522 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 2=note 3}
10-30 17:12:57.531 12984-12984/org.random.app E/Logs: Notes in getView{0=note 1, 2=note 3}

1 个答案:

答案 0 :(得分:1)

我可能会发现原因: 您在get View中再次将字符串添加回SharedPrefernces,每次调用它时都要执行此代码:

fd.putString(Integer.toString(position), notes.get(position));
fd.apply();
fd.commit();

就在下面一行:

ruleNotesSet.setText(notes.get(position));

enter image description here

正如您所看到的,当适配器构建视图以使其可见时,您将相同的值添加回SharedPreferences,如果您有足够大的列表,则向上/向下滚动会增加新的共享偏好的价值。

请将它们评论出来并检查结果。

新方法

创建一个类注意:

public class Note {

    private long key;
    private String note;


    public Note(String note) {
        this.note = note;
        this.key = System.currentTimeMillis();
    }

    public Note(long key, String note) {
        this.key = key;
        this.note = note;
    }

    public Note(String key, String note) {
        this.key = Long.parseLong(key);
        this.note = note;
    }

    public long getKey() {
        return key;
    }

    public String getNote() {
        return note;
    }

    public String getKeyAsString(){
        return String.valueOf(key);
    }
}

然后将此添加到您的Activity(仅用于测试):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<Note> notes = new ArrayList<Note>();
    try {
        notes.add(new Note(System.currentTimeMillis(), "Note 1"));
        Thread.sleep(2);
        notes.add(new Note(System.currentTimeMillis(), "Note 2"));
        Thread.sleep(2);
        notes.add(new Note(System.currentTimeMillis(), "Note 3"));
    } catch (Exception e) {

    }
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

    SharedPreferences.Editor edit = sp.edit();

    for (Note n : notes) {
        edit.putString(n.getKeyAsString(), n.getNote());
    }
}

这种从SharedPreferences加载笔记的方法

private List<Note> readNotes() {
    List<Note> notes = new ArrayList<Note>();
    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    Map<String, ?> values = sp.getAll();

    for (String key : values.keySet()) {
        notes.add(new Note(key, (String) values.get(key)));
    }

    return notes;
}

最后是你的自定义ListAdapter:

public class CustomListAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private ArrayList<Note> notes = new ArrayList<Note>();
    private ImageView image;
    private int imageCross;
    TextView ruleNotesSet;
    final SharedPreferences FeedPref= PreferenceManager.getDefaultSharedPreferences(getContext());
    final SharedPreferences.Editor fd = FeedPref.edit();

    public CustomListAdapter(Activity context, ArrayList<String> notes, int imageCross) {
        super(context, R.layout.item,notes);
        this.context=context;
        this.notes = notes;
        this.imageCross = imageCross;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        Note note = notes.get(position);

        LayoutInflater inflater = context.getLayoutInflater();
        final View rowView = inflater.inflate(R.layout.item, null, false);
        ruleNotesSet = (TextView) rowView.findViewById(R.id.textView1);
        image = (ImageView) rowView.findViewById(R.id.icon);

        Glide.with(getContext())
                .load(imageCross)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(image);


        image.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                Note n = (Note) v.getTag();
                notes.remove(n);
                fd.remove(n.getKeyAsString());
                fd.apply();
                fd.commit();
                notifyDataSetChanged();
            }
        });

        ruleNotesSet.setText(note.getNote());

        image.setTag(note);

        return rowView;
    }

    public void addNote(String data) {
        Note note = new Note(System.currentTimeMillis(), data);
        fd.putString(note.getKeyAsString(), note.getNote());
        fd.commit();
        notes.add(note);
        notifyDataSetChanged();
    }
}

我相信现在可以根据需要使用。

注意:我将时间戳作为密钥,以确保密钥永不重复,因此在添加新笔记时不会覆盖现有密钥。

另请注意,我在每个图像上添加了相应的Note对象作为视图标记,以便稍后在用户单击以将其删除时将其恢复,以便我们获得正确的注释键。

请尝试一下,让我知道它是否适合您。