특정 시점 액티비티로 인텐트 전송하기혹은 데이터 저장 되게 하기카메라 작동되게 하기현재 코딩실력 없으면 안보이게 하기이미지 최대 5개 넣을 수 있게 하기답변하기 intent값 받아서 집어넣기- 다중이미지 선택가능하게
답변하기 리사이클러뷰intent연결- 뷰페이저 추가로 페이지 추가되게
- smallquestionIdx 확인
정리본
//startActivityresult대신
activityResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
}
이번에 startActivityresult 가 중단되었다.
이걸 대체하는 함수로 여기저기 찾아보니 위처럼 사용하면 대체할 수 있다
//5개 이하일때만 추가가능
if (count<5){
//추가버튼
binding.checkcodingquestionPlusIv.setOnClickListener {
val intent =
Intent(this, CodingCameraShootingActivity::class.java)
activityResultLauncher.launch(intent)
// finish()
}
}
나는 뷰페이저의 개수를 5개로 제한해야하는데 어댑터에서 size를 제한하면 오류가 생겨
이렇게 count라는 함수를 적용시키고 이미지가 삽입되면 count가 올라가게 했다.
private fun getCoding(): CheckCoding { // view에서 받은 값들
title=binding.checkcodingquestionTitleTextEt.text.toString()
currentError=binding.checkcodingquestionStopPartTextEt.text.toString()
myCodingSkill=binding.checkcodingquestionCodingLevelTextEt.text.toString()
codeQuestionUrl=binding.checkcodingquestionErrorCodeTextEt.text.toString()
bigCategoryIdx=binding.checkcodingquestionBigCategorySp.selectedItemPosition.toLong()+1
Log.d("images", images.toString())
Log.d("userIdx : ", userIdx.toString())
Log.d("title : ", title)
Log.d("currentError : ", currentError)
Log.d("myCodingSkill : ", myCodingSkill)
Log.d("codeQuestionUrl : ", codeQuestionUrl)
Log.d("bigCategoryIdx : ", bigCategoryIdx.toString())
Log.d("smallCategoryIdx :", smallCategoryIdx.toString())
return CheckCoding(images, userIdx, currentError, myCodingSkill, bigCategoryIdx, smallCategoryIdx, title, codeQuestionUrl)
}
서버로 값을 보내는 것을 확인하는 함수이다.
//api서버
private fun checkCodingQuestion() {
val checkCodingQuestionService=CheckCodingQuestionService()
checkCodingQuestionService.setcheckcodingquestionView(this)
//원래는 getJwt(this)
checkCodingQuestionService.checkCodingQuestion(getJwt(this), getCoding())
Log.d("CHECKCODING/API","Hello")
}
private fun checkcodingif(){
if(binding.checkcodingquestionSmallCategorySp.isEnabled()==false){
Toast.makeText(this, "카테고리를 선택해주세요.", Toast.LENGTH_SHORT).show()
return
}
if (binding.checkcodingquestionTitleTextEt.text.isEmpty()) {
Toast.makeText(this, "제목을 작성해주세요.", Toast.LENGTH_SHORT).show()
return
}
if (binding.checkcodingquestionStopPartTextEt.text.isEmpty()) {
Toast.makeText(this, "현재 막힌 부분을 작성해주세요.", Toast.LENGTH_SHORT).show()
return
}
binding.checkcodingquestionQuestionIv.setImageResource(R.drawable.ic_click_question)
//승인 버튼 눌러야 api전송
val builder = AlertDialog.Builder(this).create()
val dialogView = layoutInflater.inflate(R.layout.dialog_question, null)
builder?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
builder?.setCancelable(false)
builder?.setCanceledOnTouchOutside(false)
val approve = dialogView.findViewById<Button>(R.id.dialog_approve_btn)
approve.setOnClickListener {
checkCodingQuestion()
builder.dismiss()
}
val cancle = dialogView.findViewById<Button>(R.id.dialog_cancel_btn)
cancle.setOnClickListener {
builder.dismiss()
}
builder.setView(dialogView)
builder.show()
}
api연결이 성공되면 값들을 보내는 것인데 그 전에 필수로 입력되어야 할 값들이 적용되지 않으면 토스트메세지를 띄우게 했다.
그리고 완료버튼 누르기 전 다이얼로그를 띄웠다.
//카메라 앨범 이미지 가져오기
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK) {
var imagePath = data?.getStringExtra("path")!!
photoList.apply {
add(Photo(imagePath))
Log.d("SEND/path", imagePath)
count++
Log.d("path/count", count.toString())
}
Log.d("GETGET", photoList.toString())
//이미지가 5개부터는 추가 불
if (count>=5){
//추가버튼
binding.checkcodingquestionPlusIv.setOnClickListener {
Toast.makeText(this, "이미지는 최대 5개까지 넣을 수 있습니다", Toast.LENGTH_SHORT).show()
}
}
//count값을 매겨줘야 함
// if (count>0&&count<=5){
// binding.checkcodingquestionEditIv.visibility=View.VISIBLE
// binding.checkcodingquestionPlusIv.visibility=View.INVISIBLE
// photoList.add(Photo())
// if (count==1){
// binding.checkcodingquestionEditIv.visibility=View.VISIBLE
// binding.checkcodingquestionPlusIv.visibility=View.INVISIBLE
// }
// }
// 뷰페이저 어댑터 생성
viewPagerAdapter = ViewPagerAdapter(this, photoList)
binding.checkcodingquestionVp.adapter = viewPagerAdapter
binding.checkcodingquestionVp.orientation = ViewPager2.ORIENTATION_HORIZONTAL
binding.checkcodingIndicator.setViewPager(binding.checkcodingquestionVp)
//추가한 부분
// viewPagerAdapter.addlist(photoList)
viewPagerAdapter.notifyDataSetChanged()
images.add(imagePath)
Log.d("MMM", images.toString())
}
}
제일 많이 고생했던 부분이다.
카메라와 갤러리에서 이미지 삽입이 완료되면 새로운 액티비티를 불러오는 게 아니라 그 전 액티비티로 돌아가기+사진 삽입
그 전 액티비티에도 onActivityResult함수가 필요하다는 걸 몰랐다.
여기저기 찾아봐도 onActivityResult이게 필요하다는 글은 없었는데 딱 한 개의 글을 보고 찾을 수 있었다.
카메라 부분은 다양한 방법으로 적용해봐서 이제 앞으로 쉬울 것 같다!
// 스피너 클릭 이벤트 핸들러
private fun setupSmallCategorySpinnerHandler() {
binding.checkcodingquestionSmallCategorySp.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
binding.checkcodingquestionSmallCategorySp.setBackgroundResource(R.drawable.bg_category_outline)
if (!binding.checkcodingquestionSmallCategorySp.getItemAtPosition(position).equals("하위 선택")) {
// 하위 카테고리 선택하면 배경 변경
binding.checkcodingquestionSmallCategorySp.setBackgroundResource(R.drawable.bg_category_selected)
// SmallCategory 변수에 하위 카테고리 저장하기
smallCategory = binding.checkcodingquestionSmallCategorySp.getItemAtPosition(position).toString()
if (bigCategory=="앱") {
smallCategoryIdx =
binding.checkcodingquestionSmallCategorySp.selectedItemPosition.toLong() + 1
Log.d("category test", "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
if (bigCategory=="웹") {
smallCategoryIdx =
binding.checkcodingquestionSmallCategorySp.selectedItemPosition.toLong() + 3
Log.d("category test", "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
if (bigCategory=="서버") {
smallCategoryIdx =
binding.checkcodingquestionSmallCategorySp.selectedItemPosition.toLong() + 6
Log.d("category test", "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
if (bigCategory=="프로그래밍 언어") {
smallCategoryIdx =
binding.checkcodingquestionSmallCategorySp.selectedItemPosition.toLong() + 8
Log.d("category test", "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
if (bigCategory=="기타") {
smallCategoryIdx =
binding.checkcodingquestionSmallCategorySp.selectedItemPosition.toLong()
Log.d("category test", "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
// Log.i(ContentValues.TAG, "하위 카테고리 확인: $smallCategory")
} else {
smallCategory = null
Log.d("category test","하위 카테고리 넘버 확인: $smallCategory")
// Toast.makeText(context, "상위 카테고리를 선택해주세요!", Toast.LENGTH_SHORT).show()
}
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
// Log.d("category test"," : $smallCategoryIdx ")
}
스피너 부분
기타일땐 하위카테고리를 클릭하지 않아도 null값이 입력되야하고
다른 걸 누르고 기타를 누르면 새로 값이 저장되어야 하는데 그부분이 되지 않는다
기타를 누르고 다른 하위카테고리를 누르면 값이 저장되는데
다른 하위카테고리를 누르고 기타를 누르면 값이 null로 전송되지 않는다.
이 부분은 해결책을 찾아봐야할 것 같다.
++null값 문제 해결!
생각보다 간단했다.
다만 그 답을 찾는 과정이 길었을 뿐ㅠ
private var smallCategoryIdx: Long? = null
이렇게 선언해주고
smallCategoryIdx =null
if (bigCategory=="기타") {
smallCategoryIdx =null
// binding.checkcodingquestionSmallCategorySp.selectedItemPosition.toLong()
Log.d("category test", "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
기타일때는 하위카테고리가 선택이 안되니까 if문 전에 한 번 더 선언해주면 된다ㅎㅎ
++ 스피너 스몰카테고리값 최종본!
어디를 갖다와도 기타일때 null값이 잘 전달되도록 했다.
// 스피너 클릭 이벤트 핸들러
private fun setupSmallCategorySpinnerHandler() {
binding.checkconceptquestionSmallCategorySp.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
binding.checkconceptquestionSmallCategorySp.setBackgroundResource(R.drawable.bg_category_outline)
if (!binding.checkconceptquestionSmallCategorySp.getItemAtPosition(position).equals("하위 선택")) {
// 하위 카테고리 선택하면 배경 변경
binding.checkconceptquestionSmallCategorySp.setBackgroundResource(R.drawable.bg_category_selected)
// SmallCategory 변수에 하위 카테고리 저장하기
smallCategory = binding.checkconceptquestionSmallCategorySp.getItemAtPosition(position).toString()
Log.i(ContentValues.TAG, "하위 카테고리 확인: $smallCategory")
smallCategoryIdx = null
if (bigCategory=="앱") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 1
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
smallCategoryIdx = null
if (bigCategory=="웹") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 3
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
smallCategoryIdx = null
if (bigCategory=="서버") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 6
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
smallCategoryIdx = null
if (bigCategory=="프로그래밍 언어") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 8
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
else{//기타, 클릭
smallCategoryIdx = null
if (bigCategory=="앱") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 1
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
if (bigCategory=="웹") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 3
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
if (bigCategory=="서버") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 6
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
if (bigCategory=="프로그래밍 언어") {
smallCategoryIdx =
binding.checkconceptquestionSmallCategorySp.selectedItemPosition.toLong() + 8
Log.d(ContentValues.TAG, "하위 카테고리 넘버 확인: $smallCategoryIdx")
}
}
} else {
smallCategory = null
Log.i(ContentValues.TAG, "하위 카테고리 확인: $smallCategory")
// Toast.makeText(context, "상위 카테고리를 선택해주세요!", Toast.LENGTH_SHORT).show()
}
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
}
'무물컴 앱개발기록' 카테고리의 다른 글
개발일지 29 - 3/6 (0) | 2022.03.08 |
---|---|
개발일지 28 - 3/6 (0) | 2022.03.06 |
개발일지 26 - 3/3 (0) | 2022.03.05 |
개발일지 25 - 3/2 (0) | 2022.03.03 |
개발일지 24 - 3/1 (0) | 2022.03.02 |