繁体   English   中英

Android AutoCompleteTextView刷新

[英]Android AutoCompleteTextView Refreshing

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

我正在尝试将返回的地址显示到AutoCompleteTextView。

当我Log.i("Result:"," "+list_of_addresses);时,我可以正确看到这些值Log.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,
            android.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

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

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

任何帮助,将不胜感激! 谢谢。

您似乎无法正常使用它。 我会粘贴对我非常有用的内容,仅粘贴AutoComplete的相关部分。

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 KEY

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;
    }

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

@Override
public UPorPackageItem getItem(int index) {
    return mValues.get(index);
}

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM