|
| 1 | +package com.example.user.dogslight; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.support.v7.app.AlertDialog; |
| 6 | +import android.support.v7.app.AppCompatActivity; |
| 7 | +import android.view.View; |
| 8 | +import android.widget.Button; |
| 9 | +import android.widget.EditText; |
| 10 | +import android.widget.TextView; |
| 11 | + |
| 12 | +import com.android.volley.RequestQueue; |
| 13 | +import com.android.volley.Response; |
| 14 | +import com.android.volley.toolbox.Volley; |
| 15 | + |
| 16 | +import org.json.JSONObject; |
| 17 | + |
| 18 | +import java.text.SimpleDateFormat; |
| 19 | +import java.util.Date; |
| 20 | + |
| 21 | +// 산책 기록을 추가하는 액티비티. |
| 22 | +public class AddScheduleActivity extends AppCompatActivity { |
| 23 | + |
| 24 | + private String walk; |
| 25 | + private String formatDate; |
| 26 | + |
| 27 | + @Override |
| 28 | + protected void onCreate(Bundle savedInstanceState) { |
| 29 | + super.onCreate(savedInstanceState); |
| 30 | + setContentView(R.layout.activity_add_schedule); |
| 31 | + |
| 32 | + // xml의 위젯을 변수로 선언한다. |
| 33 | + final TextView walkContentText = (TextView) findViewById(R.id.walkContentText); |
| 34 | + final TextView walkDateText = (TextView) findViewById(R.id.walkDateText); |
| 35 | + final EditText walkNameText = (EditText) findViewById(R.id.walkNameText); |
| 36 | + final Button addButton = (Button) findViewById(R.id.addButton); |
| 37 | + |
| 38 | + //현재시간을 now에 저장한다. |
| 39 | + long now= System.currentTimeMillis(); |
| 40 | + // now로 새로운 Date객체를 만든다. |
| 41 | + Date date= new Date(now); |
| 42 | + // date를 테이블에 삽입하는 타입으로 만들기 위해 SimpleDateFormat 변수를 선언한다. |
| 43 | + SimpleDateFormat df; |
| 44 | + df = new SimpleDateFormat("yyyy-MM-dd"); |
| 45 | + // date, 즉 현재 날짜를 formatDate에 2017-08-30과 같은 값으로 저장한다. |
| 46 | + formatDate= df.format(date); |
| 47 | + |
| 48 | + // 자신을 부른 Intent를 얻어온다. |
| 49 | + Intent intent = getIntent(); |
| 50 | + // Intent에 넣어준 산책 시간인 walk를 String변수에 저장한다. |
| 51 | + walk = intent.getExtras().getString("walk"); |
| 52 | + |
| 53 | + // textView에 현재시간과 산책시간을 보여준다. |
| 54 | + walkDateText.setText(formatDate); |
| 55 | + walkContentText.setText(walk); |
| 56 | + |
| 57 | + // 추가버튼을 누르면 아래와 같은 작업을 한다. |
| 58 | + addButton.setOnClickListener(new View.OnClickListener() { |
| 59 | + @Override |
| 60 | + public void onClick(View v) { |
| 61 | + // textView에 적힌 데이터를 읽어 String 변수에 저장한다. |
| 62 | + String content = walkContentText.getText().toString(); |
| 63 | + String date = walkDateText.getText().toString(); |
| 64 | + String name = walkNameText.getText().toString(); |
| 65 | + // MainActivity에 static으로 선언된 userID를 가져온다. |
| 66 | + String userID = MainActivity.userID; |
| 67 | + |
| 68 | + // 사용자가 값을 덜 넣으면 아래의 문장을 수행한다. |
| 69 | + if(content.equals("") ||date.equals("") || name.equals("")) { |
| 70 | + AlertDialog.Builder builder = new AlertDialog.Builder(AddScheduleActivity.this); |
| 71 | + AlertDialog dialog = builder.setMessage("빈칸 없이 입력해주십시오.") |
| 72 | + .setPositiveButton("확인", null) |
| 73 | + .create(); |
| 74 | + dialog.show(); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + // 추가 작업을 하는 리스너를 만든다. |
| 79 | + Response.Listener<String> responseListener = new Response.Listener<String>() { |
| 80 | + @Override |
| 81 | + public void onResponse(String response) { |
| 82 | + try { |
| 83 | + // JSONObject의 response값을 얻어온다. |
| 84 | + JSONObject jsonResponse = new JSONObject(response); |
| 85 | + boolean success = jsonResponse.getBoolean("success"); |
| 86 | + // response의 success값이 true, 즉 삽입이 성공적으로 이루어졌다면 현재 액티비티를 종료한다. |
| 87 | + if(success) { |
| 88 | + finish(); // WalkFragment로 돌아간다. |
| 89 | + } |
| 90 | + |
| 91 | + } catch (Exception e) { |
| 92 | + e.printStackTrace(); |
| 93 | + } |
| 94 | + } |
| 95 | + }; |
| 96 | + // AddScheduleRequest에 입력된 값을 모두 넘긴다. |
| 97 | + AddScheduleRequest addScheduleRequest = new AddScheduleRequest(userID, content, name, date, responseListener); |
| 98 | + // 큐를 만들어 리퀘스트를 실제로 보낼 수 있도록 한다. |
| 99 | + RequestQueue queue = Volley.newRequestQueue(AddScheduleActivity.this); |
| 100 | + queue.add(addScheduleRequest); |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + @Override |
| 107 | + protected void onStop() { |
| 108 | + super.onStop(); |
| 109 | + |
| 110 | + |
| 111 | + } |
| 112 | +} |
0 commit comments