我想在使用共享首选项生成的ArrayList中搜索输入字符串

时间:2018-10-26 16:24:56

标签: java android arraylist sharedpreferences

 public void onClick(View view){

    if(view.getId()==R.id.loginId){

        String usr= number.getText().toString();
        String pwd= passWord.getText().toString();
        boolean containsUsr = newUserIds.contains(usr);

        if(containsUsr==true)
   {
            int index = newUserIds.indexOf(usr);

            if(pwd.equals(newPws.get(index).toString()))
      {
        //Toast.makeText(this,"Logging in",Toast.LENGTH_SHORT).show();
        Intent loginActivityIntent= new Intent(getApplicationContext(),GeoWorker.class);
        startActivity(loginActivityIntent);
       }
        else
        Toast.makeText(this,"Invalid Password",Toast.LENGTH_SHORT).show();
    }
        else
            Toast.makeText(this,"Invalid Phone Number",Toast.LENGTH_SHORT).show();
    }
如果电话号码错误,将执行

else语句。但无法验证密码。输入密码后,应用程序将关闭。消息显示:应用程序停止工作。 在OnCreate方法中:

SharedPreferences sharedPreferences=this.getSharedPreferences("com.example.monica.miniprojectdemo", Context.MODE_PRIVATE);

    userIds= new ArrayList<String>();
    userIds.add("1234567890");
    userIds.add("9876543210");
    userIds.add("8822446688");
    userIds.add("0504030201");
    userIds.add("519121318");
    userIds.add("900000249");

    pws = new ArrayList<String>();
    pws.add("0000");
    pws.add("1111");
    pws.add("2222");
    pws.add("3333");
    pws.add("4444");
    pws.add("5555");
    try {
        sharedPreferences.edit().putString("userName",ObjectSerializer.serialize(userIds)).apply();
        sharedPreferences.edit().putString("pass",ObjectSerializer.serialize(pws)).apply();
    }catch (Exception e){
        e.printStackTrace();
    }

    newUserIds = new ArrayList<String>();
    newPws= new ArrayList<String>();
    try {
        newUserIds = (ArrayList<String>)ObjectSerializer.deserialize(sharedPreferences.getString("userName", ObjectSerializer.serialize(new ArrayList<String>())));
        newPws = (ArrayList<String>) ObjectSerializer.deserialize(sharedPreferences.getString("password", ObjectSerializer.serialize(new ArrayList<String>())));
    }catch (Exception e){
        e.printStackTrace();
    }

这是我的学校项目。我的时间很少。

0 个答案:

没有答案