SchoolAdapter.java
5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.hjx.personalcenter.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import com.hjx.personalcenter.R;
import com.hjx.personalcenter.model.SchoolInfo;
import java.util.ArrayList;
import java.util.List;
/**
* Created by h on 2017/8/21.
*/
public class SchoolAdapter extends BaseAdapter implements Filterable {
private List<SchoolInfo.DataBean> mDatas;
ArrayList<SchoolInfo.DataBean> objects;
private Context context;
private final Object mLock = new Object();
private ArrayFilter mFilter;
public SchoolAdapter(ArrayList<SchoolInfo.DataBean> objects, Context context) {
this.objects = objects;
this.context = context;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ProvincesAdapter.ViewHolder holder = null;
if (convertView == null) {
convertView = View.inflate(context, R.layout.custom_adilog_school_list_items, null);
holder = new ProvincesAdapter.ViewHolder();
holder.nameText = (TextView) convertView.findViewById(R.id.list_school_items);
convertView.setTag(holder);
} else {
holder = (ProvincesAdapter.ViewHolder) convertView.getTag();
}
holder.nameText.setText(objects.get(position).getSchoolName());
return convertView;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ArrayFilter();
}
return mFilter;
}
static class ViewHolder {
TextView nameText;
}
private class ArrayFilter extends Filter {
//执行刷选
@Override
protected FilterResults performFiltering(CharSequence prefix) {
FilterResults results = new FilterResults();//过滤的结果
//原始数据备份为空时,上锁,同步复制原始数据
if (objects == null) {
synchronized (mLock) {
objects = new ArrayList<>(mDatas);
}
}
//当首字母为空时
if (prefix == null || prefix.length() == 0) {
ArrayList<SchoolInfo.DataBean> list;
synchronized (mLock) {//同步复制一个原始备份数据
list = new ArrayList<>(objects);
}
results.values = list;
results.count = list.size();//此时返回的results就是原始的数据,不进行过滤
} else {
String prefixString = prefix.toString().toLowerCase();//转化为小写
ArrayList<SchoolInfo.DataBean> values;
synchronized (mLock) {//同步复制一个原始备份数据
values = new ArrayList<>(objects);
}
final int count = values.size();
final ArrayList<SchoolInfo.DataBean> newValues = new ArrayList<>();
for (int i = 0; i < count; i++) {
final SchoolInfo.DataBean value = values.get(i);//从List<User>中拿到User对象
// final String valueText = value.toString().toLowerCase();
final String valueText = value.getSchoolName().toString().toLowerCase();//User对象的name属性作为过滤的参数
// First match against the whole, non-splitted value
if (valueText.startsWith(prefixString) || valueText.indexOf(prefixString.toString()) != -1) {//第一个字符是否匹配
newValues.add(value);//将这个item加入到数组对象中
} else {//处理首字符是空格
final String[] words = valueText.split(" ");
final int wordCount = words.length;
// Start at index 0, in case valueText starts with space(s)
for (int k = 0; k < wordCount; k++) {
if (words[k].startsWith(prefixString)) {//一旦找到匹配的就break,跳出for循环
newValues.add(value);
break;
}
}
}
}
results.values = newValues;//此时的results就是过滤后的List<User>数组
results.count = newValues.size();
}
return results;
}
//刷选结果
@Override
protected void publishResults(CharSequence prefix, FilterResults results) {
//noinspection unchecked
mDatas = (List<SchoolInfo.DataBean>) results.values;//此时,Adapter数据源就是过滤后的Results
if (results.count > 0) {
notifyDataSetChanged();//这个相当于从mDatas中删除了一些数据,只是数据的变化,故使用notifyDataSetChanged()
} else {
/**
* 数据容器变化 ----> notifyDataSetInValidated
容器中的数据变化 ----> notifyDataSetChanged
*/
notifyDataSetInvalidated();//当results.count<=0时,此时数据源就是重新new出来的,说明原始的数据源已经失效了
}
}
}
}