Listview 내에서 arraylist 의 초성 검색 예제입니다.
참고 url
http://www.androidpub.com/45681
HangulUtils.cass 파일을 생성합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | /** * com.ziofront.utils * HangulUtils.java * Jiho Park 2009. 11. 27. * * Copyright (c) 2009 ziofront.com. All Rights Reserved. */ public class HangulUtils { private static String toHexString( int decimal) { Long intDec = Long.valueOf(decimal); return Long.toHexString(intDec); } public static final int HANGUL_BEGIN_UNICODE = 44032 ; // 가 public static final int HANGUL_END_UNICODE = 55203 ; // 힣 public static final int HANGUL_BASE_UNIT = 588 ; public static final int [] INITIAL_SOUND_UNICODE = { 12593 , 12594 , 12596 , 12599 , 12600 , 12601 , 12609 , 12610 , 12611 , 12613 , 12614 , 12615 , 12616 , 12617 , 12618 , 12619 , 12620 , 12621 , 12622 }; public static final char [] INITIAL_SOUND = { 'ㄱ' , 'ㄲ' , 'ㄴ' , 'ㄷ' , 'ㄸ' , 'ㄹ' , 'ㅁ' , 'ㅂ' , 'ㅃ' , 'ㅅ' , 'ㅆ' , 'ㅇ' , 'ㅈ' , 'ㅉ' , 'ㅊ' , 'ㅋ' , 'ㅌ' , 'ㅍ' , 'ㅎ' }; /** * 문자를 유니코드(10진수)로 변환 후 반환 한다. * * @param ch * @return */ public static int convertCharToUnicode( char ch) { return ( int ) ch; } /** * 문자열을 유니코드(10진수)로 변환 후 반환 한다. * * @param str * @return */ public static int [] convertStringToUnicode(String str) { int [] unicodeList = null ; if (str != null ) { unicodeList = new int [str.length()]; for ( int i = 0 ; i < str.length(); i++) { unicodeList[i] = convertCharToUnicode(str.charAt(i)); } } return unicodeList; } /** * 유니코드(16진수)를 문자로 변환 후 반환 한다. * * @param hexUnicode * @return */ public static char convertUnicodeToChar(String hexUnicode) { return ( char ) Integer.parseInt(hexUnicode, 16 ); } /** * 유니코드(10진수)를 문자로 변환 후 반환 한다. * * @param unicode * @return */ public static char convertUnicodeToChar( int unicode) { return convertUnicodeToChar(toHexString(unicode)); } /** * * @param value * @return */ public static String getHangulInitialSound(String value) { StringBuffer result = new StringBuffer(); int [] unicodeList = convertStringToUnicode(value); for ( int unicode : unicodeList) { if (HANGUL_BEGIN_UNICODE <= unicode && unicode <= HANGUL_END_UNICODE) { int tmp = (unicode - HANGUL_BEGIN_UNICODE); int index = tmp / HANGUL_BASE_UNIT; result.append(INITIAL_SOUND[index]); } else { result.append(convertUnicodeToChar(unicode)); } } return result.toString(); } public static boolean [] getIsChoSungList(String name) { if (name == null ) { return null ; } boolean [] choList = new boolean [name.length()]; for ( int i = 0 ; i < name.length(); i++) { char c = name.charAt(i); boolean isCho = false ; for ( char cho : INITIAL_SOUND) { if (c == cho) { isCho = true ; break ; } } choList[i] = isCho; } return choList; } public static String getHangulInitialSound(String value, String searchKeyword) { return getHangulInitialSound(value, getIsChoSungList(searchKeyword)); } public static String getHangulInitialSound(String value, boolean [] isChoList) { StringBuffer result = new StringBuffer(); int [] unicodeList = convertStringToUnicode(value); for ( int idx = 0 ; idx < unicodeList.length; idx++) { int unicode = unicodeList[idx]; if (isChoList != null && idx <= (isChoList.length - 1 )) { if (isChoList[idx]) { if (HANGUL_BEGIN_UNICODE <= unicode && unicode <= HANGUL_END_UNICODE) { int tmp = (unicode - HANGUL_BEGIN_UNICODE); int index = tmp / HANGUL_BASE_UNIT; result.append(INITIAL_SOUND[index]); } else { result.append(convertUnicodeToChar(unicode)); } } else { result.append(convertUnicodeToChar(unicode)); } } else { result.append(convertUnicodeToChar(unicode)); } } return result.toString(); } public static void main(String[] args) { for ( char ch : HangulUtils.INITIAL_SOUND) { System.out.print(HangulUtils.convertCharToUnicode(ch) + "," ); } } } |
ListView뷰를 구성합니다.
xml코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version= "1.0" encoding= "utf-8" ?> <RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "@color/colorBg" > <android.support.v7.widget.RecyclerView android:layout_height= "match_parent" android:layout_width= "match_parent" android:background= "@color/colorBg" android:id= "@+id/rv" > </android.support.v7.widget.RecyclerView> <!-- no data --> <TextView android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_margin= "20dp" android:id= "@+id/tv_noSearch" android:textColor= "#000000" android:text= "검색된 메모가 없습니다" android:visibility= "invisible" android:textSize= "16sp" /> </RelativeLayout> |
EditText 내에서의 TextWathcher 작업을 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <p>search_view = (EditText) findViewById(R.id.search_view); search_view.addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence c, int i, int i1, int i2) { setChangeListData(c.toString()); } @Override public void afterTextChanged(Editable editable) { } });</p> |
setChangeListData 메소드를 작성합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | public void setChangeListData(String searchKeyword) { if (searchKeyword != null ) { if (searchKeyword.length() == 0 ) { setLoadListData(oriList); } else { List<ItemMemo> temp = new ArrayList<>(); for (ItemMemo i : mList) { boolean isAdd = false ; String iniName = HangulUtils.getHangulInitialSound(i.contents, searchKeyword); if (iniName.indexOf(searchKeyword) >= 0 ) { isAdd = true ; } if (isAdd) { temp.add(i); } } setSearchData(temp); } } else { setLoadListData(oriList); } } private void setSearchData(List<ItemMemo> list) { lay_noData.setVisibility(View.GONE); tv_noSearch.setVisibility(View.GONE); if (list.size() == 0 ) { rv.setVisibility(View.INVISIBLE); tv_noSearch.setVisibility(View.VISIBLE); } else { rv.setVisibility(View.VISIBLE); mAdapter.swap(list); } } |
'Android' 카테고리의 다른 글
[안드로이드] Activity,Fragment 에서 Bundle 로 Object, ArrayList 넘기기 (1) | 2017.04.17 |
---|---|
[안드로이드] sqlite Like 검색 예제 (0) | 2017.04.17 |
[안드로이드] System.currentTimeMillis() 에서 몇분전, 몇시간전, 몇일전 등등 계산하기 (4) | 2017.04.16 |
[안드로이드] 현재 fragment 불러오기 및 구분 (0) | 2017.04.16 |
[안드로이드] cardView background color (0) | 2017.04.16 |