0
我正在處理用戶需要登錄的應用程序,需要我已經存儲在數據庫中的用戶名和密碼,登錄後我將能夠看到輸入的帳戶的信息,但我得到其他帳戶我在登錄後得到不同的用戶數據
登錄類
public class DocLogin extends Fragment {
ImageView ivIcon;
TextView tvItemName, tvRegister;
EditText user, pass;
Button btnLogin;
String username, password;
SQLController dbcon;
SharedPreferences sh_Pref;
Editor editor;
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
user.setSelection(user.length());
username = user.getText().toString();
password = pass.getText().toString();
sharedPreferences();
Toast.makeText(getActivity(), "Login Successful", 20).show();
Log.d("DocLogin", user.getText().toString());
Log.v("LoginDetails", user.getText().toString() + "../.."
+ pass.getText().toString());
Cursor cur = dbcon.getuser_information(user.getText()
.toString(), pass.getText().toString());
if (cur.getCount() != 0) {
Cursor c = dbcon.getUserData();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
DocProfile fragment = new DocProfile();
Bundle bundle = new Bundle();
bundle.putString("RegId", c.getString(1));
bundle.putString("DocCode", c.getString(2));
bundle.putString("Firstname", c.getString(3));
bundle.putString("Lastname", c.getString(4));
bundle.putString("Specialty", c.getString(5));
bundle.putString("CardId", c.getString(6));
bundle.putString("Region", c.getString(7));
bundle.putString("Location", c.getString(8));
bundle.putString("Contact", c.getString(9));
bundle.putString("Monday", c.getString(10));
bundle.putString("Tuesday", c.getString(11));
bundle.putString("Wednesday", c.getString(12));
bundle.putString("Thursday", c.getString(13));
bundle.putString("Friday", c.getString(14));
bundle.putString("Saturday", c.getString(15));
bundle.putString("Sunday", c.getString(16));
fragment.setArguments(bundle);
ft.replace(R.id.content_frame, fragment);
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("Login Credentials", PRIVATE_MODE);
editor = sh_Pref.edit();
editor.putBoolean(IS_LOGIN, true);
editor.putString("Username", username);
editor.putString("Password", password);
editor.commit();
}
}
Profile類
public class DocProfile extends Fragment {
SharedPreferences pref;
Editor editor;
SessionManager session;
SQLiteDatabase db;
SQLController dbcon;
Cursor cursor;
SqlDbHelper dbhelper;
int PRIVATE_MODE = 0;
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);
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 regid = "", doccode = "", first = "", last = "", specialty = "", location = "", contact = "";
Bundle args = getArguments();
if (args != null && args.containsKey("RegId"))
regid = args.getString("RegId");
if (args != null && args.containsKey("DocCode"))
doccode = args.getString("DocCode");
if (args != null && args.containsKey("Firstname"))
first = args.getString("Firstname");
if (args != null && args.containsKey("Lastname"))
last = args.getString("Lastname");
if (args != null && args.containsKey("Specialty"))
specialty = args.getString("Specialty");
if (args != null && args.containsKey("Location"))
location = args.getString("Location");
if (args != null && args.containsKey("Contact"))
contact = args.getString("Contact");
fname.setText(first);
lname.setText(last);
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:
SharedPreferences myPrefs = getActivity().getSharedPreferences("PREF", PRIVATE_MODE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.clear();
editor.commit();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content_frame, new DocLogin());
ft.commit();
Toast.makeText(getActivity(), "Logout Successful", 20).show();
return false;
default:
break;
}
return false;
}
}
DB類
不同的數據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 getUserData() {
String[] allColumns = new String[] { SqlDbHelper._id,
SqlDbHelper.COL_REG_ID, SqlDbHelper.COL_DOC_CODE,
SqlDbHelper.COL_FNAME, SqlDbHelper.COL_LNAME,
SqlDbHelper.COL_SPEC, SqlDbHelper.COL_CARD_ID,
SqlDbHelper.COL_REGION, SqlDbHelper.COL_LOCATION,
SqlDbHelper.COL_CONTACT, SqlDbHelper.SCHED_MON,
SqlDbHelper.SCHED_TUE, SqlDbHelper.SCHED_WED,
SqlDbHelper.SCHED_THU, SqlDbHelper.SCHED_FRI,
SqlDbHelper.SCHED_SAT, SqlDbHelper.SCHED_SUN,
SqlDbHelper.COL_PASS, SqlDbHelper.COL_CONPASS, };
Cursor c = database.query(SqlDbHelper.DATABASE_TABLE, allColumns, null,
null, null, null, null);
if (c != null) {
c.moveToFirst();
}
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;
}
}
public class SqlDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_TABLE = "doc_table";
public static final String DATABASE_NAME = "doc_db";
public static final int DATABASE_VERSION = 1;
public static final String _id = "_id";
public static final String COL_REG_ID = "reg_id";
public static final String COL_DOC_CODE = "doc_code";
public static final String COL_FNAME = "firstname";
public static final String COL_LNAME = "lastname";
public static final String COL_SPEC = "specialty";
public static final String COL_CARD_ID = "card_id";
public static final String COL_REGION = "region";
public static final String COL_LOCATION = "location";
public static final String COL_CONTACT = "contact";
public static final String SCHED_MON = "monday";
public static final String SCHED_TUE = "tuesday";
public static final String SCHED_WED = "wednesday";
public static final String SCHED_THU = "thursday";
public static final String SCHED_FRI = "friday";
public static final String SCHED_SAT = "saturday";
public static final String SCHED_SUN = "sunday";
public static final String COL_PASS = "password";
public static final String COL_CONPASS = "confirm_password";
private static final String CREATE_DATABASE = "CREATE TABLE "
+ DATABASE_TABLE + " (" + _id
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_REG_ID
+ " TEXT NOT NULL, " + COL_DOC_CODE + " TEXT NOT NULL, "
+ COL_FNAME + " TEXT NOT NULL, " + COL_LNAME + " TEXT NOT NULL, "
+ COL_SPEC + " TEXT NOT NULL, " + COL_CARD_ID + " TEXT NOT NULL, "
+ COL_REGION + " TEXT NOT NULL, " + COL_LOCATION
+ " TEXT NOT NULL, " + COL_CONTACT + " TEXT NOT NULL, " + SCHED_MON
+ " TEXT NOT NULL, " + SCHED_TUE + " TEXT NOT NULL, " + SCHED_WED
+ " TEXT NOT NULL, " + SCHED_THU + " TEXT NOT NULL, " + SCHED_FRI
+ " TEXT NOT NULL, " + SCHED_SAT + " TEXT NOT NULL, " + SCHED_SUN
+ " TEXT NOT NULL, " + COL_PASS + " TEXT NOT NULL, " + COL_CONPASS
+ " TEXT NOT NULL);";
public SqlDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}