有人可以告诉我为什么在使用过滤器时屏幕上没有任何变化。在调试下我可以看到:
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilteredEmployeeItems = (ArrayList<EmployeeItem>) results.values;
notifyDataSetChanged();
}
}
}
mFilteredEmployeeItems具有正确的筛选元素数,但在notifyDataSetChanged()之后,屏幕上没有任何更改。
这是我的完整代码:
public class EmployeesFragment extends Fragment {
private static final int item_Header = 0;
private static final int item_Employee = 1;
public static class EmployeeItem {
private int mItemType;
private String mGUID;
private String mName;
private String mCardID;
public EmployeeItem(int AItemType, String AGUID, String AName, String ACardID) {
mItemType = AItemType;
mGUID = AGUID;
mName = AName;
mCardID = ACardID;
}
public int getItemType() {
return mItemType;
}
public String getGUID() {
return mGUID;
}
public String getName() {
return mName;
}
public String getCardID() {
return mCardID;
}
}
private class EmployeeItemsAdapter extends ArrayAdapter<EmployeeItem> implements Filterable {
public EmployeeItemsAdapter(Context context) {
super(context, R.layout.groupheader_empty, mFilteredEmployeeItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
EmployeeItem vEmployeeItem = getItem(position);
switch (vEmployeeItem.getItemType()) {
case item_Header:
if (vEmployeeItem.getName().equals("")) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.groupheader_empty, null);
} else {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.groupheader_text, null);
}
((TextView) convertView.findViewById(R.id.header_txt)).setText(vEmployeeItem.getName());
break;
case item_Employee:
convertView = LayoutInflater.from(getContext()).inflate(R.layout.universallist_item, null);
((TextView) convertView.findViewById(R.id.UniversalItem_CaptionTxt)).setText(vEmployeeItem.getName());
break;
}
return convertView;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mFilteredEmployeeItems = (ArrayList<EmployeeItem>) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
ArrayList<EmployeeItem> FilteredArrayNames = new ArrayList<EmployeeItem>();
if (constraint == null || constraint.length() == 0) {
results.count = mFullEmployeeItems.size();
results.values = mFullEmployeeItems;
} else {
constraint = constraint.toString().toLowerCase();
for (int i = 0; i < mFullEmployeeItems.size(); i++) {
EmployeeItem dataNames = mFullEmployeeItems.get(i);
if (dataNames.getName().toLowerCase().startsWith(constraint.toString())) {
FilteredArrayNames.add(dataNames);
}
}
}
results.count = FilteredArrayNames.size();
results.values = FilteredArrayNames;
return results;
}
};
return filter;
}
}
public EmployeesFragment() {
// Required empty public constructor
}
private static View mRootView;
private List<EmployeeItem> mFullEmployeeItems = new ArrayList<EmployeeItem>();
private List<EmployeeItem> mFilteredEmployeeItems = new ArrayList<EmployeeItem>();
private ArrayAdapter<EmployeeItem> mAdapter;
private int mPosition;
private EditText mSearch;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_employees, container, false);
mAdapter = null;
doGenerateFormElements();
mSearch = (EditText) mRootView.findViewById(R.id.search_edt);
mSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Call back the Adapter with current character to Filter
mAdapter.getFilter().filter(s.toString().trim());
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
return mRootView;
}
private void doGenerateFormElements() {
String vSQLText;
switch (AppCommon.mActualCardAction) {
case AppCommon.C_CardAction_Remove:
vSQLText = "select EmployeeGUID, EmployeeName, CardID from TempEmployeesList where (CardID <> '') order by EmployeeName;";
break;
default:
vSQLText = "select EmployeeGUID, EmployeeName, CardID from TempEmployeesList order by EmployeeName;";
break;
}
mFullEmployeeItems.clear();
mFilteredEmployeeItems.clear();
mFullEmployeeItems.add(new EmployeeItem(item_Header, "", "", ""));
mFilteredEmployeeItems.add(new EmployeeItem(item_Header, "", "", ""));
SQLiteDatabase vSQLiteDatabase = DatabaseManager.getInstance().openDatabase();
Cursor vCursor = null;
try {
vCursor = AppCommon.SQLite_SelectRecords(vSQLiteDatabase, vSQLText);
if (vCursor.getCount() != 0) {
do {
mFullEmployeeItems.add(new EmployeeItem(item_Employee,
AppCommon.Cursor_GetValueAsString(vCursor, AppCommon.Fld_EmployeeGUID),
AppCommon.Cursor_GetValueAsString(vCursor, AppCommon.Fld_EmployeeName),
AppCommon.Cursor_GetValueAsString(vCursor, AppCommon.Fld_CardID)));
mFilteredEmployeeItems.add(new EmployeeItem(item_Employee,
AppCommon.Cursor_GetValueAsString(vCursor, AppCommon.Fld_EmployeeGUID),
AppCommon.Cursor_GetValueAsString(vCursor, AppCommon.Fld_EmployeeName),
AppCommon.Cursor_GetValueAsString(vCursor, AppCommon.Fld_CardID)));
} while (vCursor.moveToNext());
//mFilteredEmployeeItems = mFullEmployeeItems;
}
} catch (SQLiteException sqle) {
Log.e("Employees", "doGenerateFormElements error: " + sqle.toString());
} finally {
if (vCursor != null) {
vCursor.close();
}
DatabaseManager.getInstance().closeDatabase();
}
doRefreshAdapterContent();
}
private void doRefreshAdapterContent() {
if (mAdapter == null) {
mAdapter = new EmployeeItemsAdapter(mRootView.getContext());
ListView listView = (ListView) mRootView.findViewById(R.id.Employees_List);
//Here i analyse items OnClick
listView.setAdapter(mAdapter);
} else {
mAdapter.notifyDataSetChanged();
}
}