共享首选项无法使用条件存储和读取值。(Android Studio)

时间:2018-01-28 07:10:43

标签: java android json if-statement sharedpreferences

我正在开发考勤系统。为了避免学生帮助他们的朋友签署出席,我计划使用共享偏好方法来存储学生 ID ,然后在使用登录时阻止他们访问朋友 ID

但是if else语句似乎没有读取共享首选项方法。

请指导我错误的地方,谢谢:

以下是代码:

package com.example.android.jomsign;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class LoginActivity extends AppCompatActivity {

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

        final SharedPreferences sharedPref = getSharedPreferences("data",MODE_PRIVATE);
        final Boolean Logined = sharedPref.getBoolean("Logined", false);
        final String sid = sharedPref.getString("SID", "");

        final EditText etid = (EditText) findViewById(R.id.etid);
        final EditText etpwd = (EditText) findViewById(R.id.etpwd);
        final Button btnlogin = (Button) findViewById(R.id.btnlogin);
        final Button btnregister = (Button) findViewById(R.id.btnregister);
        final TextView TextView2 = (TextView) findViewById(R.id.etid);
        TextView2.setText(sid);

        btnregister.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                LoginActivity.this.startActivity(registerIntent);
            }
        });

        btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String id = etid.getText().toString();
                final String pwd = etpwd.getText().toString();

                if(Logined == true) {
                    Response.Listener<String> responseListener = new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject jsonResponse = new JSONObject(response);
                                boolean success = jsonResponse.getBoolean("success");

                                if (success) {
                                    String id = jsonResponse.getString("id");
                                    String pwd = jsonResponse.getString("pwd");
                                    String name = jsonResponse.getString("name");

                                    Intent intent = new Intent(LoginActivity.this, UserAreaActivity.class);
                                    intent.putExtra("id", id);
                                    intent.putExtra("pwd", pwd);
                                    intent.putExtra("name", name);

                                    SharedPreferences.Editor prefEditor = sharedPref.edit();
                                    prefEditor.putBoolean("Logined", true);
                                    prefEditor.putString("SID", id);
                                    prefEditor.apply();

                                    LoginActivity.this.startActivity(intent);
                                } else {
                                    AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
                                    builder.setMessage("Login Failed")
                                            .setNegativeButton("Retry", null)
                                            .create()
                                            .show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    };

                    LoginRequest loginRequest = new LoginRequest(id, pwd, responseListener);
                    RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
                    queue.add(loginRequest);
                }else if(Logined == false) {
                    AlertDialog.Builder helpfriend = new AlertDialog.Builder(LoginActivity.this);
                    helpfriend.setMessage("The login ID not same with the previous ID.Trying to help friend sign? Please contact your lecturer.")
                            .setNegativeButton("Close", null)
                            .create()
                            .show();
                }
            };
        });
    }
}

我设置TextView2是因为我想查看共享的Preference值。但似乎没有用。

1 个答案:

答案 0 :(得分:0)

它永远不会进入if块 专注于这条线。

final Boolean Logined = sharedPref.getBoolean("Logined", false);

第一次打开时,默认值为false,因此如果是,则直接进入else。

对于它上面的解决方案更新,如下所示

final int Logined = sharedPref.getInt("Logined", 0);

在btnlogin.setOnClickListeren()

 btnlogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            final String id = etid.getText().toString();
            final String pwd = etpwd.getText().toString();


            if(Logined == 0) {

            //Simply allow user to login and write here code for logging.

            SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("Logined", 1);
                editor.putString("SID", sid);//You have to get sid from somewhere.
                editor.commit();
            }
            else if(Logined == 1)
            {
                if(!sid.equals(sid2))//sid2 is a sid of user who is logging currently
                {
                    AlertDialog.Builder helpfriend = new AlertDialog.Builder(LoginActivity.this);
                helpfriend.setMessage("The login ID not same with the previous ID.Trying to help friend sign? Please contact your lecturer.")
                        .setNegativeButton("Close", null)
                        .create()
                        .show();
               }
               else
               {
                   //allow user for login and write code for logging.
               }
            }