我发现了许多与我的问题有关的问题和答案。但我无法实现这些目标。我有一个ListView,其中显示了来自DB的所有数据。现在,当我在editext上输入字母时,我必须从该列表中过滤。我尝试的过滤器总是在Filter和FilterResult类中显示错误。我试图从昨天找到解决方案。我知道,我做错了什么事,但我无法找到那是什么,在哪里。如何在我的代码中修复此过滤器。
显示来自DB的所有数据:
private void getAdapterView() {
List<NewCustomerGetSetter> cont = ncdbh.getAllCustomerBikeWithStatusOfP(); // Get all the value from DB
for (NewCustomerGetSetter cn : cont) {
cust_name.add(cn.getName()); //Below all are List
cust_mobile.add(cn.getMobile1());
vehicle_number.add(cn.getBikeNo());
from_date.add(cn.getFromDate());
to_date.add(cn.getToDate());
days.add(cn.getTotalDays());
amount.add(cn.getTotalCost());
Log.d(cn.getID()+"",cn.getName()+" "+cn.getMobile1()+" "+count);
count++;
}
// Pass values to custom Adapter
list_return.setAdapter(new ReturnVehicleCustomAdapter(this, cust_name, cust_mobile, vehicle_number,from_date,to_date,days,amount));
}
AfterTextChanged功能
search.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable arg0) {
//Not coded. Depends on Shorted filter problem Solved need to think how to pass/ call that filter.
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
});
自定义适配器类:
public class ReturnVehicleCustomAdapter extends BaseAdapter{
private Filter mFilter;
//ReturnVehicle rv = new ReturnVehicle();
List<String> cust_mobile = new ArrayList<String>();
List<String> vehicle_number = new ArrayList<String>();
List<String> cust_name = new ArrayList<String>();
List<String> cust_from = new ArrayList<String>();
List<String> cust_to = new ArrayList<String>();
List<String> cust_days = new ArrayList<String>();
List<String> cust_amount = new ArrayList<String>();
Context context, context1;
private LayoutInflater inflater = null;
public ReturnVehicleCustomAdapter(ReturnVehicle vehicleMasterClass, List<String> cust_name_,
List<String> cust_mobile_, List<String> vehicle_number_,List<String> cust_from_,
List<String> cust_to_, List<String> cust_days_,List<String> vehicle_amount_) {
cust_name=cust_name_;
cust_mobile=cust_mobile_;
vehicle_number=vehicle_number_;
cust_to=cust_to_;
cust_from=cust_from_;
cust_days=cust_days_;
cust_amount=vehicle_amount_;
context=vehicleMasterClass;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public ReturnVehicleCustomAdapter(ReturnVehicle returnVehicle) {
}
@Override
public int getCount() {
return cust_mobile.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;
}
public class Holder1 {
TextView tv;
TextView tv1;
TextView tv2;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final Object[] cust_mobile_obj = cust_mobile.toArray();
final Object[] vehicle_number_obj = vehicle_number.toArray();
final Object[] cust_name_obj = cust_name.toArray();
final Object[] cust_from_obj = cust_from.toArray();
final Object[] cust_to_obj = cust_to.toArray();
final Object[] cust_days_obj = cust_days.toArray();
final Object[] cust_amount_obj = cust_amount.toArray();
Holder1 holder1 = new Holder1();
View rowView1 = inflater.inflate(R.layout.return_custom_listview, null);
holder1.tv = (TextView) rowView1.findViewById(R.id.r_name);
holder1.tv1 = (TextView) rowView1.findViewById(R.id.r_mobile);
holder1.tv2 = (TextView) rowView1.findViewById(R.id.r_bike);
cust_mobile.toString();
holder1.tv.setText((CharSequence) vehicle_number_obj[position]);
holder1.tv1.setText((CharSequence) cust_name_obj[position]);
holder1.tv2.setText((CharSequence) cust_mobile_obj[position]);
rowView1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((ReturnVehicle) context).name_ = cust_name_obj[position].toString();
((ReturnVehicle) context).mobile_ = cust_mobile_obj[position].toString();
((ReturnVehicle) context).bike1 = vehicle_number_obj[position].toString();
((ReturnVehicle) context).from_date1 = cust_from_obj[position].toString();
((ReturnVehicle) context).to_date1 = cust_to_obj[position].toString();
((ReturnVehicle) context).days1 = cust_days_obj[position].toString();
((ReturnVehicle) context).amount1 = cust_amount_obj[position].toString();
((ReturnVehicle) context).name.setText(((ReturnVehicle)context).name_);
((ReturnVehicle) context).mobile.setText(((ReturnVehicle)context).mobile_);
((ReturnVehicle) context).bike.setText(((ReturnVehicle)context).bike1);
((ReturnVehicle) context).from.setText(((ReturnVehicle)context).from_date1);
((ReturnVehicle) context).eto.setText(((ReturnVehicle)context).to_date1);
((ReturnVehicle) context).tdays.setText(((ReturnVehicle)context).days1);
((ReturnVehicle) context).price.setText(((ReturnVehicle)context).amount1);
((ReturnVehicle) context).extend.setText(((ReturnVehicle)context).df.format(((ReturnVehicle) context).c.getTime()).toString());
((ReturnVehicle)context).diffDateSet();
//Toast.makeText(context, "You Clicked "+cust_mobile_obj[position], Toast.LENGTH_LONG).show();
}
});
return rowView1;
}
@Override
public Filter getFilter() {
return new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Log.d("", "**** PUBLISHING RESULTS for: " + constraint);
vehicle_number = (List<String>) results.values;
ReturnVehicleCustomAdapter.this.notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.d("", "**** PERFORM FILTERING for: " + constraint);
List<String> filteredResults = getFilteredResults(constraint);
FilterResults results = new FilterResults();
results.values = filteredResults;
return results;
}
private List<String> getFilteredResults(CharSequence constraint) {
if (constraint.length() == 0) {
return vehicle_number;
}
List<String> listResult = new ArrayList<String>();
for (String obj : vehicle_number){
// if (condition){
listResult.add(obj);
// }
}
return listResult;
}
};
}
}
答案 0 :(得分:1)
我觉得SimpleCursorAdapter很难实现。所以我找到了Filter Method过滤的解决方案。以下是代码。
@Override
public Filter getFilter() {
if(valueFilter==null) {
valueFilter=new ValueFilter();
}
return valueFilter;
}
/**This class do all the filters*/
private class ValueFilter extends Filter {
/**Invoked in a worker thread to filter the data according to the constraint.*/
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results=new FilterResults();
if(constraint!=null && constraint.length()>0){
ArrayList<ReturnVehicleListValueGetSet> filterList=new ArrayList<ReturnVehicleListValueGetSet>();
for(int i=0;i<filter_list.size();i++){
if((filter_list.get(i).getName().toUpperCase()).contains(constraint.toString().toUpperCase())) {
ReturnVehicleListValueGetSet contacts = new ReturnVehicleListValueGetSet();
contacts.setName(filter_list.get(i).getName());
contacts.setMob(filter_list.get(i).getMob());
contacts.setVehNum(filter_list.get(i).getVehNum());
filterList.add(contacts);
}
}
results.count=filterList.size();
results.values=filterList;
}else{
results.count=filter_list.size();
results.values=filter_list;
}
return results;
}
/**Invoked in the UI thread to publish the filtering results in the user interface.*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
veh_list=(ArrayList<ReturnVehicleListValueGetSet>) results.values;
notifyDataSetChanged();
}
}