gpt4 book ai didi

android - android 中的地理编码器自动完成

转载 作者:可可西里 更新时间:2023-11-01 18:48:03 25 4
gpt4 key购买 nike

我一直在谷歌上搜索我的屁股,试图找到一个和我有同样问题的人,但不幸的是。所以这是我的问题:

我正在尝试在用户使用 Android 中的地理编码器键入地点名称时实现地址自动完成建议。我希望它的行为与使用组合框的 javascript 版本非常相似。

我正在使用一个带有 AutoCompleteTextView 的布局和一个数组适配器来在用户键入时动态更新建议列表。我在使用处理程序调用 geocoder.getFromLocationName 之前添加了 500 毫秒的延迟,从收到 onTextChanged() 事件开始。如果用户在 500 毫秒内键入更多字母,则最后一个事件将被取消。我遇到的问题是这些建议几乎从不在 UI 中显示为下拉列表中的可选项。我得到了地址建议,但是当我将它们添加到连接到 autocomplatetextview 的适配器时,它们根本不会显示。

我正在使用 API 级别 7 的模拟器上运行它,其中包括谷歌 API。

现在一些源代码可以帮助您:布局:

<LinearLayout android:id="@+id/searchInputLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="6dip"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/searchMessage" />
<EditText android:id="@+id/freetextInput"
android:hint="@string/searchFreetextLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background" />
<CheckBox android:id="@+id/includeVincinityCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/includeVincinityLabel"
android:checked="true"
android:onClick="includeVincinityClick" />
<AutoCompleteTextView android:id="@+id/locationInput"
android:hint="@string/locationInputHint"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/searchBtnLabel"
android:onClick="searchBtnClicked" />
</LinearLayout>

我的 Activity 的源代码(我省略了不相关的代码):

public class SearchLocationTabActivity extends Activity implements TextWatcher, OnItemSelectedListener {

private static final int MESSAGE_TEXT_CHANGED = 0;
private static final int AUTOCOMPLETE_DELAY = 500;
private static final int THRESHOLD = 3;
private String latitude, longitude;
private List<Address> autoCompleteSuggestionAddresses;
private ArrayAdapter<String> autoCompleteAdapter;
private Handler messageHandler;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.search);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);

messageHandler = new MyMessageHandler(this, this);
autoCompleteAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new ArrayList<String>());
autoCompleteAdapter.setNotifyOnChange(false);
AutoCompleteTextView locationinput = (AutoCompleteTextView) findViewById(R.id.locationInput);
locationinput.addTextChangedListener(this);
locationinput.setOnItemSelectedListener(this);
locationinput.setThreshold(THRESHOLD);
locationinput.setAdapter(autoCompleteAdapter);
}

@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
messageHandler.removeMessages(MESSAGE_TEXT_CHANGED);
}

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
String value = arg0.toString();
if (!"".equals(value) && value.length() >= THRESHOLD) {
Message msg = Message.obtain(messageHandler, MESSAGE_TEXT_CHANGED, arg0.toString());
messageHandler.sendMessageDelayed(msg, AUTOCOMPLETE_DELAY);
} else {
autoCompleteAdapter.clear();
}
}

@Override
public void afterTextChanged(Editable arg0) {
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
if (arg2 < autoCompleteSuggestionAddresses.size()) {
Address selected = autoCompleteSuggestionAddresses.get(arg2);
latitude = Double.toString(selected.getLatitude());
longitude = Double.toString(selected.getLongitude());
}
}

private void notifyResult(List<Address> suggestions) {
latitude = longitude = null;
autoCompleteAdapter.clear();
for (Address a : autoCompleteSuggestionAddresses) {
autoCompleteAdapter.add(a.toString());//TODO: figure out a nice way to display this address in list
}
autoCompleteAdapter.notifyDataSetChanged();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
latitude = longitude = null;
}

private class MyMessageHandler extends Handler {

private Context context;
private AsyncTaskSubscriber subscriber;

public MyMessageHandler(Context context, AsyncTaskSubscriber subscriber) {
this.context = context;
this.subscriber = subscriber;
}

@Override
public void handleMessage(Message msg) {
if (msg.what == MESSAGE_TEXT_CHANGED) {
String enteredText = (String) msg.obj;

try {
autoCompleteSuggestionAddresses = new Geocoder(context).getFromLocationName(enteredText, 10);

notifyResult(response);
} catch (IOException ex) {
Log.e(GeoCoderAsyncTask.class.getName(), "Failed to get autocomplete suggestions", ex);
}
}
}
}
}

非常感谢任何帮助!

最佳答案

对于那些没有设法删除过滤的人,这是我所做的(除了其他小的修改,但我认为它们不会对过滤部分产生影响)。另请注意,对于单击要检测的项目之一,you need to add an OnItemClickListener .

autoCompleteAdapter = new ArrayAdapterNoFilter(this, android.R.layout.simple_dropdown_item_1line);

其中 ArrayAdapterNoFilter 的灵感来自 this other answer :

public class ArrayAdapterNoFilter extends ArrayAdapter<String> {

public ArrayAdapterNoFilter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}

private static final NoFilter NO_FILTER = new NoFilter();

/**
* Override ArrayAdapter.getFilter() to return our own filtering.
*/
@Override
public Filter getFilter() {
return NO_FILTER;
}

/**
* Class which does not perform any filtering. Filtering is already done by
* the web service when asking for the list, so there is no need to do any
* more as well. This way, ArrayAdapter.mOriginalValues is not used when
* calling e.g. ArrayAdapter.add(), but instead ArrayAdapter.mObjects is
* updated directly and methods like getCount() return the expected result.
*/
private static class NoFilter extends Filter {
protected FilterResults performFiltering(CharSequence prefix) {
return new FilterResults();
}

protected void publishResults(CharSequence constraint, FilterResults results) {
// Do nothing
}
}
}

关于android - android 中的地理编码器自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9142885/

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