게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
안드로이드 custom listview getfilter 질문입니다.
게시물ID : programmer_8998짧은주소 복사하기
작성자 : 늑대지렁
추천 : 0
조회수 : 2061회
댓글수 : 2개
등록시간 : 2015/03/30 16:33:23
옵션
  • 본인삭제금지
get filter가 동작하지를 않습니다

내부의 mDatabaseOfnames 가 값이 없는것같은데 어떤식으로 해주면 될까요



// main

@SuppressLint("ValidFragment")
public class TabNotification extends Fragment {

    EditText user_search;
    Context mContext;
    View view;

    public GroupListAdapter adapter;
    public List<Map<String, ?>> list;
    
    int search_length = 0;

    public TabNotification(Context context) {
        mContext = context;
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.activity_notification, null);

        list = new LinkedList<Map<String, ?>>();

        adapter = new GroupListAdapter(getActivity());
        adapter.addSection(getString(R.string.groupbell), new ArrayAdapter<String>(getActivity(),
                R.layout.listview_notification, new String[] { "최고 관리자", "중간 관리자", "일반 관리자" }));

        adapter.addSection(getString(R.string.userbell), new ArrayAdapter<String>(getActivity(),
                R.layout.listview_notification, new String[] { "사원1", "사원2", "사원3" }));
        ListView list = (ListView) view.findViewById(R.id.list_group);
        list.setAdapter(adapter);
        
        user_search = (EditText)view.findViewById(R.id.edit_search);
        user_search.addTextChangedListener(new TextWatcher() {
            
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                TabNotification.this.adapter.getFilter().filter(s);     // 검색했을때 list를 바꿔주는부분 -> 동작x
            }
            
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                
            }
        });

        return view;
    }
}

// adapter

public class GroupListAdapter extends BaseAdapter implements Filterable {

    public final static int TYPE_SECTION_HEADER = 0;

    public ArrayAdapter<String> headers;
    public Map<String, Adapter> sections = new LinkedHashMap<String, Adapter>();

    public GroupListAdapter(Context context) {
        // TODO Auto-generated constructor stub
        headers = new ArrayAdapter<String>(context, R.layout.list_header);
    }

    public void addSection(String groupbell, Adapter adapter) {
        this.headers.add(groupbell);
        this.sections.put(groupbell, adapter);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        int total = 0;
        for (Adapter adapter : this.sections.values())
            total += adapter.getCount() + 1;
        return total;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        for (Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            if (position == 0)
                return section;
            if (position < size)
                return adapter.getItem(position - 1);

            position -= size;
        }

        return null;

    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int sectionNum = 0;
        for (Object section : this.sections.keySet()) {
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1;

            // position이 해당 section 안에 있는지 체크
            if (position == 0)
                return headers.getView(sectionNum, convertView, parent);
            if (position < size)
                return adapter.getView(position - 1, convertView, parent);

            // position이 섹션의 크기를 넘어간다면 다음 섹션으로 점프
            position -= size;
            sectionNum++;
        }
        return null;
    }

    // 아이템(필드)의 타입을 반환
    public int getItemViewType(int position) {
        int type = 1;
        for (Object section : this.sections.keySet()) {

            // 해당 섹션의 어댑터와 어댑터의 아이템 크기를 반환
            Adapter adapter = sections.get(section);
            int size = adapter.getCount() + 1; // +1 : header

            // position이 해당 section 안에 있는지 체크
            if (position == 0)
                return TYPE_SECTION_HEADER;
            if (position < size)
                return type + adapter.getItemViewType(position - 1);

            // position이 섹션의 크기를 넘어간다면 다음 섹션으로 점프
            position -= size;
            type += adapter.getViewTypeCount();
        }
        return -1;
    }

    public boolean areAllItemsSectable() {
        return false;
    }

    public boolean isEnabled(int position) {
        return (getItemViewType(position) != TYPE_SECTION_HEADER);
    }

@Override
public Filter getFilter() {//method not working!

    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {

            List<String> arrayListNames;
            arrayListNames = (List<String>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            Filter.FilterResults results = new FilterResults();
            ArrayList<String> FilteredArrayNames = new ArrayList<String>();
            List<String> mDatabaseOfNames = null;                                            // 초기화되지를 않습니다.
            // perform your search here using the searchConstraint String.

            constraint = constraint.toString().toLowerCase();
            for (int i = 0; i < mDatabaseOfNames.size(); i++) {
                String dataNames = mDatabaseOfNames.get(i);
                if (dataNames.toLowerCase().startsWith(constraint.toString()))  {
                    FilteredArrayNames.add(dataNames);
                }
            }

            results.count = FilteredArrayNames.size();
            results.values = FilteredArrayNames;
            Log.e("VALUES", results.values.toString());
            return results;
        }
    };
    return filter;
  }
}

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호