gpt4 book ai didi

Android - AutoCompleteTextView 仅在退格时有效

转载 作者:行者123 更新时间:2023-11-29 14:21:51 25 4
gpt4 key购买 nike

我有一个 AutoCompleteTextView 可以在用户键入时动态更新建议列表。我的问题是,当我键入时,列表得到更新,但未显示下拉列表!但是当我删除一个字符(退格键)时,下拉菜单出现了!

我什至尝试在文本更改后显式调用 autoText.showDropDown(); 但它不起作用。

这是我的 TextChangedListener:

private TextWatcher textChecker = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if(autoText.length()>9){
new GeoTask().execute();
}
}

@Override
public void afterTextChanged(Editable editable) {
if(autoText.length()>9){
autoText.showDropDown();
Log.i("UPDATE","showDropDown");
}
}
};

我什至尝试在 autoText.showDropDown(); 上方重置适配器,但仍然没有:

autoText.setAdapter(null);
autoText.setAdapter(adapter);

有没有办法让它工作?


编辑:这是 Activity 的完整代码。也许有人可以发现我做错了什么......

public class TestDoctor extends Activity {

TextView latText;
TextView lngText;
AutoCompleteTextView autoText;
ArrayAdapter<String> adapter;

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

latText = (TextView) findViewById(R.id.latTextView);
lngText = (TextView) findViewById(R.id.longTextView);
autoText = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);


Button button = (Button) findViewById(R.id.buttonDoctor);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new GeoTask().execute();
}
});
autoText.setThreshold(10);
autoText.setHint("Οδός Αριθμός, Περιοχή");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
autoText.setAdapter(adapter);
adapter.setNotifyOnChange(true);
autoText.addTextChangedListener(textChecker);
}

private TextWatcher textChecker = new TextWatcher() {

@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3){}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if(autoText.length()>9){
new GeoTask().execute();
}
}

@Override
public void afterTextChanged(Editable editable) {}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private List<SimpleAddress> getAddressesFromText(String address) {
address = address.replace(' ', '+');
HttpHelper httpHelper = new HttpHelper();
InputStream inputStream = httpHelper.makeHttpGetRequest("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
String response = httpHelper.inputStreamToString(inputStream);

List<SimpleAddress> simpleAddresses = new ArrayList<SimpleAddress>();
try {
JSONObject jsonObject = new JSONObject(response);


int size = ((JSONArray) jsonObject.get("results")).length();

for (int i = 0; i<size; i++){
String formatted_address = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getString("formatted_address");

Double lng = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");

Double lat = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
simpleAddresses.add(i,new SimpleAddress(formatted_address,lat,lng));
}
return simpleAddresses;
} catch (JSONException e) {
return null;
}
}

public class GeoTask extends AsyncTask<Void, Void, Void> {
List<SimpleAddress> simpleAddresses = new ArrayList<SimpleAddress>();

@Override
protected Void doInBackground(Void... voids) {
Log.i("UPDATE","1");
try {
simpleAddresses = getAddressesFromText(autoText.getText().toString());
} catch (NullPointerException e) {
}
Log.i("UPDATE","2");
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
Log.i("UPDATE", "3");

int size = simpleAddresses.size();

if(size > 0){
adapter.clear();
Log.i("ADAPTER_SIZE",""+size);
for (int i = 0; i< size; i++){
adapter.add(simpleAddresses.get(i).getFormatted_address());
Log.i("ADDED",simpleAddresses.get(i).getFormatted_address());
}
Log.i("UPDATE","4");
autoText.setAdapter(null);
autoText.setAdapter(adapter);
autoText.showDropDown();
Log.i("UPDATE","showDropDown");
}
super.onPostExecute(aVoid);
}
}
}

编辑 2我也试过了,但还是不行。下拉菜单仅在我按下退格键时显示...

@Override
public void afterTextChanged(Editable editable) {
runOnUiThread(updateAdapter);
}

...

private Runnable updateAdapter = new Runnable() {

public void run(){
Log.i("ADAPTER_UPDATE","start");
adapter.notifyDataSetChanged();
autoText.showDropDown();
Log.i("ADAPTER_UPDATE","end");
}
};

注意问题是:我输入一个地址。当我输入任何内容时。但我在 logcat 中看到适配器已更新。但是下拉列表不显示。然后,当我删除一个字符时,下拉菜单会出现??!!

最佳答案

看来你真的需要实现一个过滤器:

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Filter;
import android.widget.TextView;

public class TestDoctor extends Activity {

TextView latText;
TextView lngText;
AutoCompleteTextView autoText;
ArrayAdapter<String> adapter;
private Filter filter;
private static final int ADDRESS_TRESHOLD = 10;

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

latText = (TextView) findViewById(R.id.latTextView);
lngText = (TextView) findViewById(R.id.longTextView);
autoText = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);

Button button = (Button) findViewById(R.id.buttonDoctor);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new AdapterUpdaterTask().execute();
}
});

autoText.setThreshold(ADDRESS_TRESHOLD);
autoText.setHint("Οδός Αριθμός, Περιοχή");

filter = new Filter() {
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
}

@Override
protected FilterResults performFiltering(CharSequence constraint) {
Log.i("Filter",
"Filter:" + constraint + " thread: " + Thread.currentThread());
if (constraint != null && constraint.length() > ADDRESS_TRESHOLD) {
Log.i("Filter", "doing a search ..");
new AdapterUpdaterTask().execute();
}
return null;
}
};

adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line) {
public android.widget.Filter getFilter() {
return filter;
}
};

autoText.setAdapter(adapter);
adapter.setNotifyOnChange(false);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private List<SimpleAddress> getAddressesFromText(String address) {
address = address.replace(' ', '+');
HttpHelper httpHelper = new HttpHelper();
InputStream inputStream = httpHelper
.makeHttpGetRequest("http://maps.google.com/maps/api/geocode/json?address="
+ address + "&sensor=false");
String response = httpHelper.inputStreamToString(inputStream);

List<SimpleAddress> simpleAddresses = new ArrayList<SimpleAddress>();
try {
JSONObject jsonObject = new JSONObject(response);

int size = ((JSONArray) jsonObject.get("results")).length();

for (int i = 0; i < size; i++) {
String formatted_address = ((JSONArray) jsonObject.get("results"))
.getJSONObject(i).getString("formatted_address");

Double lng = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");

Double lat = ((JSONArray) jsonObject.get("results")).getJSONObject(i)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
simpleAddresses.add(i, new SimpleAddress(formatted_address, lat, lng));
}
return simpleAddresses;
} catch (JSONException e) {
return null;
}
}

public class AdapterUpdaterTask extends AsyncTask<Void, Void, Void> {
List<SimpleAddress> simpleAddresses = new ArrayList<SimpleAddress>();

@Override
protected Void doInBackground(Void... voids) {
Log.i("UPDATE", "1");
try {
simpleAddresses = getAddressesFromText(autoText.getText().toString());
} catch (NullPointerException e) {
}
Log.i("UPDATE", "2");
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
Log.i("UPDATE", "3");

int size = simpleAddresses.size();

if (size > 0) {
adapter.clear();
Log.i("ADAPTER_SIZE", "" + size);
for (int i = 0; i < size; i++) {
adapter.add(simpleAddresses.get(i).getFormatted_address());
Log.i("ADDED", simpleAddresses.get(i).getFormatted_address());
}
Log.i("UPDATE", "4");

adapter.notifyDataSetChanged();
autoText.showDropDown();

}
super.onPostExecute(aVoid);
}
}

}

关于Android - AutoCompleteTextView 仅在退格时有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17857334/

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