使用AutoCompleteTextView和AsyncTask 檢索城市

使用AutoCompleteTextView和AsyncTask 檢索城市android

功能:一個AutoCompleteTextView 輸入一個城市名的第一個字時
自動檢索全部城市第一個有相同的字
//記得配置聯網權限網絡

一、在佈局界面佈局 activity_main.xmlide

代碼佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >
<!-- 只檢索一個字 completionThreshold="1"-->
    <AutoCompleteTextView
        android:id="@+id/autocompletetextview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="請輸入要查找的城市"
        android:completionThreshold="1" />this

</RelativeLayout>url

-------------------------xml

二、MainActivity 類utf-8

代碼ci

public class MainActivity extends Activity {
private AutoCompleteTextView auto;
private ArrayAdapter<String> adapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  this.auto = (AutoCompleteTextView) this.findViewById(R.id.autocompletetextview);
 
  
  //須要有該文件
  String url = "http://192.168.10.50:8080/xml/citys_weather.xml";
  new MyAsyncTask().execute(url);
 }
 
 class MyAsyncTask extends AsyncTask<String, Void, byte[]>{get

  @Override  protected byte[] doInBackground(String... params) {   String url = params[0];   HttpGet get = new HttpGet(url);   HttpClient client = new DefaultHttpClient();   try {    HttpResponse response = client.execute(get);    if(response.getStatusLine().getStatusCode() == 200){     return EntityUtils.toByteArray(response.getEntity());    }   } catch (ClientProtocolException e) {    // TODO Auto-generated catch block    e.printStackTrace();   } catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }   return null;     }  @Override  protected void onPostExecute(byte[] result) {   super.onPostExecute(result);   if(result != null){    //Xml.newPullParser -- 解析器    XmlPullParser parser = Xml.newPullParser();    ByteArrayInputStream bis = new ByteArrayInputStream(result);    try {     parser.setInput(bis, "utf-8");     List<String> list = city_list(parser);      adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,list);      auto.setAdapter(adapter);    } catch (XmlPullParserException e) {     // TODO Auto-generated catch block     e.printStackTrace();    }   }else{    Toast.makeText(MainActivity.this, "網絡異常", Toast.LENGTH_SHORT).show();   }  }  private List<String> city_list(XmlPullParser parser) {   List<String> list = new ArrayList<String>();   try {    int event = parser.getEventType();    while(event != XmlPullParser.END_DOCUMENT){     switch(event){     case XmlPullParser.START_TAG:      if("d".equals(parser.getName())){       list.add(parser.nextText());      }     }     event = parser.next();    }   } catch (Exception e) {    // TODO Auto-generated catch block    e.printStackTrace();   }      return list;  }   }}

相關文章
相關標籤/搜索