gpt4 book ai didi

java - AutoCompleteTextView 不显示任何下拉列表

转载 作者:行者123 更新时间:2023-11-30 02:38:57 26 4
gpt4 key购买 nike

我正在处理一项要求,其中使用自动完成 TextView 来获取匹配的Localities。用户必须输入几个字符,然后从服务器返回与这些字符匹配的 Localities 列表。自动完成 TextView 然后应将此列表显示为下拉列表(或弹出窗口)。出于某种原因,即使我从服务器接收到有效的 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 的 fragment )

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() 中,我检查了我是否收到了有效的地点列表,然后将它们推送到支持适配器的 LocalityList 中,但是下拉列表中没有显示这些地点。实际上没有显示下拉菜单。任何可能错误或遗漏的原因?

最佳答案

我觉得有一种更好的方法可以避免使用 AsyncTask 来处理这种情况.取而代之的是 Filter可以使用,它提供performFiltering()实际上在工作线程中运行的回调。在这里可以决定/准备匹配数据(例如通过服务器调用或类似方式)。当结果准备好时 publishResults在 UI 线程上运行的回调可用于修改支持自定义适配器的列表。我刚刚开始了解 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;
}
}

关于java - AutoCompleteTextView 不显示任何下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26059450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com