0

我正在尝试使用对话框中的共享首选项保存数据,然后在列表视图中显示该数据。我能够在列表视图中显示数据,但无法使用共享首选项保存数据

这是代码

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

    final Dialog dialog=new Dialog(Template.this);
    dialog.setContentView(R.layout.temp_dialog);
    dialog.setTitle("New Template");

    Button btn_ok=(Button)dialog.findViewById(R.id.bt_temp_diolg_ok);

    btn_ok.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        String edit1=(String) ((EditText)dialog.findViewById(R.id.et_diolg_temp)).getText().toString();
        template.add(edit1);

        SharedPreferences spf = getSharedPreferences("temp", MODE_MULTI_PROCESS);
        SharedPreferences.Editor spfe = spf.edit();
        Map alldata=spf.getAll();
        spfe.putString("Template", edit1);
        spfe.commit();
        dialog.dismiss();
        temp.notifyDataSetChanged();

        tempListView=(ListView)findViewById(R.id.listView_templates);
        temp = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,template);
        tempListView.setAdapter(temp);

        Button btn=(Button)findViewById(R.id.btn_newtemp);
        btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            dialog.show();
        }
    }

这是对话框的代码:

    final Dialog dialog=new Dialog(Template.this);
    dialog.setContentView(R.layout.temp_dialog);
    dialog.setTitle("New Template");

    Button btn_ok=(Button)dialog.findViewById(R.id.bt_temp_diolg_ok);

    btn_ok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String edit1=(String) ((EditText)dialog.findViewById(R.id.et_diolg_temp)).getText().toString();
            template.add(edit1);


            SharedPreferences spf = getSharedPreferences("temp", MODE_MULTI_PROCESS);
            SharedPreferences.Editor spfe = spf.edit();
            Map alldata=spf.getAll();
            spfe.putString("template",edit1);
            spfe.commit();
            dialog.dismiss();
            spf.getString("template", edit1);
            temp.notifyDataSetChanged();    

        }
    });

            tempListView=(ListView)findViewById(R.id.listView_templates);
            temp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,template);
            tempListView.setAdapter(temp);

            Button btn=(Button)findViewById(R.id.btn_newtemp);
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.show();
                }
          });
    }
}
4

0 回答 0