Commit bfaa7264b36ae68733981113e4f96303558ab305

Authored by xiongwei
1 parent 22599b8b76
Exists in master

学校无法搜索问题解决

PersonalCenter/app/src/main/java/com/hjx/personalcenter/adapter/SchoolAdapter.java
1 1 package com.hjx.personalcenter.adapter;
2 2  
3 3 import android.content.Context;
  4 +import android.os.Handler;
  5 +import android.os.Message;
4 6 import android.view.View;
5 7 import android.view.ViewGroup;
6 8 import android.widget.BaseAdapter;
... ... @@ -9,25 +11,27 @@ import android.widget.Filterable;
9 11 import android.widget.TextView;
10 12  
11 13 import com.hjx.personalcenter.R;
  14 +import com.hjx.personalcenter.http.HttpCode;
12 15 import com.hjx.personalcenter.model.SchoolInfo;
13 16  
14 17 import java.util.ArrayList;
15   -import java.util.List;
16 18  
17 19 /**
18 20 * Created by h on 2017/8/21.
19 21 */
20 22  
21 23 public class SchoolAdapter extends BaseAdapter implements Filterable {
22   - private List<SchoolInfo.DataBean> mDatas;
23   - ArrayList<SchoolInfo.DataBean> objects;
  24 + private ArrayList<SchoolInfo.DataBean> mDatas;
  25 + private ArrayList<SchoolInfo.DataBean> objects;
24 26 private Context context;
25 27 private final Object mLock = new Object();
26 28 private ArrayFilter mFilter;
  29 + Handler handler = null;
27 30  
28   - public SchoolAdapter(ArrayList<SchoolInfo.DataBean> objects, Context context) {
  31 + public SchoolAdapter(ArrayList<SchoolInfo.DataBean> objects, Context context,Handler handler) {
29 32 this.objects = objects;
30 33 this.context = context;
  34 + this.handler = handler;
31 35 }
32 36  
33 37 @Override
... ... @@ -37,7 +41,7 @@ public class SchoolAdapter extends BaseAdapter implements Filterable {
37 41  
38 42 @Override
39 43 public Object getItem(int position) {
40   - return position;
  44 + return objects.get(position);
41 45 }
42 46  
43 47 @Override
... ... @@ -82,16 +86,16 @@ public class SchoolAdapter extends BaseAdapter implements Filterable {
82 86 protected FilterResults performFiltering(CharSequence prefix) {
83 87 FilterResults results = new FilterResults();//过滤的结果
84 88 //原始数据备份为空时,上锁,同步复制原始数据
85   - if (objects == null) {
  89 + if (mDatas == null) {
86 90 synchronized (mLock) {
87   - objects = new ArrayList<>(mDatas);
  91 + mDatas = new ArrayList<SchoolInfo.DataBean>(objects);
88 92 }
89 93 }
90 94 //当首字母为空时
91 95 if (prefix == null || prefix.length() == 0) {
92   - ArrayList<SchoolInfo.DataBean> list;
  96 + ArrayList<SchoolInfo.DataBean> list ;
93 97 synchronized (mLock) {//同步复制一个原始备份数据
94   - list = new ArrayList<>(objects);
  98 + list = new ArrayList<SchoolInfo.DataBean>(mDatas);
95 99 }
96 100 results.values = list;
97 101 results.count = list.size();//此时返回的results就是原始的数据,不进行过滤
... ... @@ -100,10 +104,10 @@ public class SchoolAdapter extends BaseAdapter implements Filterable {
100 104  
101 105 ArrayList<SchoolInfo.DataBean> values;
102 106 synchronized (mLock) {//同步复制一个原始备份数据
103   - values = new ArrayList<>(objects);
  107 + values = new ArrayList<SchoolInfo.DataBean>(mDatas);
104 108 }
105 109 final int count = values.size();
106   - final ArrayList<SchoolInfo.DataBean> newValues = new ArrayList<>();
  110 + final ArrayList<SchoolInfo.DataBean> newValues = new ArrayList<SchoolInfo.DataBean>(count);
107 111  
108 112 for (int i = 0; i < count; i++) {
109 113 final SchoolInfo.DataBean value = values.get(i);//从List<User>中拿到User对象
... ... @@ -135,7 +139,11 @@ public class SchoolAdapter extends BaseAdapter implements Filterable {
135 139 @Override
136 140 protected void publishResults(CharSequence prefix, FilterResults results) {
137 141 //noinspection unchecked
138   - mDatas = (List<SchoolInfo.DataBean>) results.values;//此时,Adapter数据源就是过滤后的Results
  142 + objects = (ArrayList<SchoolInfo.DataBean>) results.values;//此时,Adapter数据源就是过滤后的Results
  143 + Message message = Message.obtain();
  144 + message.what = HttpCode.SCOOL;
  145 + message.obj = objects;
  146 + handler.sendMessage(message);
139 147 if (results.count > 0) {
140 148 notifyDataSetChanged();//这个相当于从mDatas中删除了一些数据,只是数据的变化,故使用notifyDataSetChanged()
141 149 } else {
... ...
PersonalCenter/app/src/main/java/com/hjx/personalcenter/customdialog/SchoolListDialog.java
... ... @@ -74,6 +74,11 @@ public class SchoolListDialog extends BaseCircleDialog implements AdapterView.On
74 74 data.addAll( (List<SchoolInfo.DataBean>)msg.obj);
75 75 listadapter.notifyDataSetChanged();
76 76 break;
  77 + case HttpCode.SCOOL:
  78 + data.clear();
  79 + data.addAll( (List<SchoolInfo.DataBean>)msg.obj);
  80 + listadapter.notifyDataSetChanged();
  81 + break;
77 82 }
78 83 }
79 84 };
... ... @@ -123,7 +128,7 @@ public class SchoolListDialog extends BaseCircleDialog implements AdapterView.On
123 128 country =SaveParam.getInstance().getLoginParam(getActivity(), SaveParam.ADRESSID);
124 129  
125 130 }
126   - listadapter = new SchoolAdapter(data, getActivity());
  131 + listadapter = new SchoolAdapter(data, getActivity(),handler);
127 132 school_list.setAdapter(listadapter);
128 133 school_list.setOnItemClickListener(this);
129 134 cance.setOnClickListener(new View.OnClickListener() {
... ... @@ -155,11 +160,12 @@ public class SchoolListDialog extends BaseCircleDialog implements AdapterView.On
155 160 public void onTextChanged(CharSequence s, int start, int before, int count) {
156 161 // mAdapter.getFilter().filter(s);
157 162  
158   - listadapter.getFilter().filter(s);
  163 + listadapter.getFilter().filter(school_sech.getText().toString());
159 164 }
160 165  
161 166 @Override
162 167 public void afterTextChanged(Editable s) {
  168 + //listadapter.notifyDataSetInvalidated();
163 169  
164 170 }
165 171 });
... ...
PersonalCenter/app/src/main/java/com/hjx/personalcenter/http/HttpCode.java
... ... @@ -59,6 +59,8 @@ public class HttpCode {
59 59 public static final int PESERNAOL = 28;
60 60 //touxan
61 61 public static final int TOUXIANG = 29;
  62 + //学校
  63 + public static final int SCOOL = 29;
62 64  
63 65  
64 66 }
... ...
PersonalCenter/app/src/main/res/layout/activity_main.xml
... ... @@ -590,7 +590,6 @@
590 590 <View
591 591 android:layout_width="5dp"
592 592 android:layout_height="20dp"
593   - android:layout_marginTop="10dp"
594 593 android:background="@color/login_text_blue">
595 594  
596 595 </View>
... ... @@ -598,7 +597,6 @@
598 597 <TextView
599 598 android:layout_width="wrap_content"
600 599 android:layout_height="wrap_content"
601   - android:layout_marginTop="10dp"
602 600 android:layout_marginLeft="5dp"
603 601 android:text="版本信息"
604 602 android:textSize="22sp" />
... ...