Я нашел много вопросов и ответов, связанных с моей проблемой. Но я не могу с этим справиться. У меня есть ListView, в котором отображаются все данные из БД. Теперь я должен отфильтровать из этого списка, когда я набираю буквы в editext. Фильтр, который я пытался, всегда показывал ошибку в классе Filter и FilterResult. Я пытаюсь найти решение со вчерашнего дня. Я знаю, что я что-то делаю неправильно, но я не могу найти, что это и где. Как исправить этот фильтр в моем коде.Пользовательский ListView с фильтром на textChanged
Показать все данные из БД:
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;
}
};
}
}
Что такое 'ncdbh'? откуда вы получаете данные? – pskink
ncdbh является объектом класса, который содержит функцию getAllCustomerBikeWithStatusOfP(), чтобы получить все байки из Sqlite DB –
, поэтому используйте 'SimpleCursorAdapter' с настройкой' FilterQueryProvider' – pskink