1、Android异步操作
··之前说过用Thread和Handler可以再Android中实现异步处理,这里讨论的使用AsyncTask类来实现异步处理,相比之下,运用这种方法来完成异步操作更为简单。贴个地址:http://blog.csdn.net/mylzc/article/details/6772129
1)AsyncTask类中有许多函数,在执行AsyncTask时对应的条用顺序为:
onPreExxcute()-->doInBackground()-->onPostExecute();
2)onRreExecute是来准备开启一个新线程的,它是在UI线程中完成的,因此可以修改布局。
3)doInBackground是你想要异步执行的内容,它不是在UI线程中,因此不可以修改布局。
4)onPostExecute是任务结束后执行的内容,它是在UI线程中执行,因此也可以修改布局。
5)如果想要在doInBackground运行过程中更新布局,可以再doInBackground中调用publishProgress函数,该函数调用时会执行onProgressUpdate函数,该函数可以修改UI布局。
6)上述几个函数实际上都带有参数,这些参数是定义AsyncTask子类时定义的。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.click).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new async().execute();//执行任务,execute里面实际上是第一个参数
}
});
}
class async extends AsyncTask<Void , Integer , Void> {//三个参数类型
@Override
protected Void doInBackground(Void... params) {//返回值是第三个参数
// TODO Auto-generated method stub
for(int i = 0; i<=50 ; i++){
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(i);//传入的参数是第二个参数
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
TextView text = (TextView)findViewById(R.id.click);
text.setText(""+values[0]);
super.onProgressUpdate(values);
}
}
2、Http基础操作
1)常用的发送Http请求的方式有两种,get和post
2)想要获得http返回的数据,首先要生成一个请求包。
3)生成一个客户端对象。
4)客户端执行请求包。
5)得到响应对象。
6)从响应对象获取返回数据。代码如下:(Mars老师代码)
requestButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//生成一个请求对象
HttpGet httpGet = new HttpGet("http://www.baidu.com");
//生成一个Http客户端对象
HttpClient httpClient = new DefaultHttpClient();
//使用Http客户端发送请求对象
InputStream inputStream = null;
try {
httpResponse = httpClient.execute(httpGet);
//获取数据流,将数据流转为字符串
httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String result = "";
String line = "";
while((line = reader.readLine()) != null){
result = result + line;
}
Toast.makeText(MainActivity.this, result, 2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try{
inputStream.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
});
最后再贴个地址...http://www.oschina.net/code/snippet_12_5909
3、ListView的使用方法
1)要实现ListView,一种是直接使用ListView创建,另一种是将Activity继承ListActivity,这里只谈第一种,第二种感觉实际用处不大。
2)首先需要创建一个包含ListView的布局文件。
3)然后需要在创建一个布局文件,这个布局文件实际上是用来显示ListView中每一个元素的布局。
4)然后需要在Activity中创建一个ArrayList,ArrayList中每个元素都是一个HashMap,HashMap中可以包含多个键值对。
5)创建一个SimpleAdapter对象,这个对象是用来将ListView中显示的内容和控件一一对应起来,最后调用ListView的setAdapter函数即可。
包含ListView 的布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
ListView中每个元素的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Activity中的代码:
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();//创建一个ArrayList对象
ListView list = (ListView)findViewById(R.id.list);
HashMap<String, String> hashMap1 = new HashMap<String, String>();
hashMap1.put("name", "zhangsan");
hashMap1.put("age", "10");
arrayList.add(hashMap1);
HashMap<String, String> hashMap2 = new HashMap<String, String>();
hashMap2.put("name", "wanger");
hashMap2.put("age", "20");
arrayList.add(hashMap2);
SimpleAdapter adapter = new SimpleAdapter(this,arrayList,R.layout.layout4list,new String[]{"name","age"},new int[]{R.id.text1,R.id.text2});//创建一个SimpleAdapter,这位ListView的产生带来的方便
list.setAdapter(adapter);
//android.widget.SimpleAdapter.SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
需要注意的是Adapter的种类有很多,因此创建ListView的方法也有很多,这里只是介绍一种,这种方法比较全面。