|
|
@@ -1,8 +1,10 @@
|
|
|
package com.usai.redant.CommonEditor.CommonEditor;
|
|
|
|
|
|
import android.content.Context;
|
|
|
+import android.util.Log;
|
|
|
import android.view.View;
|
|
|
import android.view.ViewGroup;
|
|
|
+import android.widget.ArrayAdapter;
|
|
|
import android.widget.BaseAdapter;
|
|
|
import android.widget.Filter;
|
|
|
import android.widget.Filterable;
|
|
|
@@ -15,21 +17,29 @@ import java.util.List;
|
|
|
* Created by macmini1 on 2017/12/18.
|
|
|
*/
|
|
|
|
|
|
-public class CommonEditorAutoCompleteAdapter extends BaseAdapter implements Filterable {
|
|
|
+public class CommonEditorAutoCompleteAdapter extends ArrayAdapter implements Filterable {
|
|
|
|
|
|
private Context context;
|
|
|
private List<String> list = new ArrayList<>();
|
|
|
- public List<String> dataSource;
|
|
|
- private int resourceId,textViewId;
|
|
|
+ private int resourceId, textViewId;
|
|
|
|
|
|
- public CommonEditorAutoCompleteAdapter(Context context, int resourceId, int textViewId, List<String> dataSource) {
|
|
|
+ public CommonEditorAutoCompleteAdapter(Context context, int resourceId, int textViewId) {
|
|
|
+ super(context,resourceId,textViewId);
|
|
|
this.context = context;
|
|
|
- this.dataSource = dataSource;
|
|
|
+ this.resourceId = resourceId;
|
|
|
+ this.textViewId = textViewId;
|
|
|
}
|
|
|
|
|
|
- public void setDataSource(List<String> dataSource) {
|
|
|
- this.dataSource = dataSource;
|
|
|
- notifyDataSetChanged();
|
|
|
+ private String[] arr;
|
|
|
+ public void addObjects(String[] arr) {
|
|
|
+ this.arr = arr;
|
|
|
+ addAll((Object[]) arr);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void clear() {
|
|
|
+ arr = null;
|
|
|
+ super.clear();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -43,20 +53,25 @@ public class CommonEditorAutoCompleteAdapter extends BaseAdapter implements Filt
|
|
|
@Override
|
|
|
protected FilterResults performFiltering(CharSequence constraint) {
|
|
|
FilterResults results = new FilterResults();
|
|
|
- if (constraint == null || constraint.length() == 0 || dataSource == null || dataSource.size() == 0) {
|
|
|
+
|
|
|
+ Log.d("Auto Complete Adapter", "performFiltering: " + constraint.toString());
|
|
|
+ int count = getCount();
|
|
|
+ if (constraint == null || constraint.length() == 0 || count == 0) {
|
|
|
//
|
|
|
results.values = null;
|
|
|
results.count = 0;
|
|
|
} else {
|
|
|
//这个newList是实际搜索出的结果集合,实际上是将该newList的数据赋给了list
|
|
|
List<String> newList = new ArrayList<>();
|
|
|
- for (String s : dataSource) {
|
|
|
+
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ String s = (String) getItem(i);
|
|
|
//包含就添加到newList中
|
|
|
- if (s.contains(constraint.toString().trim()
|
|
|
- )) {
|
|
|
+ if (s.contains(constraint.toString().trim())) {
|
|
|
newList.add(s);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
//将newList传给results
|
|
|
results.values = newList;
|
|
|
results.count = newList.size();
|
|
|
@@ -73,14 +88,14 @@ public class CommonEditorAutoCompleteAdapter extends BaseAdapter implements Filt
|
|
|
@Override
|
|
|
protected void publishResults(CharSequence constraint, FilterResults results) {
|
|
|
|
|
|
- // 主动再调用一次,获取最新的
|
|
|
- results = performFiltering(constraint);
|
|
|
+ Log.d("Auto Complete Adapter", "publishResults: " + results.count);
|
|
|
|
|
|
if (results != null && results.count > 0) {//有符合过滤规则的数据
|
|
|
list.clear();
|
|
|
list.addAll((List<String>) results.values);
|
|
|
notifyDataSetChanged();
|
|
|
} else {//没有符合过滤规则的数据
|
|
|
+ clear();
|
|
|
notifyDataSetInvalidated();
|
|
|
}
|
|
|
}
|
|
|
@@ -101,7 +116,12 @@ public class CommonEditorAutoCompleteAdapter extends BaseAdapter implements Filt
|
|
|
|
|
|
@Override
|
|
|
public int getCount() {
|
|
|
- return list != null && list.size() > 0 ? list.size() : 0;
|
|
|
+
|
|
|
+ if (arr != null) {
|
|
|
+ return arr.length;
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -112,14 +132,12 @@ public class CommonEditorAutoCompleteAdapter extends BaseAdapter implements Filt
|
|
|
*/
|
|
|
@Override
|
|
|
public Object getItem(int position) {
|
|
|
- return list.get(position);
|
|
|
+ return arr[position];
|
|
|
}
|
|
|
-
|
|
|
@Override
|
|
|
public long getItemId(int position) {
|
|
|
return position;
|
|
|
}
|
|
|
-
|
|
|
@Override
|
|
|
public View getView(final int position, View convertView, ViewGroup parent) {
|
|
|
TvViewHolder holder;
|
|
|
@@ -134,12 +152,11 @@ public class CommonEditorAutoCompleteAdapter extends BaseAdapter implements Filt
|
|
|
//注意这里不要为convertView添加点击事件,默认是点击后:①下拉窗收起;
|
|
|
//②点击的条目数据会显示在搜索框中;③光标定位到字符串末位。
|
|
|
//如果自己添加点击事件,就要首先实现上面的①、②、③。
|
|
|
- holder.tv.setText(list.get(position));
|
|
|
+
|
|
|
+ holder.tv.setText((String) getItem(position));
|
|
|
return convertView;
|
|
|
}
|
|
|
-
|
|
|
class TvViewHolder {
|
|
|
TextView tv;
|
|
|
}
|
|
|
-
|
|
|
}
|