Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Can You Help Me in Debugging this Android Code?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 4.66k
    Answer it

    Can anyone please explain me this code ?

    Final class called ‘UserProfile’. Make the default constructor private. Create an inner class called ‘Users’ by implementing ‘BaseColumn’ interface. Inside the inner class, define the columns you need along with the table.

        import android.provider.BaseColumns;
        
        public final class  UserProfile {
            private UserProfile(){}
        
            public static class  Users implements BaseColumns{
        
                public static final String Table_Name = "UserInfo";
                public static final String Column_Username = "username";
                public static final String Column_dateOfBirth = "dateofBirth";
                public static final String Column_Gender = "Gender";
        
            }
        }

    Create another class called DBHelper inside the database folder by extending the class SQLiteOpenHelper as its superclass. Implement the relevant methods and constructors.
    Database Table Name:    UserInfo
    Column Name:             _ID - Primary Key | username | dateOfBirth | Gender

        public class DBHelper extends SQLiteOpenHelper{
            private static final String DB_NAME = "profiler";
        
            public DBHelper(Context context){
                super(context,DB_NAME,null,1);
            }
        
            @Override
            public void onCreate(SQLiteDatabase db) {
                String sql = "CREATE TABLE " + UserProfile.Users.TABLE_NAME + "("
                        + UserProfile.Users._ID + "INTEGER PRIMARY KEY, "
                        + UserProfile.Users.COLUMN_USERNAME + "TEXT,"
                        + UserProfile.Users.COLUMN_DATEOFBIRTH + "TEXT,"
                        + UserProfile.Users.COLUMN_GENDER + "TEXT)";
                db.execSQL(sql);
            }
        
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        
            }
        }

    Implement a method named addInfo() to store user details details .

        public boolean addInfor(String username, String dob, String gender){
            ContentValues contentValues = new ContentValues();
            contentValues.put(UserProfile.Users.COLUMN_USERNAME, username);
            contentValues.put(UserProfile.Users.COLUMN_DATEOFBIRTH, dob);
            contentValues.put(UserProfile.Users.COLUMN_GENDER, gender);
        
            SQLiteDatabase sqLiteDatabase = getWritableDatabase();
            long result = sqLiteDatabase.insert(UserProfile.Users.TABLE_NAME,null,contentValues);
            sqLiteDatabase.close();
            if (result >=0)
                return true;
            else
                return false;
        }

    Implement a method named updateInfor() to modify stored user details based on the user ID. Method must return a Boolean value based on the success or failure.

        public boolean updateInfor(String username,String dob, String gender){
            ContentValues contentValues = new ContentValues();
            contentValues.put(UserProfile.Users.COLUMN_USERNAME, username);
            contentValues.put(UserProfile.Users.COLUMN_DATEOFBIRTH, dob);
            contentValues.put(UserProfile.Users.COLUMN_GENDER, gender);
        
            SQLiteDatabase sqLiteDatabase = getWritableDatabase();
            String[] selectionArg = {username};
            int rows = sqLiteDatabase.update(UserProfile.Users.TABLE_NAME,contentValues, UserProfile.Users.COLUMN_USERNAME + "= ?",selectionArg);
            sqLiteDatabase.close();
        
            if (rows >= 0)
                return true;
            else
                return false;
        }

    Implement a method named readAllInfor() to retrieve all the user details stored in the database table.

        public List readAllInfor(){
            List users = new ArrayList<>();
        
            SQLiteDatabase sqLiteDatabase = getReadableDatabase();
            Cursor cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+ UserProfile.Users.TABLE_NAME,null);
            while (cursor.moveToNext()){
                String value = cursor.getString(cursor.getColumnIndexOrThrow(UserProfile.Users.COLUMN_USERNAME));
                users.add(value);
            }
            sqLiteDatabase.close();
            return users;
        }

    Overload the method readAllInfor() to retrieve the user details based on the primary key.

        public List readAllInfor(int ID){
            List users = new ArrayList<>();
            SQLiteDatabase sqLiteDatabase = getReadableDatabase();
            String[] selection = { "WHERE " + UserProfile.Users._ID + "=" + ID};
            Cursor cursor = sqLiteDatabase.rawQuery("SELECT *  FROM "+ UserProfile.Users.TABLE_NAME, selection);
            if (cursor.moveToNext()) {
                users.add(cursor.getString(1));
            }while(cursor.moveToNext());
            sqLiteDatabase.close();
            return users;
        }

    Implement a method named deleteInfo() to delete a specific user

        public void deleteInfo(String username){
          SQLiteDatabase sqLiteDatabase = getWritableDatabase();
          String sql = UserProfile.Users.COLUMN_USERNAME + "LIKE ?";
          String[] selection = {username};
          sqLiteDatabase.delete(UserProfile.Users.TABLE_NAME,sql,selection);
          sqLiteDatabase.close();
        }

    Create intent objects to start
    ProfileManagement activity from ‘Register’ button

        public void register(View view){
        
            Intent intent = new Intent(Home.this, ProfileManagement.class);
            startActivity(intent);
        }

    EditProfile activity from ‘Update Profile’ button

        public void editProfile(View view){
            Intent intent = new Intent(ProfileManagement.this, EditProfile.class);
            startActivity(intent);
        }

    Call addInfor() method implemented in DbHandler class from the onClick event of Register button. Display a Toast Message indicating success or failure.

        public class ProfileManagement extends AppCompatActivity {
        
            private EditText username;
            private EditText dob;
            private RadioGroup radGroup;
            private RadioButton radbtn;
        
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_profile_management);
        
                username = (EditText) findViewById(R.id.editText3);
                dob = (EditText) findViewById(R.id.editText4);
                radGroup = (RadioGroup) findViewById(R.id.radG);
            }
        
            public void editProfile(View view){
                Intent intent = new Intent(ProfileManagement.this, EditProfile.class);
                startActivity(intent);
            }
        
            public void registerUser(View view){
        
                int radioButtonId = radGroup.getCheckedRadioButtonId();
                radbtn = (RadioButton) findViewById(radioButtonId);
        
                DBHelper dbHelper = new DBHelper(getBaseContext());
                boolean value = dbHelper.addInfo(username.getText().toString(), dob.getText().toString(), radbtn.getText().toString());
        
                if(value == true)
                    Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_SHORT).show();
            }
        }

    Call readAllInfor() method implemented in DbHandler class from the onClick event of the Search button to retrieve profile of a specific user.

        public class EditProfile extends AppCompatActivity {
        
            private TextView showBox;
            private EditText username;
            private EditText dob;
            private RadioGroup rg5;
            private RadioButton radiobutton;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_edit_profile);
                showBox = (TextView) findViewById(R.id.showBox);
                username = (EditText) findViewById(R.id.editText3un);
                dob = (EditText) findViewById(R.id.editTextdb);
                rg5 = (RadioGroup) findViewById(R.id.rad66);
            }
        
            @RequiresApi(api = Build.VERSION_CODES.O)
            public void show(View view){
        
                    DBHelper dbHelper = new DBHelper(getBaseContext());
                    List userList = dbHelper.readAllInfo();
                    String userString = String.join("\n", userList);
                    showBox.setText(userString);
        
            }
        
            public void delete(View view){
                DBHelper dbHelper = new DBHelper(getBaseContext());
                dbHelper.deleteInfo(username.getText().toString());
            }
        
            public void edit(View view){
                        int radId = rg5.getCheckedRadioButtonId();
                        radiobutton = (RadioButton) findViewById(radId);
        
                        DBHelper dbHelper = new DBHelper(getBaseContext());
                        dbHelper.updateInfo(username.getText().toString(), dob.getText().toString(), radiobutton.getText().toString());
        
            }
        }

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: