有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android AutoCompleteTextView刷新

我正在使用Geocoder从字符串中获取匹配的地址

我正在尝试向AutoCompleteTextView显示返回的地址

当ILog.i("Result:"," "+list_of_addresses);时,我可以正确地看到这些值

因为我们讨论的是动态列表加载,所以我调用adapter.NotifyDataSetChanged();

我正在使用addTextChangedListener来侦听用户输入的文本中的更改

这就是代码的样子

public class Map extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    final Geocoder gc = new Geocoder(getApplicationContext(), Locale.getDefault());
    final ArrayList<String> address_name  = new  ArrayList<String>();
    final AutoCompleteTextView search = (AutoCompleteTextView) findViewById(R.id.search); 

    search.setThreshold(1);
    final ArrayAdapter<String> adapter =new ArrayAdapter<String>(this,
            安卓.R.layout.simple_dropdown_item_1line,address_name);

    search.setAdapter(adapter); //moved this line out of the try block

    search.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            List<Address> list = null;
            Address address = null;
            try {
                list = gc.getFromLocationName(s.toString(), 10);
                Log.i("List:", ""+list); //CAN see this log
            } catch (IOException e) {
                e.printStackTrace();
            }
            for(int i=0; i<list.size(); i++){

                    address = list.get(i);
                    address_name.add(address.getFeatureName().toString());
                    Log.i("Address: ", address.getFeatureName().toString()); //CANto see this Log

            }
            if(!list.isEmpty()){
                list.clear();
            }
            adapter.notifyDataSetChanged();
            search.showDropDown();
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
    });
}
}

在for循环中,我无法看到log.i("Addresses:",address.getFeatureName.toString());

但是,当我将for循环条件更改为i<=list.size()时,我确实获得了日志输出,但是应用程序强制关闭时出现了这个错误java.lang.IndexOutOfBoundsException: Invalid index 10, size is 10

也许这就是为什么我看不到地址列表

EDIT:我将for循环条件更改为i<list.size(),并在通知数据集更改后添加了search.showDropDown()

任何帮助都将不胜感激!谢谢


共 (2) 个答案

  1. # 1 楼答案

    该问题与适配器方法的错误或空重写有关:

    @Override
    public UPorPackageItem getItem(int index) {
        return mValues.get(index);
    }
    
    @Override
    public int getCount() {
        if (mValues == null)
            return 0;
        return mValues.size();
    }
    

    您必须实现上述方法。 它会起作用的

  2. # 2 楼答案

    你似乎在让它工作上有问题。我将粘贴对我非常有效的内容,只粘贴自动完成的相关部分

    onCreate

    pickUpAutoComplete = new AutoComplete(this, R.layout.pickupautocomplete);
    pickUpAutoComplete.setNotifyOnChange(true);
    locationText = (AutoCompleteTextView) findViewById(R.id.locationText);
    locationText.setOnItemClickListener(this);
    locationText.setOnFocusChangeListener(this);
    locationText.setAdapter(pickUpAutoComplete);
    

    布局xml

     <AutoCompleteTextView
            android:id="@+id/locationText"
            style="@style/registerLargestTextSize"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:layout_weight="6"
            android:background="#ffffff"
            android:completionThreshold="3"
            android:focusable="true"
            android:gravity="center"
            android:hint="home location"
            android:lines="3"
            android:maxLines="3"
            android:minLines="3"
            android:paddingBottom="3dp"
            android:paddingLeft="6dp"
            android:paddingRight="6dp"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbarStyle="outsideOverlay"
            android:textColor="#b2b2b2"
            android:textStyle="bold" />
    

    自动完成类,别忘了替换API密钥

    public class AutoComplete extends ArrayAdapter<String> implements Filterable {
        private static final String LOG_TAG = "carEgiri";
    
        private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place";
        private static final String TYPE_AUTOCOMPLETE = "/autocomplete";
        private static final String OUT_JSON = "/json";
    
        private static final String API_KEY = "**YOUR API KEY HERE**";
    
        private ArrayList<String> resultList;
    
        public AutoComplete(IPostAutoCompleteUIChange context,
                int textViewResourceId) {
            super((Context) context, textViewResourceId);
        }
    
        @Override
        public int getCount() {
            if (resultList == null)
                return 0;
            return resultList.size();
        }
    
        @Override
        public String getItem(int index) {
            return resultList.get(index);
        }
    
        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        resultList = autocomplete(constraint.toString());
    
                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }
    
                @Override
                protected void publishResults(CharSequence constraint,
                        FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };
            return filter;
        }