我是Android的初学者。我试图制作一个自定义列表视图过滤,它以某种方式工作。我班上有数组适配器。我在文本过滤器更改时使用该方法。我运行我的应用程序,没有错误。但结果与关键字不一样。
我的代码在onCreateView中,如果text关键字改变了
inputSearch=(EditText) rootView.findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
// adapter = new ListFPPAdapter();
//listProyekfpp.setAdapter(adapter);
FPPFragment.this.adapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
这是我的方法阵列适配器
class ListFPPAdapter extends ArrayAdapter<ClsProyek> {
ArrayList<ClsProyek> listOrginalProyek = null;
ArrayList<ClsProyek> FilterListProyek = null;
Filter filter;
ListFPPAdapter() {
super(getActivity(), R.layout.list_proyek_item, Proyek);
this.listOrginalProyek = new ArrayList<ClsProyek>(Proyek);
this.FilterListProyek = new ArrayList<ClsProyek>(Proyek);
}
@Override
public View getView(int pos, View row, ViewGroup viewGroup) {
ListProyekItemHolder holder = null;
if(row == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.list_proyek_item, viewGroup, false);
holder = new ListProyekItemHolder(row);
row.setBackgroundColor(android.R.drawable.list_selector_background);
row.setTag(holder);
} else {
holder = (ListProyekItemHolder)row.getTag();
}
holder.populateFrom(Proyek.get(pos));
return row;
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new ResultFilter();
}
return filter;
}
@Override
public int getCount() {
return FilterListProyek.size();
}
@Override
public ClsProyek getItem(int position) {
return FilterListProyek.get(position);
}
private class ResultFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
String filterText = constraint.toString().toLowerCase();
if (filterText == null || filterText.length() == 0) {
synchronized (this) {
results.values = listOrginalProyek;
results.count = listOrginalProyek.size();
}
} else {
ArrayList<ClsProyek> filterList = new ArrayList<ClsProyek>();
ArrayList<ClsProyek> originalList = new ArrayList<ClsProyek>();
synchronized (this) {
originalList.addAll(listOrginalProyek);
}
for (int i = 0, l = originalList.size(); i < l; i++) {
ClsProyek m = originalList.get(i);
final String Param=m.getNamaClient().toLowerCase();
if (Param.contains(filterText)) {
filterList.add(m);
}
}
results.values = filterList;
results.count = filterList.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
FilterListProyek = (ArrayList<ClsProyek>) results.values;
if(results.count > 0)
{
notifyDataSetChanged();
}else{
notifyDataSetInvalidated();
}
}
}
}
请帮助我。
答案 0 :(得分:0)
使用适配器中提供的排序方法来完成它。
adapter.sort(new Comparator<String>() {
@Override
public int compare(String lhs, String rhs) {
return lhs.compareTo(rhs); //Your sorting algorithm
}
});
有关详情,请参阅此问题:Sort listview with array adapter