<file_sep>package kay.kookmin.ac.kr.term_project;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.DecimalFormat;

public class ResultNumActivity extends AppCompatActivity {
    TextView resultView;
    TextView countResultView;
    Button bt_job;
    Button bt_hobby;
    Button bt_etc;
    Button bt_all;
    DecimalFormat df = new DecimalFormat("0.##");

    // Database 관련 객체들
    SQLiteDatabase db;
    String dbName = "eventData.db";
    String tableName = "eventDataTable";
    int dbMode = Context.MODE_PRIVATE;

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

        countResultView = (TextView)findViewById(R.id.countResult);
        resultView = (TextView)findViewById(R.id.resultView);

        // Database 생성 및 열기
        db = openOrCreateDatabase(dbName,dbMode,null);

        countResultView.setText(resultCount());
        resultView.setText(selectAll());

        Button bt_back = (Button)findViewById(R.id.back);
        bt_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        bt_job = (Button)findViewById(R.id.job);
        bt_job.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultView.setText(selectJob());
            }
        });

        bt_hobby = (Button)findViewById(R.id.hobby);
        bt_hobby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultView.setText(selectHobby());
            }
        });

        bt_etc = (Button)findViewById(R.id.etc);
        bt_etc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultView.setText(selectEtc());
            }
        });

        bt_all = (Button) findViewById(R.id.all);
        bt_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resultView.setText(selectAll());
            }
        });
    }
    // Data Count 읽기
    public String resultCount() {
        String sql = "select * from " + tableName + ";";
        Cursor results = db.rawQuery(sql, null);
        results.moveToFirst();
        int[] typeCount = new int[3];
        String view = "총 횟수\n";

        while(!results.isAfterLast()) {
            String type = results.getString(1);
            if(type.equals("일"))
                typeCount[0]++;
            else if(type.equals("취미"))
                typeCount[1]++;
            else
                typeCount[2]++;

            results.moveToNext();
        }

        int typeSum = typeCount[0] + typeCount[1] + typeCount[2];

        view += "일 : " + typeCount[0] + "\n";
        view += "취미 : " + typeCount[1] + "\n";
        view += "기타 : " + typeCount[2] + "\n";
        view += "전체 : " + typeSum + "\n";

        results.close();

        return view;
    }

    // 모든 Data 읽기
    public String selectAll() {
        String sql = "select * from " + tableName + ";";
        Cursor results = db.rawQuery(sql, null);
        results.moveToFirst();

        String view = "사건\t\t시간\t\t위치\t\t내용\n";

        String[] location = results.getString(3).split(",");

        while (!results.isAfterLast()) {
            for(int i=1; i<=4; i++) {
                if(i == 3)
                    view += df.format(Double.parseDouble(location[0])) + " " +
                            df.format(Double.parseDouble(location[1])) + "\t\t";
                else
                    view += results.getString(i) + "\t\t";
            }
            view += "\n";
            results.moveToNext();
        }
        results.close();

        return view;
    }

    public String selectJob() {
        String sql = "select * from " + tableName + ";";
        Cursor results = db.rawQuery(sql, null);
        results.moveToFirst();

        String view = "사건\t\t시간\t\t위치\t\t내용\n";

        String[] location = results.getString(3).split(",");
        DecimalFormat df = new DecimalFormat("0.##");

        while (!results.isAfterLast()) {
            for(int i=1; i<=4; i++) {
                if(results.getString(1).equals("일")) {
                    if (i == 3)
                        view += df.format(Double.parseDouble(location[0])) + " " +
                                df.format(Double.parseDouble(location[1])) + "\t\t";
                    else
                        view += results.getString(i) + "\t\t";
                }
            }
            if(results.getString(1).equals("일"))
                view += "\n";
            results.moveToNext();
        }
        results.close();

        return view;
    }

    public String selectHobby() {
        String sql = "select * from " + tableName + ";";
        Cursor results = db.rawQuery(sql, null);
        results.moveToFirst();

        String view = "사건\t\t시간\t\t위치\t\t내용\n";

        String[] location = results.getString(3).split(",");

        while (!results.isAfterLast()) {
            for(int i=1; i<=4; i++) {
                if(results.getString(1).equals("취미")) {
                    if (i == 3)
                        view += df.format(Double.parseDouble(location[0])) + " " +
                                df.format(Double.parseDouble(location[1])) + "\t\t";
                    else
                        view += results.getString(i) + "\t\t";
                }
            }
            if(results.getString(1).equals("취미"))
                view += "\n";
            results.moveToNext();
        }
        results.close();

        return view;
    }

    public String selectEtc() {
        String sql = "select * from " + tableName + ";";
        Cursor results = db.rawQuery(sql, null);
        results.moveToFirst();

        String view = "사건\t\t시간\t\t위치\t\t내용\n";

        String[] location = results.getString(3).split(",");

        while (!results.isAfterLast()) {
            for(int i=1; i<=4; i++) {
                if(results.getString(1).equals("기타")){
                    if (i == 3)
                        view += df.format(Double.parseDouble(location[0])) + " " +
                                df.format(Double.parseDouble(location[1])) + "\t\t";
                    else
                        view += results.getString(i) + "\t\t";
                }
            }
            if(results.getString(1).equals("기타"))
                view += "\n";
            results.moveToNext();
        }
        results.close();

        return view;
    }
}
<file_sep>package kay.kookmin.ac.kr.term_project;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class ResultMapActivity extends AppCompatActivity {
    // google map
    private GoogleMap map;

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

        // Database 관련 객체들
        SQLiteDatabase db;
        String dbName = "eventData.db";
        String tableName = "eventDataTable";
        int dbMode = Context.MODE_PRIVATE;

        Intent intent = getIntent();
        String currentLocation = intent.getStringExtra("Location");
        String[] l = currentLocation.split(",");

        // google map
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(l[0]),Double.parseDouble(l[1])), 15));
        map.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);

        // Database 생성 및 열기
        db = openOrCreateDatabase(dbName,dbMode,null);

        String sql = "select * from " + tableName + ";";
        Cursor results = db.rawQuery(sql, null);
        results.moveToFirst();

        while (!results.isAfterLast()) {
            String[] location = results.getString(3).split(",");
            String[] date = results.getString(2).split("/");

            if(results.getString(1).equals("일")) {
                map.addMarker(new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(location[0]), Double.parseDouble(location[1])))
                        .snippet(results.getString(1) + " " + results.getString(4))
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                        .title(date[0].substring(2) + "년" + date[1] + "월" + date[2] + "일"));

                results.moveToNext();
            }
            else if(results.getString(1).equals("취미")) {
                map.addMarker(new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(location[0]), Double.parseDouble(location[1])))
                        .snippet(results.getString(1) + " " + results.getString(4))
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                        .title(date[0].substring(2) + "년" + date[1] + "월" + date[2] + "일"));

                results.moveToNext();
            }
            else {
                map.addMarker(new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(location[0]), Double.parseDouble(location[1])))
                        .snippet(results.getString(1) + " " + results.getString(4))
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))
                        .title(date[0].substring(2) + "년" + date[1] + "월" + date[2] + "일"));

                results.moveToNext();
            }

        }
        results.close();

    }
}
<file_sep>package kay.kookmin.ac.kr.term_project;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;

import java.util.GregorianCalendar;

public class MainActivity extends AppCompatActivity implements GoogleMap.OnMyLocationChangeListener {

    // google map
    static final LatLng SEOUL = new LatLng(37.56,126.97);
    private GoogleMap map;

    // Database 관련 객체들
    SQLiteDatabase db;
    String dbName = "eventData.db";
    String tableName = "eventDataTable";
    int dbMode = Context.MODE_PRIVATE;

    // layout object
    ArrayAdapter<CharSequence> adapter = null;
    Spinner spinner = null;

    EditText et_event;
    Button bt_insert;
    Button bt_resultNum;
    Button bt_resultMap;

    Context context;

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

        // spinner
        adapter = ArrayAdapter.createFromResource(this,R.array.type_data,android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);

        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setPrompt("사건선택");

        spinner.setAdapter(adapter);

        // google map
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(SEOUL, 15));
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

        map.setMyLocationEnabled(true);
        map.setOnMyLocationChangeListener(this);

        // Database 생성 및 열기
        db = openOrCreateDatabase(dbName,dbMode,null);

        // 테이블 생성
        createTable();

        context = this.getApplication().getApplicationContext();

        et_event = (EditText) findViewById(R.id.event);
        bt_insert = (Button) findViewById(R.id.insert);
        bt_resultNum = (Button) findViewById(R.id.result_num);
        bt_resultMap = (Button) findViewById(R.id.result_map);

        bt_insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = spinner.getSelectedItem().toString();
                GregorianCalendar today = new GregorianCalendar();

                String time = today.get(today.YEAR) + "/"
                        + Integer.toString(today.get(today.MONTH)+1) + "/"
                        + today.get(today.DAY_OF_MONTH) + "/"
                        + today.get(today.HOUR_OF_DAY)+ ":"
                        + today.get(today.MINUTE) + ":"
                        + today.get(today.SECOND);


                String location = map.getMyLocation().getLatitude() + "," +
                        map.getMyLocation().getLongitude();

                String event = et_event.getText().toString();
                insertData(type,time,location,event);
            }
        });

        bt_resultNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ResultNumActivity.class);
                startActivity(intent);
            }
        });

        bt_resultMap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ResultMapActivity.class);
                String location = map.getMyLocation().getLatitude() + ","
                        + map.getMyLocation().getLongitude();
                intent.putExtra("Location",location);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onMyLocationChange(Location location) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        Log.e("onMyLocationChange", lat + "," + lng);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat,lng),15));
    }

    // Table 생성
    public void createTable() {
        try {
            String sql = "create table " + tableName + "(id integer primary key autoincrement, " +
                    "type text, time text, location text, etc text)";
            db.execSQL(sql);
        } catch (android.database.sqlite.SQLiteException e) {
            Log.d("sqlite", "error: " + e);
        }
    }

    // Data 추가
    public void insertData(String type, String time, String location, String event) {
        String sql = "insert into " + tableName + " values(NULL, '"
                + type + "','" + time + "','" + location + "','" + event + "');";
        Toast.makeText(context, type + "/" + time + "/" + location + "/" + event,
                Toast.LENGTH_LONG).show();
        db.execSQL(sql);
    }
}
