사용자 도구

사이트 도구


android:collection:search
search

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
android:collection:search [2025/05/12 00:27] – 검색 결과 하이라이트 이거니맨android:collection:search [2025/06/03 18:22] (현재) – SQL 검색 이거니맨
줄 219: 줄 219:
  
 } }
 +</code> 
 +
 +
 +===== Roomdatabase에서 검색하기 ===== 
 +
 +==== 1. Dao 만들기 ==== 
 +
 +Dao에서 SQL문은 다음과 같이 작성하면 된다.
 +
 +<code kotlin>
 +    @Query("SELECT * FROM SCORE " +
 +            "WHERE (CASE WHEN :isFiltered = 1 THEN (quoteTitle LIKE :title AND speechResult LIKE '%' || :search || '%') ELSE (1=1 AND speechResult LIKE '%' || :search || '%') END)" +
 +            "ORDER BY " +
 +            "CASE WHEN :sortMethod='TitleASC'  THEN quoteTitle END ASC, " +
 +            "CASE WHEN :sortMethod='TitleDESC' THEN quoteTitle END DESC," +
 +            "CASE WHEN :sortMethod='DateASC' THEN createdAt END ASC," +
 +            "CASE WHEN :sortMethod='DateDESC' THEN createdAt END DESC," +
 +            "CASE WHEN :sortMethod='ScoreASC' THEN speechScore END ASC," +
 +            "CASE WHEN :sortMethod='ScoreDESC' THEN speechScore END DESC"
 +
 +    )
 +    fun getScoreList(sortMethod : String, isFiltered : Boolean, title : String, search : String) : LiveData<List<Score>>
 +</code> 
 +
 +일부 단어 포함(위의 Collection 기능에서 contains 함수)을 하려면 SQL 문에서 '%'를 앞뒤로 붙이면 된다.
 +
 +그런데 Kotlin문에서 이를 표시하면 concatrate연산자 '||'를 사용한다. 따라서 다음과 같이 LIKE문을 만든다. 
 +
 +  LIKE '%' || :search || '%'
 +  
 +
 +==== 2. ViewModel 만들기 ====
 +
 +ViewModel은 다음과 같이 하면 된다.
 +
 +<code kotlin>
 +    fun scoreList(sortMethod : Sort, isFiltered : Boolean, filter : String, search : String) : LiveData<List<Score>> {
 +        return when (sortMethod) {
 +            Sort.DateASC -> scoreDao.getScoreList("DateASC", isFiltered, filter, search)
 +            Sort.DateDESC -> scoreDao.getScoreList("DateDESC", isFiltered, filter, search)
 +            Sort.TitleASC -> scoreDao.getScoreList("TitleASC", isFiltered, filter, search)
 +            Sort.TitleDESC -> scoreDao.getScoreList("TitleDESC", isFiltered, filter, search)
 +            Sort.ScoreASC -> scoreDao.getScoreList("ScoreASC", isFiltered, filter, search)
 +            Sort.ScoreDESC -> scoreDao.getScoreList("ScoreDESC", isFiltered, filter, search)
 +        }
 +    }
 +</code>
 +
 +
 +==== 3. Compose View화면 ==== 
 +
 +View  화면에서는 다음과 같이 뷰모델을 호출한다.
 +
 +<code kotlin>
 +    //  검색어
 +    var searchString by remember { mutableStateOf("") }
 +
 +    val scoreList by viewModel.scoreList(sortMethod.value, isListAll.value, curQuoteText?.title!!, searchString).observeAsState() // 점수 목록
 +    
 +        // 검색 창
 +    Row(modifier = Modifier
 +        .fillMaxWidth()
 +        .padding(6.dp), horizontalArrangement = Arrangement.SpaceEvenly) {
 +
 +        OutlinedTextField(
 +            value =  searchString,
 +            onValueChange = { searchString = it },
 +            label = { Text(text = "검색", style = MaterialTheme.typography.titleMedium )  },
 +            trailingIcon = {
 +                IconButton(onClick = { searchString = "" }) {
 +                    Icon(
 +                        imageVector = Icons.Default.Close,
 +                        contentDescription = "Select Drop Down"
 +                    )
 +                }
 +            },
 +            textStyle = TextStyle(fontFamily = fontkjcMyungjo, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, fontStyle = FontStyle.Normal),
 +            keyboardOptions = KeyboardOptions.Default,
 +        )
 +    }
 +    
 +
 +        // 연락처 리스트
 +        scoreList?.forEach() { item ->
 +
 +            // 자세히 버튼
 +//            val completeQuote by remember { mutableStateOf(doneList?.find { it.quoteID == item.quoteID })  }
 +//            val done by remember { mutableStateOf(completeQuote?.isDone?:false) }
 +            val completeQuote = doneList?.find { it.quoteID == item.quoteID }
 +            val done = completeQuote?.isDone?:false
 +
 +
 +            if (!done || !isFiltered) {
 +                ScoreItemCard(item = item, onDelete = {viewModel.deleteScore(item)},
 +                    onComplete = {
 +                        if (completeQuote != null) {
 +                            completeQuote.isDone = false;
 +                            viewModel.upsertDone(completeQuote)
 +                        }
 +                    },
 +                    onUnComplete = {
 +                        if (completeQuote != null) {
 +                            completeQuote.isDone = true;
 +                            viewModel.upsertDone(completeQuote)
 +                        }else {
 +                            val tempComplete = DoneList(
 +                                quoteID = item.quoteID,
 +                                isDone = false,
 +                                completeDate = Date.from(Instant.now()),
 +                                ext1 = ""
 +                            )
 +                            viewModel.upsertDone(tempComplete)
 +                        }
 +                    }, isDone = done)
 +            }
 +        }?: Text(
 +            modifier = Modifier.fillMaxWidth(),
 +            textAlign = TextAlign.Center,
 +            text = "No items yet",
 +            fontSize = 16.sp
 +        )
 </code> </code>
android/collection/search.1746977221.txt.gz · 마지막으로 수정됨: 저자 이거니맨