我的代码需要帮助,我尝试实现自动完成 textview 但不起作用。
我使用了一个 asyntask 和一个 httpclient 从谷歌获取信息。
我的代码是:
公共类 MainActivity 扩展 Activity {
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;
}
}
谢谢你。