데이터베이스 사용자 로그인

제목:
1. 프로젝트 작성(10점)
2. 로그인 페이지 구현(10점)
3. 데이터베이스 저장 실현(10점)
4. 테이블 만들기 (10점)
5. 첨삭 실현(10점)
6. 각종 조건에 따라 조회(10점)
7. 조회한 데이터를listview에 보여주기(10점)
8. Notitfcation 생성(10점)
9. 첨삭에 성공하면 Notitfcation 알림 보내기(10점)
10. 주석(10점)
코드:
레이아웃 코드:
activity_main.xml 파일:
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
   >

            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:orientation="horizontal">

                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp" />

                    android:id="@+id/userName_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="      " />

    

            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:orientation="horizontal">

                    android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textSize="20sp" />

                    android:id="@+id/userPassword_et"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="     " />

    

    

activity_home.xml 파일:
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >



item_layout.xml 파일:
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

            android:id="@+id/name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:textColor="@android:color/black"
        android:textSize="20sp" />

            android:id="@+id/password_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginBottom="10dp"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

dialog_layout.xml 파일:
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

            android:id="@+id/dialog_name_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="      " />

            android:id="@+id/dialog_password_et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:hint="     " />

            android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp">

        

Java 코드:
Config:
public class Config {
    //      
    public static final String DB_NAME = "user.db";
    //        
    public static final String TABLE_NAME = "user";
    //   
    public static final int DB_VERSION = 1;
}

SQLiteHelper:
public class SQLiteHelper extends SQLiteOpenHelper {

    public SQLiteHelper(Context context ) {
        super(context, Config.DB_NAME, null, Config.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists user(_id integer primary key autoincrement,name varchar(20),password varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

DBUtils:
public class DBUtils {
    private SQLiteHelper helper;

    private static DBUtils dbUtils;

    private DBUtils(Context context) {
        helper = new SQLiteHelper(context);
    }

    /**
     *   
     *
     * @param context
     * @return
     */
    public static synchronized DBUtils getDButils(Context context) {
        if (dbUtils == null) {
            dbUtils = new DBUtils(context);
        }
        return dbUtils;
    }
    
    /**
     *          
     *
     * @param name
     * @param password
     * @return
     */
    public boolean insertUser(String name, String password) {
        SQLiteDatabase db = helper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("password", password);
        return db.insert(Config.TABLE_NAME, null, values) > 0;
    }

    /**
     *          
     *
     * @return   
     */
    public Cursor getAllUser() {
        SQLiteDatabase db = helper.getReadableDatabase();
        return db.rawQuery("select * from user", null);
    }

    /**
     *     
     *
     * @param name
     * @return
     */
    public boolean deleteUser(String name) {
        SQLiteDatabase db = helper.getReadableDatabase();
        return db.delete(Config.TABLE_NAME, "name = ?", new String[]{name}) > 0;
    }

    /**
     *     
     *
     * @param name
     * @param password
     * @return
     */
    public boolean upDataUser(String name, String password) {
        SQLiteDatabase db = helper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("password", password);
        return db.update(Config.TABLE_NAME, values, "name = ?", new String[]{name}) > 0;
    }

}

NotificationManager:
public class NotificationManager {

    //            
    public static void sendNotification(Context context,String info){
        android.app.NotificationManager manager = (android.app.NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification.Builder  builder = new Notification.Builder(context);
        builder.setContentTitle("  ");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentText(info);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            manager.notify(10,builder.build());
        }

    }
}

MainActivity:
public class MainActivity extends AppCompatActivity {

    private EditText userNameEt;
    private EditText userPasswordEt;
    private DBUtils dbUtils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbUtils = DBUtils.getDButils(this);

        userNameEt = (EditText) findViewById(R.id.userName_et);
        userPasswordEt = (EditText) findViewById(R.id.userPassword_et);

        //                  
        Cursor allUser = dbUtils.getAllUser();
        if (allUser!=null&&allUser.getCount()>0){
            Intent intent = new Intent(this, HomeActivity.class);
            startActivity(intent);
        }
    }

    public void onClick(View view) {
        //                       
        String userName = userNameEt.getText().toString();
        String userPassword = userPasswordEt.getText().toString();
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(userPassword)) {
            Toast.makeText(this, "         ", Toast.LENGTH_SHORT).show();
            return;
        }
        //           
        boolean isSuccess = dbUtils.insertUser(userName, userPassword);

        if (isSuccess) {
            //      
            NotificationManager.sendNotification(this, "");
            Toast.makeText(this, "      ", Toast.LENGTH_SHORT).show();

        }
        //      
        Intent intent = new Intent(this, HomeActivity.class);
        startActivity(intent);

    }
}

HomeActivity:
public class HomeActivity extends AppCompatActivity {

    private ListView listView;
    private DBUtils dButils;
    private SimpleCursorAdapter adapter;
    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empty);
        listView = (ListView) findViewById(R.id.listView);
        dButils = DBUtils.getDButils(this);
        Cursor allUser = dButils.getAllUser();
        adapter = new SimpleCursorAdapter(this, R.layout.item_layout, allUser, new String[]{"name", "password"}, new int[]{R.id.name_tv, R.id.password_tv}, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listView.setAdapter(adapter);
        // listView        
        registerForContextMenu(listView);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1, 1, 0, "    ");
        menu.add(1, 2, 0, "    ");
        menu.add(1, 3, 0, "    ");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int position = menuInfo.position;

        switch (item.getItemId()) {
            case 1:
                //    
                showDialog(true);
                break;
            case 2:
                //    
                Cursor cursor = (Cursor) adapter.getItem(position);
                String name = cursor.getString(cursor.getColumnIndex("name"));
                boolean isSuccess = dButils.deleteUser(name);
                if (isSuccess) {
                    Toast.makeText(this, "    ", Toast.LENGTH_SHORT).show();
                    NotificationManager.sendNotification(this, "      ");
                }
                //    
                adapter.changeCursor(dButils.getAllUser());
                break;
            case 3:
                //    
                showDialog(false);
                break;
        }
        return super.onContextItemSelected(item);

    }

    public void showDialog(final boolean isInsert) {

        View view = LayoutInflater.from(this).inflate(R.layout.dialog_layout, null);
        final EditText nameEt = (EditText) view.findViewById(R.id.dialog_name_et);
        final EditText passwordEt = (EditText) view.findViewById(R.id.dialog_password_et);


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        if (isInsert) {
            builder.setTitle("    ");
        } else {
            builder.setTitle("    ");
        }
        builder.setIcon(R.mipmap.ic_launcher);


        //    
        view.findViewById(R.id.bt_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        //    
        view.findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameEt.getText().toString();
                String password = passwordEt.getText().toString();
                boolean isSuccess;
                if (isInsert) {
                    isSuccess = dButils.insertUser(name, password);
                } else {
                    isSuccess = dButils.upDataUser(name, password);
                }
                if (isInsert) {
                    Toast.makeText(HomeActivity.this, "    ", Toast.LENGTH_SHORT).show();
                    NotificationManager.sendNotification(HomeActivity.this, "        ");
                } else {
                    Toast.makeText(HomeActivity.this, "    ", Toast.LENGTH_SHORT).show();
                    NotificationManager.sendNotification(HomeActivity.this, "        ");
                }

                //    
                adapter.changeCursor(dButils.getAllUser());
                dialog.dismiss();

            }
        });

        builder.setView(view);
        dialog = builder.show();
    }
}

좋은 웹페이지 즐겨찾기