Guest User

Untitled

a guest
Oct 25th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. public class Tab5TwinOGE extends Fragment{
  2.  
  3. Button btnLogin;
  4. EditText editUsername;
  5. EditText editPassword;
  6. TextView result;
  7. DatabaseHelperTwinIGE databaseHelperTwinIGE;
  8.  
  9.  
  10. @Override
  11. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  12. Bundle savedInstanceState) {
  13. View view = inflater.inflate(R.layout.tab5twinoge, container, false);
  14.  
  15.  
  16. btnLogin = (Button)view.findViewById(R.id.btnAdd);
  17. editUsername = (EditText)view.findViewById(R.id.txtNumber1);
  18. editPassword = (EditText)view.findViewById(R.id.txtNumber2);
  19. result = (TextView)view .findViewById(R.id.txtResult);
  20.  
  21. databaseHelperTwinIGE = new DatabaseHelperTwinIGE(getActivity());
  22.  
  23. btnLogin.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. String weight = databaseHelperTwinIGE.getUserWeigth(editUsername.getText().toString(), editPassword.getText().toString());
  27.  
  28. if (weight != null){
  29. Toast.makeText(getActivity(),"Login Success, weight = " + weight,Toast.LENGTH_SHORT).show();
  30. result.setText(weight);
  31. } else {
  32. editPassword.setText(null);
  33. Toast.makeText(getActivity(),"Login failed. Invalid username or password.",Toast.LENGTH_SHORT).show();
  34. }
  35.  
  36.  
  37. }
  38. });
  39.  
  40.  
  41. return view;
  42.  
  43. }
  44.  
  45. private static final String DATABASE_NAME = "test.db";
  46. private static final int DATABASE_VERSION = 1;
  47. private final Context context;
  48. SQLiteDatabase db;
  49.  
  50. private static final String DATABASE_PATH = "/data/data/com.app.army.tab/databases/";
  51. private final String USER_TABLE = "user";
  52.  
  53.  
  54. public DatabaseHelperTwinIGE(Context context) {
  55. super(context, DATABASE_NAME, null, DATABASE_VERSION);
  56. this.context = context;
  57. createDb();
  58. }
  59.  
  60. @Override
  61. public void onCreate(SQLiteDatabase sqLiteDatabase) {
  62.  
  63. }
  64.  
  65. @Override
  66. public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  67. }
  68.  
  69. public void createDb() {
  70. boolean dbExist = checkDbExist();
  71.  
  72. if (!dbExist) {
  73. this.getReadableDatabase();
  74. copyDatabase();
  75. }
  76. }
  77.  
  78. private boolean checkDbExist() {
  79. SQLiteDatabase sqLiteDatabase = null;
  80.  
  81. try {
  82. String path = DATABASE_PATH + DATABASE_NAME;
  83. sqLiteDatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
  84. } catch (Exception ex) {
  85. }
  86.  
  87.  
  88. if (sqLiteDatabase != null) {
  89. sqLiteDatabase.close();
  90. return true;
  91. }
  92. return false;
  93.  
  94. }
  95.  
  96. private void copyDatabase() {
  97. try {
  98. InputStream inputStream = context.getAssets().open(DATABASE_NAME);
  99.  
  100. String ourFileName = DATABASE_PATH + DATABASE_NAME;
  101.  
  102. OutputStream outputStream = new FileOutputStream(ourFileName);
  103.  
  104. byte[] b = new byte[1024];
  105. int length;
  106.  
  107.  
  108. while ((length = inputStream.read(b)) > 0) {
  109. outputStream.write(b, 0, length);
  110. }
  111.  
  112. outputStream.flush();
  113. outputStream.close();
  114. inputStream.close();
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119.  
  120. private SQLiteDatabase openDatabase() {
  121. String path = DATABASE_PATH + DATABASE_NAME;
  122. db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
  123. return db;
  124. }
  125.  
  126. public void close() {
  127. if (db != null) {
  128. db.close();
  129. }
  130.  
  131. }
  132.  
  133.  
  134. public String getUserWeigth(String username, String password) {
  135. String[] column = {"weight"};
  136. db = openDatabase();
  137.  
  138. String selection = "username=? and password = ?";
  139. String[] selectionArgs = {username, password};
  140.  
  141. String weight = null;
  142.  
  143. Cursor cursor = db.query(USER_TABLE, column, selection, selectionArgs, null, null, null);
  144. if (cursor.moveToFirst()) {
  145. weight = cursor.getString(1);
  146. }
  147. cursor.close();
  148. close();
  149.  
  150.  
  151. return weight;
  152. }
Add Comment
Please, Sign In to add comment