Listview 내에서 arraylist 의 초성 검색 예제입니다.

참고 url

http://www.androidpub.com/45681


   


HangulUtils.cass 파일을 생성합니다.

/**
 * 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코드

<?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 작업을 합니다.

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) { } });


setChangeListData 메소드를 작성합니다.

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);
	}
}


+ Recent posts