我正在制定一项要求,其中使用自动完成文字视图来获取匹配的地理位置。用户必须输入几个字符,并从服务器返回与这些字符匹配的位置列表。然后,自动完成文本视图应将此列表显示为下拉列表(或弹出窗口)。出于某种原因,即使我从服务器接收有效的Localities,自动完成下拉列表也不会出现。这是我的代码。
地点(型号)
public class Locality {
private int Id;
private String name;
private boolean selected;
//Getters and Setters
}
DonorSearchFragment.xml(包含AutoCompleteTextView的布局文件)
<AutoCompleteTextView
android:id="@+id/actLocality"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
LocalityListAdapter View(locality_row.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<TextView
android:id="@+id/txvLocalityName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
LocalityListAdapter.java
public class LocalityListAdapter extends ArrayAdapter<Locality> {
List<Locality> localities;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//The Current View
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.locality_row, null);
}
Locality currentLocality = localities.get(position);
if (currentLocality != null) {
TextView txvLocalityName = (TextView) view.findViewById(R.id.txvLocalityName);
txvLocalityName.setText(currentLocality.getName());
}
return view;
}
}
DonorSearchFragment.java(显示AutoCompleteTextView的片段)
public class DonorSearchFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
searchFragmentView = inflater.inflate(R.layout.fragment_donor_search, container, false);
//Locality AutoComplete
localityList = new ArrayList<Locality>();
AutoCompleteTextView actLocality = (AutoCompleteTextView) searchFragmentView.findViewById(R.id.actLocality);
localityListAdapter = new LocalityListAdapter(this.getActivity(), R.layout.locality_row, localityList);
actLocality.setAdapter(localityListAdapter);
actLocality.addTextChangedListener(new TextWatcher() {
...
@Override
public void afterTextChanged(Editable editable) {
new LocalitySearchTask().execute();
}
});
private class LocalitySearchTask extends AsyncTask<Void, Void, List<Locality>> {
...
protected List<Locality> doInBackground(Void... voids) {
List<Locality> localities = null;
//Call WebService and populate localities
return localities;
}
@Override
protected void onPostExecute(List<Locality> localities) {
//Refresh List backing the Adapter
//I see valid localities here, but somehow they are not shown by AutoComplete
localityListAdapter.clear();
for(Locality locality : localities){
localityList.add(locality);
}
localityListAdapter.notifyDataSetChanged();
}
}
}
在 onPostExecute()中,我检查了我是否收到了有效的Localities List,然后在支持适配器的LocalityList中推送它们,但下拉列表中未显示这些Localities。实际上没有显示下拉列表。什么原因可能是错误或遗失?
答案 0 :(得分:1)
我觉得有一种更好的方法可以处理这种情况,避免使用AsyncTask。相反,可以使用Filter,它提供实际在工作线程中运行的performFiltering()回调。这里可以决定/准备匹配数据(例如通过服务器调用或类似的)。在准备结果时,在UI线程上运行的publishResults回调可用于修改支持自定义适配器的列表。我刚刚开始了解Android适配器,所以我可能在这里不正确。这是我正在使用的代码
public class LocalityListAdapter extends ArrayAdapter<Locality> {
List<Locality> localities;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.locality_row, null);
}
Locality currentLocality = localities.get(position);
if (currentLocality != null) {
TextView txvLocalityName = (TextView) view.findViewById(R.id.txvLocalityName);
txvLocalityName.setText(currentLocality.getName());
}
return view;
}
@Override
public Filter getFilter() {
return localityFilter;
}
Filter localityFilter = new Filter() {
@Override
public CharSequence convertResultToString(Object resultValue) {
Locality locality = (Locality) resultValue;
return locality.getName();
}
protected FilterResults performFiltering(CharSequence constraint) {
List<Locality> localityList = new ArrayList<Locality>();
...Get Data from Server
//Return FilterResults created from response
FilterResults filterResults = new FilterResults();
filterResults.values = localityList;
filterResults.count = localityList.size();
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
localities.clear();
List<Locality> filteredList = (List<Locality>) filterResults.values;
for(Locality locality : filteredList) {
localities.add(locality);
}
notifyDataSetChanged();
}
}
}
DonorSearchFragment.java(来自MainActivity)
public class DonorSearchFragment extends Fragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
searchFragmentView = inflater.inflate(R.layout.fragment_donor_search, container, false);
//Locality AutoComplete
localityList = new ArrayList<Locality>();
actLocality = (AutoCompleteTextView) searchFragmentView.findViewById(R.id.actLocality);
localityListAdapter = new LocalityListAdapter(this.getActivity(), R.layout.locality_row, localityList);
actLocality.setAdapter(localityListAdapter);
...
return searchFragmentView;
}
}