1
import java.util.ArrayList;

import android.content.Context;
import android.os.Vibrator;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.RelativeLayout;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Filter.FilterResults;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

public class AppleSupportList extends BaseAdapter implements Filterable {

private LayoutInflater mInflater;
//private final String[] values;
private final ArrayList<String> values;
private final ArrayList<String> valuesFiltered;
private SparseBooleanArray mCheckStates;
Context _context;
Vibrator vibration;

public AppleSupportList(Context context, ArrayList<String> values) {
    mInflater = LayoutInflater.from(context);
    this.values = values;
    valuesFiltered = values;
    _context = context;
    mCheckStates = new SparseBooleanArray(values.size());
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return values.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

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

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.customlist1, null);
        holder = new ViewHolder();

        holder.reltiveLayout = (RelativeLayout) convertView.findViewById(R.id.customRelative);
        holder.text1 = (TextView) convertView.findViewById(R.id.labelContact1);
        holder.check = (CheckBox) convertView.findViewById(R.id.checkBoxContact1);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.text1.setText(valuesFiltered.get(position));
    holder.check.setChecked(mCheckStates.get(position, false)); 
    vibration = (Vibrator) _context.getSystemService(Context.VIBRATOR_SERVICE);


    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            vibration.vibrate(25);
            if(holder.check.isChecked()){
                holder.check.setChecked(false);
                toggle(position);
                //store id 
            }else{
                holder.check.setChecked(true);
                toggle(position);
                //remove id 
            }
        }
    });

     holder.check.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            vibration.vibrate(25);
            // TODO Auto-generated method stub
            CheckBox cb = (CheckBox) v;
            if(cb.isChecked()){
                toggle(position);
                //store id
            }else{
                toggle(position);
                //remove id
            }
        }
    });

    return convertView;
}

@Override
public Filter getFilter() {
    // TODO Auto-generated method stub
    Filter filter = new Filter() {

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            // TODO Auto-generated method stub
            valuesFiltered = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // TODO Auto-generated method stub
            FilterResults results = new FilterResults();
            ArrayList<String> filteredList  = new ArrayList<String>();
            if(values == null){
                values  = new ArrayList<String>(valuesFiltered);
            }

            if(constraint == null || constraint.length()==0){
                results.count = values.size();
                results.values = values;
            }else{
                constraint = constraint.toString().toLowerCase();
                for(int i=0;i<values.size();i++){
                    String data = values.get(i);
                    if(data.toLowerCase().startsWith(constraint.toString())){
                        filteredList.add(values.get(i));
                    }

                }
                results.values = filteredList;
                results.count = filteredList.size();
            }
            return results;
        }
    };
    return filter;
}

static class ViewHolder {
    TextView text1;
    ImageView img;
    CheckBox check;
    RelativeLayout reltiveLayout;
}

public boolean isChecked(int position) {
    return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
    mCheckStates.put(position, isChecked);

}

public void toggle(int position) {
    setChecked(position, !isChecked(position));
}

}

我正在向我的自定义 listView 添加搜索功能。我在 import import android.widget.Filter.FilterResults 中遇到错误;android.widget.filter.filterresults 类型不可见。我无法检测到问题出在哪里。

4

0 回答 0