注销SharedPreferences后未清除用户数据

时间:2014-07-13 06:59:05

标签: android sqlite sharedpreferences

我的问题是每次我登录数据都停留在应用程序中,即使我点击退出并尝试登录其他帐户仍然获取我首先登录的帐户的数据..

我在注销后做了Log.d并且数据仍在那里

这是我的代码

用户登录类

public class DocLogin extends Fragment {

TextView tvItemName, tvRegister;
EditText user, pass;
Button btnLogin;
String username, password;
SqlDbHelper dbhelper;
SQLController dbcon;
SharedPreferences sh_Pref;
Editor editor;

public static final String MyPREFERENCES = "MyPrefs";
public static final String userkey = "userKey";
public static final String passkey = "passKey";
int PRIVATE_MODE = 0;

public DocLogin() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.doc_log_in, container, false);

    dbcon = new SQLController(getActivity());
    dbcon.open();

    // Log in
    user = (EditText) view.findViewById(R.id.etUser);
    pass = (EditText) view.findViewById(R.id.etPassword);
    btnLogin = (Button) view.findViewById(R.id.btnLogin);

    // Sign up
    tvRegister = (TextView) view.findViewById(R.id.tvRegDoc);
    tvRegister.setMovementMethod(LinkMovementMethod.getInstance());
    tvRegister.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            FragmentManager fm = getFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.content_frame, new DocReg());
            ft.commit();
            return false;
        }
    });

    // btnLogin onClickListener
    btnLogin.setOnClickListener(new OnClickListener() {

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

            sharedPreferences();
            Toast.makeText(getActivity(), "Login Successful", 20).show();

            Log.d("DocLogin", user.getText().toString());
            Log.d("LoginDetails", "username:" + user.getText());
            Log.d("LoginDetails", "password:" + pass.getText());
            Cursor cur = dbcon.getuser_information(user.getText()
                    .toString(), pass.getText().toString());
            if (cur.getCount() != 0) {

                Cursor cursor = dbcon.getDocData();
                if(cursor.getCount() !=0) {
                    FragmentManager fm = getFragmentManager();
                    FragmentTransaction ft = fm.beginTransaction();
                    ft.replace(R.id.content_frame, new DocProfile());
                    ft.commit();
                }
            } else {
                AlertDialog alertDialog = new AlertDialog.Builder(
                        getActivity()).create();
                alertDialog.setTitle("Login Error");
                alertDialog
                        .setMessage("Doctor Code and Password does not match");
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // TODO Auto-generated method stub
                                // dismiss dialog
                            }
                        });
                alertDialog.show();
            }

        }
    });

    return view;
}

public void sharedPreferences() {

    sh_Pref = getActivity().getSharedPreferences(MyPREFERENCES,
            getActivity().MODE_PRIVATE);
    editor = sh_Pref.edit();
    username = user.getText().toString();
    password = pass.getText().toString();
    editor.putString("Username", username);
    editor.putString("Password", password);
    editor.commit();
}

}

这是我使用TextView

显示用户数据的地方
public class DocProfile extends Fragment {

SharedPreferences pref;
Editor editor;
SQLiteDatabase db;
SQLController dbcon;
Cursor cursor;
SqlDbHelper dbhelper;

public DocProfile() {

}

public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.doc_profile, container, false);


    dbcon = new SQLController(getActivity());
    dbcon.open();

    TextView fname = (TextView) view.findViewById(R.id.docFirstname);
    TextView lname = (TextView) view.findViewById(R.id.docLastname);
    TextView spec = (TextView) view.findViewById(R.id.doc_spec);
    TextView loc = (TextView) view.findViewById(R.id.doc_location);
    TextView con = (TextView) view.findViewById(R.id.doc_contact);


    String doccode = "", firstname = "", lastname = "", specialty = "", location = "", contact = "";

    Cursor c = dbcon.getDocData();
    doccode = c.getString(c.getColumnIndex(SqlDbHelper.COL_DOC_CODE));
    Log.d("DocProfile", "DocCode: " + c.getString(c.getColumnIndex(SqlDbHelper.COL_DOC_CODE)));
    firstname = c.getString(c.getColumnIndex(SqlDbHelper.COL_FNAME));
    Log.d("DocProfile", "DocCode: " + c.getString(c.getColumnIndex(SqlDbHelper.COL_FNAME)));
    lastname = c.getString(c.getColumnIndex(SqlDbHelper.COL_LNAME));
    Log.d("DocProfile", "DocCode: " + c.getString(c.getColumnIndex(SqlDbHelper.COL_LNAME)));
    specialty = c.getString(c.getColumnIndex(SqlDbHelper.COL_SPEC));
    Log.d("DocProfile", "DocCode: " + c.getString(c.getColumnIndex(SqlDbHelper.COL_SPEC)));
    location = c.getString(c.getColumnIndex(SqlDbHelper.COL_LOCATION));
    Log.d("DocProfile", "DocCode: " + c.getString(c.getColumnIndex(SqlDbHelper.COL_LOCATION)));
    contact = c.getString(c.getColumnIndex(SqlDbHelper.COL_CONTACT));
    Log.d("DocProfile", "DocCode: " + c.getString(c.getColumnIndex(SqlDbHelper.COL_CONTACT)));

    fname.setText(firstname);
    lname.setText(lastname);
    spec.setText(specialty);
    loc.setText(location);
    con.setText(contact);

    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    getActivity().getMenuInflater().inflate(R.menu.logout, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.logout:

        TextView fname = (TextView) getActivity().findViewById(R.id.docFirstname);
        TextView lname = (TextView) getActivity().findViewById(R.id.docLastname);
        TextView spec = (TextView) getActivity().findViewById(R.id.doc_spec);
        TextView loc = (TextView) getActivity().findViewById(R.id.doc_location);
        TextView con = (TextView) getActivity().findViewById(R.id.doc_contact);

        String vFname = fname.getText().toString();
        String vLname = lname.getText().toString();
        String vSpec = spec.getText().toString();
        String vLoc = loc.getText().toString();
        String vCon = con.getText().toString();

        SharedPreferences prefs = getActivity().getSharedPreferences(DocLogin.MyPREFERENCES, getActivity().MODE_PRIVATE);
        Editor editor = prefs.edit();
        editor.clear();
        editor.commit();
        getActivity().finish();

        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.content_frame, new DocLogin());
        ft.commit();

        Log.d("Logout", "Firstname = null: " + vFname);
        Log.d("Logout", "Lastname = null: " + vLname);
        Log.d("Logout", "Specialty = null: " + vSpec);
        Log.d("Logout", "Location = null: " + vLoc);
        Log.d("Logout", "Contact = null: " + vCon);


        Toast.makeText(getActivity(), "Logout Successful", 20).show();
        return false;
    default:
        break;
    }
    return false;
}

}

我的数据库处理程序

public class SQLController {

private SqlDbHelper dbhelper;
private Context context;
private SQLiteDatabase database;

public SQLController(Context c) {
    context = c;
}

public SQLController open() throws SQLException {
    dbhelper = new SqlDbHelper(context);
    database = dbhelper.getWritableDatabase();
    return this;
}

public void close() {
    dbhelper.close();
}

public void insertData(String regid, String doc_code, String firstname,
        String lastname, String specialty, String card_id, String region,
        String location, String contact, String monday, String tuesday,
        String wednesday, String thursday, String friday, String saturday,
        String sunday, String pass, String conpass) {
    ContentValues cv = new ContentValues();
    cv.put(SqlDbHelper.COL_REG_ID, regid);
    cv.put(SqlDbHelper.COL_DOC_CODE, doc_code);
    cv.put(SqlDbHelper.COL_FNAME, firstname);
    cv.put(SqlDbHelper.COL_LNAME, lastname);
    cv.put(SqlDbHelper.COL_SPEC, specialty);
    cv.put(SqlDbHelper.COL_CARD_ID, card_id);
    cv.put(SqlDbHelper.COL_REGION, region);
    cv.put(SqlDbHelper.COL_LOCATION, location);
    cv.put(SqlDbHelper.COL_CONTACT, contact);
    cv.put(SqlDbHelper.SCHED_MON, monday);
    cv.put(SqlDbHelper.SCHED_TUE, tuesday);
    cv.put(SqlDbHelper.SCHED_WED, wednesday);
    cv.put(SqlDbHelper.SCHED_THU, thursday);
    cv.put(SqlDbHelper.SCHED_FRI, friday);
    cv.put(SqlDbHelper.SCHED_SAT, saturday);
    cv.put(SqlDbHelper.SCHED_SUN, sunday);
    cv.put(SqlDbHelper.COL_PASS, pass);
    cv.put(SqlDbHelper.COL_CONPASS, conpass);
    database.insert(SqlDbHelper.DATABASE_TABLE, null, cv);
}


public Cursor getDocData() {
    String[] docData = new String[] { SqlDbHelper.COL_DOC_CODE,
            SqlDbHelper.COL_FNAME, SqlDbHelper.COL_LNAME,
            SqlDbHelper.COL_SPEC, SqlDbHelper.COL_LOCATION,
            SqlDbHelper.COL_CONTACT };
    Cursor c = database.query(SqlDbHelper.DATABASE_TABLE, docData, null,
            null, null, null, null);

    if (c != null) {
        c.moveToFirst();
         String getDocCode = c.getString(c.getColumnIndex(SqlDbHelper.COL_DOC_CODE));
         String getFname = c.getString(c.getColumnIndex(SqlDbHelper.COL_FNAME));
         String getLname = c.getString(c.getColumnIndex(SqlDbHelper.COL_LNAME));
         String getSpec = c.getString(c.getColumnIndex(SqlDbHelper.COL_SPEC));
         String getLocation = c.getString(c.getColumnIndex(SqlDbHelper.COL_LOCATION));
         String getContact = c.getString(c.getColumnIndex(SqlDbHelper.COL_CONTACT));
    }
    return c;
}


public Cursor getuser_information(String docid, String password) {
    Cursor cursor = database.query(true, SqlDbHelper.DATABASE_TABLE,
            new String[] { SqlDbHelper.COL_REG_ID,
                    SqlDbHelper.COL_DOC_CODE, SqlDbHelper.COL_PASS },
            SqlDbHelper.COL_DOC_CODE + "='"
                    + docid.toString().toLowerCase() + "' and "
                    + SqlDbHelper.COL_PASS + "='" + password + "'", null,
            null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        return cursor;
    } else
        return cursor;

}

}

1 个答案:

答案 0 :(得分:0)

清晰的方法非常有效。问题是你没有更新log.d中的变量。清除并提交后,您需要再次从共享首选项中读取数据。