如何在BaseAdapter上实现Filterable?

时间:2014-08-23 04:43:00

标签: java android baseadapter

在我的Custom BaseAdapter中,我们正在使用List< GetSet>,在这里我们如何使用Filterable  标题和地址

请参阅我的代码

自定义BaseAdapte

public class JobAdapater extends BaseAdapter implements Filterable {
private Context context;
private List<JobsGetSet> rowItems;
// private JobsGetSet JobItems;
private LayoutInflater layoutInflater;

// ArrayFilter mFilter;

public JobAdapater(Context context , List<JobsGetSet> rowItems2) {
    this.context = context;
    this.rowItems = rowItems2;
    layoutInflater = LayoutInflater.from(this.context);
}

@Override
public View getView(int position , View convertView , ViewGroup parent)
    {
        final JobsGetSet JobItems = (JobsGetSet) this.getItem(position);
        View view = null;
        try
            {
                layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = layoutInflater.inflate(R.layout.job_list_items , null);

                TextView textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);
                TextView textViewAddess = (TextView) view.findViewById(R.id.textViewAddess);
                TextView textViewCity = (TextView) view.findViewById(R.id.textViewCity);
                TextView textViewTime = (TextView) view.findViewById(R.id.textViewTime);
                TextView textViewMap = (TextView) view.findViewById(R.id.textViewMap);
                TextView textViewPH = (TextView) view.findViewById(R.id.textViewPHtext);
                TextView textViewNumber = (TextView) view.findViewById(R.id.textViewPhNumber);

                LinearLayout linearLayoutAdderss = (LinearLayout) view.findViewById(R.id.linearLayoutAdderss);

                Typeface typface = Typeface.createFromAsset(context.getAssets() , "fonts/SourceSansPro-Regular.otf");
                textViewTitle.setTypeface(typface , Typeface.BOLD);
                textViewAddess.setTypeface(typface);
                textViewCity.setTypeface(typface);
                textViewTime.setTypeface(typface);
                textViewMap.setTypeface(typface);
                textViewPH.setTypeface(typface , Typeface.BOLD);
                textViewNumber.setTypeface(typface);

                if (CommonMethods.openAreClos == true)
                    {
                        textViewTitle.setTextSize(18);
                        textViewAddess.setTextSize(16);
                        textViewCity.setTextSize(13);
                        textViewTime.setTextSize(12);
                        textViewNumber.setTextSize(16);
                        textViewMap.setTextSize(12);

                        linearLayoutAdderss.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
                        linearLayoutAdderss.getLayoutParams().width = 360;
                        linearLayoutAdderss.requestLayout();
                    } else
                    {
                        textViewTitle.setTextSize(15);
                        textViewAddess.setTextSize(13);
                        textViewCity.setTextSize(11);
                        textViewTime.setTextSize(9);
                        textViewNumber.setTextSize(13);
                        textViewMap.setTextSize(9);

                        linearLayoutAdderss.getLayoutParams().height = LayoutParams.WRAP_CONTENT;
                        linearLayoutAdderss.getLayoutParams().width = 260;
                        linearLayoutAdderss.requestLayout();
                    }
                Paint p = new Paint();
                p.setColor(Color.parseColor("#89BC58"));
                p.setFlags(Paint.UNDERLINE_TEXT_FLAG);
                textViewNumber.setPaintFlags(p.getFlags());

                String addess = JobItems.getAddress();
                addess = addess.replace("\n" , "").replace("\r" , "");

                textViewTitle.setText(JobItems.getCustomerName());
                textViewAddess.setText(addess);
                textViewCity.setVisibility(View.GONE);
                textViewTime.setText(JobItems.getTime());
                textViewNumber.setText(" " + JobItems.getContactMobile());


            } catch (Exception e)
            {
                e.printStackTrace();
            }

        return view;
    }

@Override
public int getCount()
    {
        return rowItems.size();
    }

@Override
public Object getItem(int position)
    {
        return rowItems.get(position);
    }

@Override
public long getItemId(int position)
    {
        return 0;
    }

@Override
public Filter getFilter()
    {

        return null;
    }
   }

GetSet类

public class JobsGetSet {

private String id;
private String userId;
private String companyID;
private String customerName;
private String estimateID;
private String eventID;
private String clientID;
private String estimatedDate;
private String time;
private String visitType;
private String visit;
private String serviceType;
private String status;
private String area;
private String jobTitle;
private String address;
private String contactName;
private String contactID;
private String contactMobile;
private String estimatedHours;
private String actualStartDate;
private String actTime;
private String actualFinishDate;
private String actualFinishTime;
private String actualDuration;
private String jobPriority;
private String completionNotes;
private String jobDescription;
private String specialInstruction;
private String statusID;
private String description;
private String isSend;


private String startDateTime;
private String endDateTime;
private String isSeen;



public String getStartDateTime()
    {
        return startDateTime;
    }
public void setStartDateTime(String startDateTime)
    {
        this.startDateTime = startDateTime;
    }
public String getEndDateTime()
    {
        return endDateTime;
    }
public void setEndDateTime(String endDateTime)
    {
        this.endDateTime = endDateTime;
    }
public String getId()
    {
        return id;
    }
public void setId(String id)
    {
        this.id = id;
    }
public String getUserId()
    {
        return userId;
    }
public void setUserId(String userId)
    {
        this.userId = userId;
    }
public String getCompanyID()
    {
        return companyID;
    }
public void setCompanyID(String companyID)
    {
        this.companyID = companyID;
    }
public String getCustomerName()
    {
        return customerName;
    }
public void setCustomerName(String customerName)
    {
        this.customerName = customerName;
    }
public String getEstimateID()
    {
        return estimateID;
    }
public void setEstimateID(String estimateID)
    {
        this.estimateID = estimateID;
    }
public String getEventID()
    {
        return eventID;
    }
public void setEventID(String eventID)
    {
        this.eventID = eventID;
    }
public String getClientID()
    {
        return clientID;
    }
public void setClientID(String clientID)
    {
        this.clientID = clientID;
    }
public String getEstimatedDate()
    {
        return estimatedDate;
    }
public void setEstimatedDate(String estimatedDate)
    {
        this.estimatedDate = estimatedDate;
    }
public String getTime()
    {
        return time;
    }
public void setTime(String time)
    {
        this.time = time;
    }
public String getVisitType()
    {
        return visitType;
    }
public void setVisitType(String visitType)
    {
        this.visitType = visitType;
    }
public String getVisit()
    {
        return visit;
    }
public void setVisit(String visit)
    {
        this.visit = visit;
    }
public String getServiceType()
    {
        return serviceType;
    }
public void setServiceType(String serviceType)
    {
        this.serviceType = serviceType;
    }
public String getStatus()
    {
        return status;
    }
public void setStatus(String status)
    {
        this.status = status;
    }
public String getArea()
    {
        return area;
    }
public void setArea(String area)
    {
        this.area = area;
    }
public String getJobTitle()
    {
        return jobTitle;
    }
public void setJobTitle(String jobTitle)
    {
        this.jobTitle = jobTitle;
    }
public String getAddress()
    {
        return address;
    }
public void setAddress(String address)
    {
        this.address = address;
    }
public String getContactName()
    {
        return contactName;
    }
public void setContactName(String contactName)
    {
        this.contactName = contactName;
    }
public String getContactID()
    {
        return contactID;
    }
public void setContactID(String contactID)
    {
        this.contactID = contactID;
    }
public String getContactMobile()
    {
        return contactMobile;
    }
public void setContactMobile(String contactMobile)
    {
        this.contactMobile = contactMobile;
    }
public String getEstimatedHours()
    {
        return estimatedHours;
    }
public void setEstimatedHours(String estimatedHours)
    {
        this.estimatedHours = estimatedHours;
    }
public String getActualStartDate()
    {
        return actualStartDate;
    }
public void setActualStartDate(String actualStartDate)
    {
        this.actualStartDate = actualStartDate;
    }
public String getActTime()
    {
        return actTime;
    }
public void setActTime(String actTime)
    {
        this.actTime = actTime;
    }
public String getActualFinishDate()
    {
        return actualFinishDate;
    }
public void setActualFinishDate(String actualFinishDate)
    {
        this.actualFinishDate = actualFinishDate;
    }
public String getActualFinishTime()
    {
        return actualFinishTime;
    }
public void setActualFinishTime(String actualFinishTime)
    {
        this.actualFinishTime = actualFinishTime;
    }
public String getActualDuration()
    {
        return actualDuration;
    }
public void setActualDuration(String actualDuration)
    {
        this.actualDuration = actualDuration;
    }
public String getJobPriority()
    {
        return jobPriority;
    }
public void setJobPriority(String jobPriority)
    {
        this.jobPriority = jobPriority;
    }
public String getCompletionNotes()
    {
        return completionNotes;
    }
public void setCompletionNotes(String completionNotes)
    {
        this.completionNotes = completionNotes;
    }
public String getJobDescription()
    {
        return jobDescription;
    }
public void setJobDescription(String jobDescription)
    {
        this.jobDescription = jobDescription;
    }
public String getSpecialInstruction()
    {
        return specialInstruction;
    }
public void setSpecialInstruction(String specialInstruction)
    {
        this.specialInstruction = specialInstruction;
    }
public String getStatusID()
    {
        return statusID;
    }
public void setStatusID(String statusID)
    {
        this.statusID = statusID;
    }
public String getDescription()
    {
        return description;
    }
public void setDescription(String description)
    {
        this.description = description;
    }
public String getIsSend()
    {
        return isSend;
    }
public void setIsSend(String isSend)
    {
        this.isSend = isSend;
    }
public String getIsSeen()
    {
        return isSeen;
    }
public void setIsSeen(String isSeen)
    {
        this.isSeen = isSeen;
    }

  }

1 个答案:

答案 0 :(得分:2)

我在很多地方使用了以下内容。

将以下内容添加到适配器:

private List<JobsGetSet> mOriginalValues;
private Filter mFilter;
private final Object mLock = new Object();

然后将你的getFilter改为

@Override
public Filter getFilter() {
    if (mFilter == null) {
        mFilter = new JobFilter();
    }
    return mFilter;
}

然后将JobFilter定义为内部类:

private class JobFilter extends Filter {
    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();

        if (mOriginalValues == null) {
            synchronized (mLock) {
                mOriginalValues = new ArrayList<JobsGetSet>(rowItems);
            }
        }

        if (constraint == null || constraint.length() == 0) {
            synchronized (mLock) {
                results.values = mOriginalValues;
                results.count = mOriginalValues.size();
            }
        }
        else {
            String con = constraint.toString();
            List<JobsGetSet> values = mOriginalValues;
            int count = values.size();

            List<JobsGetSet> newValues = new ArrayList<JobsGetSet>(count);

            for (int i = 0; i < count; i++) {
                JobsGetSet data = values.get(i);

                //Here is where you compare the constraint(title/address) with values in your JobsGetSet, or any other logic
                String s = data.getTitle();
                if (s.equalsIgnoreCase(con)) {
                    newValues.add(data);
                }
            }

            results.values = newValues;
            results.count = newValues.size();
        }

        return results;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
        // noinspection unchecked
        mObjects = (List<JobsGetSet>) results.values;
        notifyDataSetChanged();
        clear();
        addAll(mObjects);
        if (results.count > 0) {
            notifyDataSetChanged();
        }
        else {
        }
    }
}

然后使用它你就这样做了

mJobAdapater.getFilter().filter(stringToFilterBy);

很抱歉,但我可能错过了重命名的一些变量,但这可以节省你花时间弄明白的时间。