Android AsyncTask异步任务(二)

  之前我们讲过了AsyncTask 的生命周期(onPreExecute-->doInBackground-->onProgressUpdate-->onPostExecute),今天我们就来写一个模拟AsyncTask获取数据库/网络数据。

三军未动,粮草先行

  mian_activity.xml

<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.test1.MainActivity" > <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点我看数据"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/barNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:visibility="gone"
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView> </LinearLayout>

再创建一个layout Xml -->items.xml  用来填充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/name"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
/>
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
/> </LinearLayout>

Ui线程-MainActivity

public class MainActivity extends Activity {

    private BaseAdapter adapter;
private ListView lv;
private List<User> userlist =new ArrayList<User>();
private MyTask task ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
lv = (ListView) findViewById(R.id.listView1);
btn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
lv.setVisibility(View.VISIBLE);
lv.setAdapter(adapter); task = new MyTask(MainActivity.this);
task.execute(userlist,adapter);
}
});
adapter = new BaseAdapter() { @Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
View view;
if(convertView==null){
view = inflater.inflate(R.layout.items, null);
}else{
view = convertView;
}
User user = userlist.get(position);
Log.i("userlist", ""+userlist.size()+user);
TextView name = (TextView) view.findViewById(R.id.name);
TextView age = (TextView) view.findViewById(R.id.age);
TextView desc = (TextView) view.findViewById(R.id.desc);
name.setText( user.getName() );
age.setText(user.getAge()+"");
desc.setText(user.getDesc()); return view;
} @Override
public long getItemId(int position) {
return 0;
} @Override
public Object getItem(int position) {
return null;
} @Override
public int getCount() {
return userlist.size();
}
};
}
}

AsyncTask-->MyTask

public class MyTask extends AsyncTask{
private List<User> userlist;
private BaseAdapter adapter;
private MainActivity activity;
private String barNum;
public MyTask(MainActivity activity) {
this.activity = activity;
} @Override
protected Object doInBackground(Object... params) {
userlist = (List<User>) params[0];
adapter = (BaseAdapter) params[1];
for(int i=0 ;i<=100;i++){ try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
userlist.add(new User("zhangsan"+i,18+i,"aa"+i+"aa"));
publishProgress(i);
barNum = i+"%";
}
return userlist;
}
@Override
protected void onProgressUpdate(Object... values) { Button btn = (Button) activity.findViewById(R.id.button1);
btn.setVisibility(View.GONE);
TextView num = (TextView) activity.findViewById(R.id.barNum);
int bar = Integer.parseInt(values[0].toString());
ProgressBar progressBar = (ProgressBar) activity.findViewById(R.id.seekBar1);
progressBar.setProgress(bar);
num.setText(barNum); adapter.notifyDataSetChanged();
} }

Ok,运行看效果:

  Android AsyncTask异步任务(二)Android AsyncTask异步任务(二)

谢谢大家的支持。The human race has one really effective weapon, and that is laughter.

上一篇:Docker 小记 — Compose & Swarm


下一篇:swift学习初步(三)--控制流操作