簡體   English   中英

AutoCompleteTextView不顯示任何下拉列表

[英]AutoCompleteTextView does not display any dropdown

我正在使用自動完成文本視圖來獲取匹配的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視圖(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()中,我檢查是否收到了有效的“地區列表”,然后將其推入支持適配器的LocalityList中,但是下拉列表中未顯示這些地區。 實際上,沒有顯示下拉列表。 有什么原因可能導致錯誤或遺漏?

我覺得有一種更好的方法來處理這種情況,從而避免使用AsyncTask 而是可以使用Filter ,該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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM