保存数据问题,SharedPreferences

时间:2016-05-04 23:32:59

标签: android

这是我的代码:

public class MainActivity extends AppCompatActivity {
ListView listView;
List all;
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sharedPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
    editor = sharedPref.edit();
    setListAdapter();
}
private void setListAdapter(){
    final String[] data = displayData().toString().split(",");
    ListAdapter listAdapter = new CustomAdapter(this, data);
    listView = (ListView) findViewById(R.id.listView);
    listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
    listView.setAdapter(listAdapter);
}
public List displayData(){
    all = new ArrayList<>();
    try {
        Map<String, ?> allMap = sharedPref.getAll();
        for (Map.Entry<String, ?> entry : allMap.entrySet()) {
            all.add(entry.getKey().toString() + ":" + entry.getValue().toString());
        }
    } catch(NullPointerException npe){
        all.add(" : ");
    }
    Log.i("understandCode", "displayData: " + all.toString());
    return all;
}

public void add(View view){
    editor.putString(" ", " ");
    editor.apply();
    setListAdapter();
}
private class CustomAdapter extends ArrayAdapter<String> {
    SharedPreferences.Editor editor;
    String type;
    String content;
    public CustomAdapter(Context context, String[] resource) {
        super(context, R.layout.custom_row, resource);
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences
                (MainActivity.this);
        editor = sharedPref.edit();
    }
    EditText typeET;
    EditText contentET;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        View customView = layoutInflater.inflate(R.layout.custom_row, parent, false);
        String singleItem = getItem(position);
        typeET = (EditText) customView.findViewById(R.id.type);
        contentET = (EditText) customView.findViewById(R.id.content);
        String[] givenStrings = singleItem.split(":");
        try {
            typeET.setText(givenStrings[0]);
            contentET.setText(givenStrings[1]);
        }catch(ArrayIndexOutOfBoundsException aioobe){
            typeET.setText(" ");
            contentET.setText(" ");
        }
        typeET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                type = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                type = s.toString();
                save();
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        contentET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                content = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                content = s.toString();
                save();
            }

            @Override
            public void afterTextChanged(Editable s) {


            }
        });
        return customView;
    }

    private void save() {
        editor.putString(type, content);
        editor.apply();
        Log.i("understandCode", type + ":" + content);

    }



}

}

这是我编辑密钥时的日志,值:

05-04 17:52:52.107 11146-11146/com.x.xI/understandCode: [ my  mom!a said:y ]
05-04 17:52:52.462 11146-11146/com.valerdaita.myinfo I/understandCode: [ my mom!a said:yo ]
05-04 17:52:52.698 11146-11146/com.valerdaita.myinfo I/understandCode: [ my mom!a said:yol ]
05-04 17:52:52.897 11146-11146/com.x.x I/understandCode: [ my mom!a said:yolo ]

当我重新启动活动时:

05-04 17:53:05.085 12056-12056/com.x.x I/understandCode: displayData: [ : ]

因此,它没有显示我保存的数据。我真的无法识别问题因为我知道我的保存功能因为Log而运行良好,但我无法识别显示器的任何问题。

1 个答案:

答案 0 :(得分:0)

您肯定会获得NPE,因为您尚未在首选项中保存任何值。

代码流程为setListAdapter() ->displayData() ->sharedPref.getAll()

在此之间,您不会向首选项添加任何键值对。 那你为什么期望你不会得到NPE呢? 如果您不想获得NPE,请在使用getAll()之前保存一些值。