Android动态自动完成don';不显示
我需要代码方面的帮助,我试图实现一个自动完成的文本视图,但不起作用 我使用一个asyntask和一个httpclient从google获取信息 我的代码是: 公共类MainActivity扩展了活动{Android动态自动完成don';不显示,android,autocomplete,textview,Android,Autocomplete,Textview,我需要代码方面的帮助,我试图实现一个自动完成的文本视图,但不起作用 我使用一个asyntask和一个httpclient从google获取信息 我的代码是: 公共类MainActivity扩展了活动{ AutoCompleteTextView text_search; ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCre
AutoCompleteTextView text_search;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_search = (AutoCompleteTextView)this.findViewById(R.id.tx_search);
ArrayList<String> entries = new ArrayList<String>(Arrays.asList("Dowgo"));
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, entries);
text_search.setAdapter(adapter);
text_search.setThreshold(1);
adapter.setNotifyOnChange(true);
text_search.addTextChangedListener(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) {
new AsyncTask<String,Void,String[]>(){
@Override
protected void onPreExecute() {
text_search.setAdapter(null);
}
@Override
protected String[] doInBackground(String... strings) {
int lon = strings.length;
if (lon<1) return null;
String busqueda = strings[0];
String url = "http://clients1.google.com/complete/search";
if (busqueda.length() < 2) return null;
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0");
if(!url.endsWith("?"))
url += "?";
List<NameValuePair> params = new LinkedList<NameValuePair>();
params.add(new BasicNameValuePair("q", busqueda));
params.add(new BasicNameValuePair("nolabels", "t"));
params.add(new BasicNameValuePair("client", "youtube"));
params.add(new BasicNameValuePair("ds", "yt"));
String paramString = URLEncodedUtils.format(params, "utf-8");
url += paramString;
HttpGet httpget = new HttpGet(url);
//httpget.addHeader("Accept-Language", "es");
ArrayAdapter adaptador;
try {
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode() != 200) return null;
HttpEntity resEntityGet = response.getEntity();
String respuesta = EntityUtils.toString(resEntityGet);
respuesta = respuesta.substring(respuesta.indexOf("[["), respuesta.indexOf("]]") + 2);
String[] resultados = respuesta.split(",");
int lens = 0;
if(resultados.length%2==0) lens = resultados.length/2;
else lens = (resultados.length-1)/2;
String[] sugeridos = new String[lens];
int con = 0;
for(int x=0;x<resultados.length-1;x+=2){
sugeridos[con] = resultados[x].replace("[","").replace("\"","");
con++;
}
Log.d("Respuesta",respuesta);
return sugeridos;
} catch (ClientProtocolException e) {
return null;
} catch (IOException e) {
return null;
}
}
@Override
protected void onPostExecute(String[] result) {
if (result != null){
adapter.clear();
for(int x=0;x<result.length;x++) {
adapter.add(result[x]);
}
adapter.notifyDataSetChanged();
text_search.showDropDown();
}else{
adapter.clear();
adapter.notifyDataSetChanged();
}
}
}.execute(charSequence.toString());
}
@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;
}
AutoCompleteTextView文本搜索;
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text\u search=(AutoCompleteTextView)this.findviewbyd(R.id.tx\u search);
ArrayList条目=新的ArrayList(Arrays.asList(“Dowgo”);
adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,条目);
text_search.setAdapter(适配器);
文本搜索设置阈值(1);
adapter.setNotifyOnChange(true);
text_search.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i2,int i3){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i2,int i3){
新建异步任务(){
@凌驾
受保护的void onPreExecute(){
text_search.setAdapter(null);
}
@凌驾
受保护的字符串[]doInBackground(字符串…字符串){
int lon=strings.length;
如果(lonIt)不清楚您在问什么。您是否遇到错误?运行应用程序时发生了什么?您是否尝试过使用调试器进行调试,如果是,您希望看到什么以及应用程序偏离了什么?我认为您需要重复文本\u search.setAdapter(adapter);当您更新适配器时。在onPostExecutr结尾处重复此操作。感谢您的帮助,很抱歉我的英语不好,我添加了以下行:text_search.setAdapter(适配器);text_search.showDropDown();onPostExecutr,再次感谢您。;)