1. InternalStorage


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="org.android.soldesk.internalstorage_01.InternalStorage_01Activity"
android:weightSum="1">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editInput"
android:ems="10"
>
<requestFocus/>

</EditText>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:text="저장~~~"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnShow"
android:text="보여주세요"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:background="#88ff0000"
android:text="저장 데이터 보여주기"
/>



</LinearLayout>

<xml>


package org.android.soldesk.internalstorage_01;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class InternalStorage_01Activity extends AppCompatActivity {

EditText editInput;
Button btnSave;
Button btnShow;
TextView txtResult;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_internal_storage_01);

editInput = (EditText)findViewById(R.id.editInput);
btnSave = (Button) findViewById(R.id.btnSave);
btnShow = (Button) findViewById(R.id.btnShow);
txtResult = (TextView) findViewById(R.id.txtResult);

// 화일 쓰기

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String data = editInput.getText().toString();
try {
FileOutputStream fos = openFileOutput("korea01.txt", Context.MODE_APPEND);
PrintWriter out = new PrintWriter(fos);

out.println(data);
out.close();


}catch (Exception e)
{
e.printStackTrace();
}

// 가상 키보드 제거

InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
}
});

// 화일 읽기

btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

try {
// 데이타 누적

StringBuffer data = new StringBuffer();

FileInputStream fis = openFileInput("korea01.txt");

BufferedReader buffer = new BufferedReader(new InputStreamReader(fis));
String str = buffer.readLine();

while (str != null) {
data.append(str + "\n");
str = buffer.readLine();
}

txtResult.setText(data.toString() + "\n");
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

<java>




2. ExternalStorage


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="org.android.soldesk.externalstorage_01.ExternalStorage_01Activity">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editInput"
android:ems="10"
>
<requestFocus/>
</EditText>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:text="저장~~~"
/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnShow"
android:text="보여줘~~~~"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/txtResult"
android:background="#9977aa77"
android:text="저장 데이타 보여주기"
/>

</LinearLayout>

<xml>


package org.android.soldesk.externalstorage_01;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

public class ExternalStorage_01Activity extends AppCompatActivity {

EditText editInput;
Button btnSave;
Button btnShow;
TextView txtResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_external_storage_01);

editInput = (EditText) findViewById(R.id.editInput);
btnSave = (Button) findViewById(R.id.btnSave);
btnShow = (Button) findViewById(R.id.btnShow);
txtResult = (TextView) findViewById(R.id.txtResult);

// 화일 쓰기

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String data = editInput.getText().toString();

try{
// pictures 폴더에 화일 저장
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

File ff = new File(path,"seoul01.txt");

FileWriter writer = new FileWriter(ff,true);// true : 데이타 추가 가능
PrintWriter out = new PrintWriter(writer);
out.println(data);
out.close();

}catch (Exception e)
{
e.printStackTrace();
}
}
});

// 화일 읽기

btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {

// 데이타 누적용

StringBuffer data = new StringBuffer();

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File ff = new File(path,"seoul01.txt");

BufferedReader buffer = new BufferedReader(new FileReader(ff));

String str = buffer.readLine();

while (str != null)
{
data.append(str+"\n");
str = buffer.readLine();
}

txtResult.setText(data);
buffer.close();

}catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}

<java>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.android.soldesk.externalstorage_01">

<!-- 외부 저장소 권한 설정 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ExternalStorage_01Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

<AndroidManifest.xml>




3. Sqlite_Data_01


package org.android.soldesk.sqlite_data_01;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Sqlite_Data_01Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite__data_01);

//1. 데이터베이스 open
MySQLiteHandler handler =
MySQLiteHandler.open(getApplicationContext());

//2. 데이터 저장
handler.insert("홍길동2", 27, "서울");
handler.insert("홍길동1", 18, "제주");
handler.insert("이순신2", 45, "전라");
handler.insert("이순신1", 33, "광주");
handler.insert("이순신3", 58, "부산");

//3. 데이터 수정
handler.update("홍길동2", 777);

//4. 데이터 삭제
handler.delete("이순신3");

//5. 데이터 조회
handler.select();

//6. 데이터베이스 close
handler.close();

}
}

<java>


package org.android.soldesk.sqlite_data_01;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
* Created by soldesk on 2017-03-30.
*/

public class MySQLiteHandler {

MySQLiteOpenHandler helper;
SQLiteDatabase db;

public MySQLiteHandler(Context ctx){
helper = new MySQLiteOpenHandler(ctx, "person6.db", null, 1);
// MySQLiteOpenHandler(ctx, "person6.db", null, 1);
//
}

// 데이터베이스open
public static MySQLiteHandler open(Context ctx){
return new MySQLiteHandler(ctx);
}

// 데이터베이스close
public void close() { helper.close();}

// 데이터저장
public void insert(String name, int age, String address){
db = helper.getWritableDatabase();

ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
values.put("address", address);
db.insert("student_5", null, values);
}

//데이터수정
public void update(String name , int newAge){

db = helper.getWritableDatabase();

ContentValues values = new ContentValues();
values.put("age", newAge);
db.update("student_5", values, "name = ?", new String[]{name});
}

//데이터 삭제
public void delete(String name){
db = helper.getWritableDatabase();

db.delete("student_5", "name = ?", new String[]{name});
}
//데이터조회
public void select(){
db = helper.getReadableDatabase();

Cursor c = db.query("student_5", null, null, null, null, null, null);
//query("student_5", 1null, 2null, 3null, 4null, 5null, 6null)
//table name = student_5
//1 : columns => select 절에 지정할 컬럼이름 모든 컬럼일 경우 null
//2 : selection : where 절에 들어가는 조건식
//3 : selectionArgs : selection 에 들어갈 데이타를 배열로 지정
//4 : groupBy : select 문의 groupBy 절을 지정
//5 : having : select 문의 having 절을 지정한다.
//6 : orderBy : 정렬할 컬럼을 지정한다.

while (c.moveToNext()){
int _id = c.getInt(c.getColumnIndex("_id"));
String name = c.getString(c.getColumnIndex("name"));
int age = c.getInt(c.getColumnIndex("age"));
String address = c.getString(c.getColumnIndex("address"));
Log.i("MyTag", _id + " " + name + " " + age + " " + address);
}
}
}

<MySQLiteHandler.java>


package org.android.soldesk.sqlite_data_01;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by soldesk on 2017-03-30.
*/

public class MySQLiteOpenHandler extends SQLiteOpenHelper{

public MySQLiteOpenHandler(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version){
super(context, name, factory, version);
}
// 테이블 생성 코드
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table student_5 ( _id integer primary key" +
" autoincrement , "+
" name text, age integer , address text )";
db.execSQL(sql);
}
// 테이블 삭제 코드
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "drop table if exists student_5";
db.execSQL(sql);

onCreate(db);
}
}

<MySQLiteOpenHandler.java>




4. Mysql_01Activity


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="org.android.soldesk.mysql_01.Mysql_01Activity">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView1">

</ListView>

</LinearLayout>

<xml>


package org.android.soldesk.mysql_01;

import android.content.DialogInterface;
import android.database.Cursor;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Mysql_01Activity extends AppCompatActivity {

ListView listView;
MySQLiteHandler handler;
Cursor c;
SimpleCursorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mysql_01);


listView = (ListView) findViewById(R.id.listView1);

handler = MySQLiteHandler.open(getApplicationContext());

c = handler.selectAll();
adapter =
new SimpleCursorAdapter(
getApplicationContext(),
R.layout.list_row,
c,
new String[]{"_id", "name", "age", "address"},
new int[]{R.id.txtId, R.id.txtName, R.id.txtAge, R.id.txtAddress},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

listView.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

//메뉴 아이템의 이벤트 처리

switch (item.getItemId()) {

case R.id.addMenu:

LayoutInflater inf = getLayoutInflater();
View convertView = inf.inflate(R.layout.studentadd, null);

final EditText editName =
(EditText) convertView.findViewById(R.id.editName);
final EditText editAddress =
(EditText) convertView.findViewById(R.id.editAddress);
final EditText editAge =
(EditText) convertView.findViewById(R.id.editAge);

AlertDialog.Builder builder =
new AlertDialog.Builder(Mysql_01Activity.this);
builder.setTitle("학생등록");
builder.setIcon(android.R.drawable.stat_sys_warning);
builder.setView(convertView);

builder.setPositiveButton("확인",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = editName.getText().toString();
String address = editAddress.getText().toString();
String age = editAge.getText().toString();

handler.insert(name, Integer.parseInt(age), address);

//추가내용 다시 가져오기 및 ListView에 변경사항 적용
c.requery();
adapter.notifyDataSetChanged();

}
});
builder.setNegativeButton("취소", null);
builder.show();

break;

case R.id.loginMenu:

Log.i("MyTag", "로그인 메뉴선택");
break;
case R.id.preferMenu:
Log.i("MyTag", "환경설정 메뉴선택");
break;
}
return super.onOptionsItemSelected(item);
}
}

<java>


<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout01"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow
android:id="@+id/TableRow01"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="이름:" />

<EditText
android:id="@+id/editName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text=""/>
</TableRow>

<TableRow
android:id="@+id/TableRow02"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="주소:" />

<EditText
android:id="@+id/editAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text=""/>
</TableRow>

<TableRow
android:id="@+id/TableRow03"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text="나이:" />

<EditText
android:id="@+id/editAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:text=""/>
</TableRow>

</TableLayout>

<studentadd.xml>


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/addMenu"
android:title="학생등록"
android:icon="@android:drawable/ic_input_add"/>
<item
android:id="@+id/loginMenu"
android:title="로그인"
/>
<item
android:id="@+id/preferMenu"
android:title="환경설정"
/>
</menu>

<mymenu.xml>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:background="#80ddaa33"
android:orientation="horizontal">

<TextView
android:id="@+id/txtId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="id값"
android:textSize="18pt" />

<TextView
android:id="@+id/txtName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="이름"
android:textSize="14pt" />

<TextView
android:id="@+id/txtAge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="나이"
android:textSize="14pt" />

<TextView
android:id="@+id/txtAddress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="주소"
android:textSize="14pt" />

</LinearLayout>

<list_row.xml>


package org.android.soldesk.mysql_01;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
* Created by soldesk on 2017-03-30.
*/

public class MySQLiteHandler {

MySQLiteOpenHandler helper;
SQLiteDatabase db;

public MySQLiteHandler(Context ctx){
helper = new MySQLiteOpenHandler(ctx, "person.sqlite", null, 1);
}
// 데이터베이스open
public static MySQLiteHandler open(Context ctx){
return new MySQLiteHandler(ctx);
}

// 데이터베이스close
public void close() { helper.close();}

//데이터저장
public void insert(String name, int age, String address){
db = helper.getWritableDatabase();

ContentValues values = new ContentValues();
values.put("name", name);
values.put("age", age);
values.put("address", address);

db.insert("student_7", null, values);
}

//데이터수정
public void update(String id, String name, String address, String age){

db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("address", address);
values.put("age", age);

db.update("student_7", values, "_id = ?", new String[]{id});
}

//데이터삭제
public void delete(String id){
db = helper.getWritableDatabase();
db.delete("student_7", " _id = ? ", new String[]{id});
}

// 전체 데이터조회
public Cursor selectAll(){

db = helper.getReadableDatabase();

Cursor c = db.query("student_7", null, null, null, null, null, null);
return c;
}

//_id 일치데이터얻기
public Cursor selectById(String _id){

db = helper.getReadableDatabase();
Cursor c =
db.query("student_7", null, " _id = ?", new String[]{ _id}, null, null, null);
return c;
}
}

<MySQLiteHandler.java>


package org.android.soldesk.mysql_01;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by soldesk on 2017-03-30.
*/

public class MySQLiteOpenHandler extends SQLiteOpenHelper{

//생성자
public MySQLiteOpenHandler(Context context, String name,
SQLiteDatabase.CursorFactory factory, int version){
super(context, name, factory, version);
}

//테이블생성코드
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table student_7 ( _id integer primary key autoincrement," +
"name text, age integer, address text)";
db.execSQL(sql);
}

//테이블삭제코드
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

String sql = "drop table if exists student_7";
db.execSQL(sql);

onCreate(db);
}
}

<MySQLiteOpenHandler.java>


+ Recent posts