如果它等于此共享首选项的新条目,如何检查存储在共享首选项中的项目

时间:2013-07-01 09:12:27

标签: android

我有两个共享首选项问题..第一个当我尝试将新数据存储到共享首选项时,旧数据被覆盖!

我也使用方法“Contains(String key)”将新条目与旧条目进行比较,如果它们相等!! ,但我认为这是不正确的! 如果新条目与旧条目之一相等,那么在共享首选项中将新条目与旧条目进行比较的最佳方法是什么?

这是我的注册代码:

   package com.example.task_8;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Ac_3 extends Activity {

    Button save;
    EditText userName,password,userId;
    String name,pass,Id;
    Intent i;
     SharedPreferences sp;

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

        setContentView(R.layout.ac_3);

        save= (Button)findViewById(R.id.button1);
        userName=(EditText) findViewById(R.id.editText1);
        password=(EditText) findViewById(R.id.editText2);
        userId=(EditText) findViewById(R.id.editText3);
        sp = getSharedPreferences("myshared", 0);

        /////////////////////////////////////////////////

          i = getIntent();




            TextWatcher listener = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @SuppressWarnings("deprecation")
                @Override
                public void afterTextChanged(Editable s) {

                    name= userName.getText().toString();
                    pass= password.getText().toString();
                    Id=userId.getText().toString();

                    } 


            };

            password.addTextChangedListener(listener);
            userName.addTextChangedListener(listener);



            save.setOnClickListener( new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub


                    if(!(Id.equals(sp.contains(Id))))
                            {


                    SharedPreferences.Editor spe = sp.edit();
                    spe.putString("userName",name);
                    spe.commit();
                    spe.putString("password",pass);
                    spe.commit();
                    spe.putString("userId",Id);
                    spe.commit();

                            }

                    else 

                    {

                        AlertDialog alertDialog = null;
                        alertDialog = new AlertDialog.Builder(Ac_3.this).create();
                        alertDialog.setTitle("error msg");
                        alertDialog.setMessage("The two passwords does not match");
                        alertDialog.show();

                    }


                    i = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(i);

                }




            });



}

}

并且在主要活动中,如果我想登录,我想检查此条目是否存在:

login.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    if(!name.equals(null) && !pass.equals(null))
                    {

                        if(sp.contains(Id)&&sp.contains(name)&&sp.contains(pass))

                        {

                            i = new Intent(getApplicationContext(), Ac_2.class);
                            i.putExtra("userName", name);
                            i.putExtra("password", pass);
                            i.putExtra("userId", Id);
                            startActivity(i);


                        }

                        else {
                            AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
                            alertDialog.setTitle("error msg");
                            //alertDialog.setPositiveButton("OK",this);
                            //alertDialog.setNegativeButton("Cancel", this);
                            alertDialog.setMessage("You should register before");
                            alertDialog.show();
                            /*alertDialog.setPositiveButton("Ok",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                          if (which == Dialog.BUTTON_POSITIVE) {
                                                      //do what ever you want on OK click
                                                    }  
                                        }
                                    }); */


                        }



                    }

                }


            }); 

1 个答案:

答案 0 :(得分:1)

你可以首先得到它已经存储在SharedPreference中的内容。

      String oldUser=spe.gettString("userName",name);

      String oldpass= spe.getString("password",pass);

       String oldId= spe.getString("userId",Id);

现在,根据您的要求,使用新值对它们进行拼凑:

例如:

if(Id.equalsIgnoreCase(oldId))
{
//it contains
}
else
{}