如何在多选列表视图中保存检查位置项的状态

时间:2014-11-11 06:11:45

标签: android android-listview android-adapter android-checkbox multipleselection

这里我使用适配器显示具有搜索功能的多个选择列表视图但是当我选择没有过滤列表视图的项目时获取正确的检查列表项但是当我过滤列表视图获取错误的检查项目时,则如何保存状态筛选列表时检查项目。以下是我的适配器类代码。

public class List_Filter_Multi_SelectAdapter extends ArrayAdapter<List_Filter> {
    private Context context;
    private List<List_Filter> user_list;
    private List<List_Filter>original_userList;
    private Filter userFilter;
    private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();

/*  public List_Filter_Multi_SelectAdapter(Context context, int resource,
            int textViewResourceId, List_Filter[] objects) {
        super(context, resource, textViewResourceId, objects);

        for(int i = 0; i < objects.length; i++){
            myChecked.put(i, false);
        }
    }*/
    public List_Filter_Multi_SelectAdapter(List<List_Filter> user_list, Context ctx) {
        super(ctx, R.layout.row_for_multiple_selection, user_list);
        this.user_list = user_list;
        this.context = ctx;
        this.original_userList = user_list;
        for(int i = 0; i < user_list.size(); i++){
            myChecked.put(i, false);
        }
    }
    public int getCount() {
        return user_list.size();
    }

    public List_Filter getItem(int position) {
        return user_list.get(position);
    }

    public long getItemId(int position) {
        return user_list.get(position).hashCode();
    }

    private static class memberHolder {
        public TextView memberNameView;

    }

    /*public void filter(String charText) {
        // TODO Auto-generated method stub
        charText = charText.toLowerCase(Locale.getDefault());

          filterOfMember.clear();
        for (String element :listOfuserName) {
             if (element.toLowerCase().contains(charText.toString().toLowerCase())) {
             filterOfMember.add(element);
                }
             }
        notifyDataSetChanged();

    }*/

    public void toggleChecked(int position){
        if(myChecked.get(position)){
            myChecked.put(position, false);
        }else{
            myChecked.put(position, true);
        }

        notifyDataSetChanged();
    }

    public List<Integer> getCheckedItemPositions(){
        List<Integer> checkedItemPositions = new ArrayList<Integer>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                (checkedItemPositions).add(i);
            }
        }

        return checkedItemPositions;
    }

    public List<String> getCheckedItems(){
        List<String> checkedItems = new ArrayList<String>();

        for(int i = 0; i < myChecked.size(); i++){
            if (myChecked.get(i)){
                List_Filter p = user_list.get(i);
                (checkedItems).add(p.getName());

            }
         }

        return checkedItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        memberHolder holder=new memberHolder();

        CheckedTextView checkedTextView;
        if(row==null){
            List_Filter p = user_list.get(position);

            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            row=li.inflate(R.layout.row_for_multiple_selection, parent, false);     
            TextView tv = (TextView) row.findViewById(R.id.textForMultiselection);
            holder.memberNameView = tv;
            row.setTag(holder);
           checkedTextView = (CheckedTextView)row.findViewById(R.id.textForMultiselection);

            checkedTextView.setText((CharSequence) p.getName());
        }else{

            List_Filter p = user_list.get(position);

            holder = (memberHolder) row.getTag();

            //List_Filter p = user_list.get(position);
            holder.memberNameView.setText(p.getName());
             checkedTextView = (CheckedTextView)row.findViewById(R.id.textForMultiselection);

                checkedTextView.setText(holder.memberNameView.getText());

        }



        Boolean checked = myChecked.get(position);
        if (checked != null) {
            checkedTextView.setChecked(checked);
       }
        return row;
    }
    public void resetData() {
        user_list = original_userList;
    }

    @Override
    public Filter getFilter() {
        if (userFilter == null)
            userFilter = new userNameFilter();

        return userFilter;
    }

    private class userNameFilter extends Filter {



        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            // We implement here the filter logic
            if (constraint == null || constraint.length() == 0) {
                // No filter implemented we return all the list
                results.values = original_userList;
                results.count = original_userList.size();
            }
            else {
                // We perform filtering operation
                List<List_Filter> nUserList = new ArrayList<List_Filter>();

                for (List_Filter p : user_list) {
                    if (p.getName().toUpperCase().startsWith(constraint.toString().toUpperCase()))
                        nUserList.add(p);
                }

                results.values = nUserList;
                results.count = nUserList.size();

            }
            return results;
        }

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

            // Now we have to inform the adapter about the new list filtered
            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                user_list = (List<List_Filter>) results.values;
                notifyDataSetChanged();
            }

        }

    }
}

1 个答案:

答案 0 :(得分:3)

因为在 myChecked 列表中,您可以保存项目的实际位置(他们在 original_userList 中的位置,而不是他们在过滤后在列表中的位置) 。 你可以在List_Filter.class中添加一个布尔参数来保存检查状态,或者你创建另一个从List_Filter扩展的类并添加这个参数

    public class Checked_List_Filter extends List_Filter{
        boolean isChecked;
}

并且您的代码变为:

   public class List_Filter_Multi_SelectAdapter extends ArrayAdapter<List_Filter> {
    private Context context;
    private List<Checked_List_Filter> user_list;
    private List<List_Filter>original_userList;
    private Filter userFilter;


    public List_Filter_Multi_SelectAdapter(List<List_Filter> user_list, Context ctx) {
        super(ctx, R.layout.row_for_multiple_selection, user_list);
        this.user_list = user_list;
        this.context = ctx;
        this.original_userList = user_list;
        for(int i = 0; i < user_list.size(); i++){
            user_list.get(i).isChecked = false;
        }
    }
    public int getCount() {
        return user_list.size();
    }

    public Ckecked_List_Filter getItem(int position) {
        return user_list.get(position);
    }

    public long getItemId(int position) {
        return user_list.get(position).hashCode();
    }

    private static class memberHolder {
        public TextView memberNameView;

    }

    /*public void filter(String charText) {
        // TODO Auto-generated method stub
        charText = charText.toLowerCase(Locale.getDefault());

          filterOfMember.clear();
        for (String element :listOfuserName) {
             if (element.toLowerCase().contains(charText.toString().toLowerCase())) {
             filterOfMember.add(element);
                }
             }
        notifyDataSetChanged();

    }*/

    public void toggleChecked(int position){
        if(user_list.get(position).isChecked){
            user_list.get(position).isChecked = false;
        }else{
            user_list.get(position).isChecked = true;
        }

        notifyDataSetChanged();
    }

    public List<Integer> getCheckedItemPositions(){
        List<Integer> checkedItemPositions = new ArrayList<Integer>();

        for(int i = 0; i < user_list.size(); i++){
            if user_list.get(position).isChecked){
                (checkedItemPositions).add(i);
            }
        }

        return checkedItemPositions;
    }

    public List<String> getCheckedItems(){
        List<String> checkedItems = new ArrayList<String>();

        for(int i = 0; i < user_list.size(); i++){
            if (user_list.get(position).isChecked){
                Checked_List_Filter p = user_list.get(i);
                (checkedItems).add(p.getName());

            }
         }

        return checkedItems;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        memberHolder holder=new memberHolder();

        CheckedTextView checkedTextView;
        if(row==null){
            Checked_List_Filter p = user_list.get(position);

            LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            row=li.inflate(R.layout.row_for_multiple_selection, parent, false);     
            TextView tv = (TextView) row.findViewById(R.id.textForMultiselection);
            holder.memberNameView = tv;
            row.setTag(holder);
           checkedTextView = (CheckedTextView)row.findViewById(R.id.textForMultiselection);

            checkedTextView.setText((CharSequence) p.getName());
        }else{

            Checked_List_Filter p = user_list.get(position);

            holder = (memberHolder) row.getTag();

            //List_Filter p = user_list.get(position);
            holder.memberNameView.setText(p.getName());
             checkedTextView = (CheckedTextView)row.findViewById(R.id.textForMultiselection);

                checkedTextView.setText(holder.memberNameView.getText());

        }



        Boolean checked = user_list.get(position).isChecked;
        if (checked != null) {
            checkedTextView.setChecked(checked);
       }
        return row;
    }
    public void resetData() {
        user_list = original_userList;//TODO try to cast objects and set isChecked  to false 
    }

    @Override
    public Filter getFilter() {
        if (userFilter == null)
            userFilter = new userNameFilter();

        return userFilter;
    }

  //.........
}