android的异步加载,android 异步加载布局

android中listview的数据的同步与异步加载有什么区别,效果有什么不同?

目前没有同步加载数据这种做法,如果网络延迟主界面UI就卡死了,

垦利网站建设公司创新互联,垦利网站设计制作,有大型网站制作公司丰富经验。已为垦利1000多家提供企业网站建设服务。企业网站搭建\外贸网站制作要多少钱,请找那个售后服务好的垦利做网站的公司定做!

之后用户不耐烦就只能强行关闭了,卡死的时候按键都没反应的。

一个简单的的多线程

class updatelocationTask extends AsyncTaskString, Integer, Response {

protected void onPreExecute() {

//这里写执行doInBackground方法之前要做的什么,比如说弹出ProgressDialog

}

}

@Override

protected Response doInBackground(String... params) {

//这里就是线程里面的方法了,比如说建立连接,请求数据

}

}

protected void onPostExecute(Response result) {

//这里可以根据返回值来确定怎么操作,比如说刷新列表或者提示用户网络不畅,是否再次刷新

}

}

}

}

android 异步加载有几种方法

子线程没有控制并发数量,当并发过多的时候异步方法的作用就体现出来了。

异步是相对于同步而言的,顾名思义,同步就是各个通讯节点之间有统一的时钟,按照相同的时钟工作,异步相反,各节点之间没有统一的时钟,每个节点按照自己内部的时钟工作。

android在所有Thread当中,有一个Thread,我们称之为UI Thread。UI

Thread在Android程序运行的时候就被创建,是一个Process当中的主线程Main

Thread,主要是负责控制UI界面的显示、更新和控件交互。在Android程序创建之初,一个Process呈现的是单线程模型,所有的任务都在一个线程中运行。因此,我们认为,UI

Thread所执行的每一个函数,所花费的时间都应该是越短越好。而其他比较费时的工作(访问网络,下载数据,查询数据库等),都应该交由子线程去执行,以免阻塞主线程。

android adapter是异步加载的吗

1、Adapter不属于异步加载,获取Adapter需要的数据后,在UI线程中调用setAdapter()设置数据,百度一下TeachCourse,就知道了!

Android 异步加载数据 创建子进程下载数据,ListView第一次加载无数据,第二次加载载才有数据

因为是异步的,你下载完数据;需要再 进行 adapter.notifyDataSetChanged();

android异步网络加载怎么实现

以自定义ListView,异步加载网络图片示例,总结了Android开发过程中,常用的三种异步加载的技术方案。

相关资源:

manifest xmlns:android=""    

02        package="com.doodle.asycntasksample"    

03        android:versionCode="1"    

04        android:versionName="1.0"     

05         

06        uses-sdk    

07            android:minSdkVersion="8"    

08            android:targetSdkVersion="15" /    

09         

10        uses-permission android:name="android.permission.INTERNET" /    

11         

12        application    

13            android:icon="@drawable/ic_launcher"    

14            android:label="@string/app_name"    

15            android:theme="@style/AppTheme"     

16            activity    

17                android:name="com.doodle.asynctasksample.ThreadHandlerPostActivity"     

18            /activity    

19            activity android:name="com.doodle.asynctasksample.AsyncTastActivity"     

20            /activity    

21            activity android:name="com.doodle.asynctasksample.ThreadHandlerActivity"     

22            /activity    

23            activity    

24                android:name="com.doodle.asynctasksample.BootActivity"    

25                android:label="@string/title_activity_boot"     

26                intent-filter    

27                    action android:name="android.intent.action.MAIN" /    

28                    category android:name="android.intent.category.LAUNCHER" /    

29                /intent-filter    

30            /activity    

31        /application    

32         

33    /manifest    

list_item.xml

01    RelativeLayout xmlns:android=""    

02        xmlns:tools=""    

03        android:layout_width="match_parent"    

04        android:layout_height="match_parent"     

05         

06        LinearLayout    

07            android:layout_width="match_parent"    

08            android:layout_height="150dp"    

09            android:layout_alignParentLeft="true"    

10            android:layout_alignParentRight="true"    

11            android:layout_alignParentTop="true"     

12         

13            ImageView    

14                android:id="@+id/imageView"    

15                android:layout_width="match_parent"    

16                android:layout_height="match_parent"    

17                android:src="a href="" target="_blank"rel="nofollow"@android/a :drawable/alert_dark_frame" /    

18         

19        /LinearLayout    

20         

21    /RelativeLayout    

ImageAdapter.java

01    /**    

02     * Create a customized data structure for each item of ListView.    

03     * An ImageAdapter inherited from BaseAdapter must overwrites    

04     * getView method to show every image in specified style.In this    

05     * instance only a ImageView will put and fill in each item of    

06     * ListView.    

07     *    

08     * @author Jie.Geng Aug 01, 2012.    

09     *    

10     */    

11    public class ImageAdapter extends BaseAdapter {    

12        private Context context;    

13        private ListHashMapString, Object listItems;    

14        private LayoutInflater listContainer;    

15             

16        public ImageView imageView;    

17             

18        public ImageAdapter(Context context, ListHashMapString, Object listItems) {    

19            super();    

20            this.context = context;    

21            this.listContainer = LayoutInflater.from(context);    

22            this.listItems = listItems;    

23        }    

24         

25        @Override    

26        public int getCount() {    

27            return listItems.size();    

28        }    

29         

30        @Override    

31        public Object getItem(int position) {    

32            return null;    

33        }    

34         

35        @Override    

36        public long getItemId(int position) {    

37            return 0;    

38        }    

39         

40        @Override    

41        public View getView(int position, View convertView, ViewGroup parent) {    

42            if(convertView == null) {    

43                convertView = listContainer.inflate(R.layout.list_item, null);    

44                imageView = (ImageView) convertView.findViewById(R.id.imageView);    

45                convertView.setTag(imageView);    

46            } else {    

47                imageView = (ImageView) convertView.getTag();    

48            }    

49            imageView.setImageDrawable((Drawable) listItems.get(position).get("ItemImage"));    

50            return convertView;    

51        }

Handler简介 Handler为Android提供了一种异步消息处理机制,它包含两个队列,一个是线程列队,另一个是消息列队。使用post方法将线 程对象添加到线程队列中,使用sendMessage(Message message)将消息放入消息队列中。当向消息队列中发送消息后就立 即返回,而从消息队列中读取消息对象时会阻塞,继而回调Handler中public void handleMessage(Message msg)方法。因此 在创建Handler时应该使用匿名内部类重写该方法。如果想要这个流程一直执行的话,可以再run方法内部执行postDelay或者 post方法,再将该线程对象添加到消息队列中重复执行。想要停止线程,调用Handler对象的removeCallbacks(Runnable r)从 线程队列中移除线程对象,使线程停止执行。

android service异步网络加载怎么实现

**

* 封装ProecssDialog对话框

*

*/

public class LoadDialog extends ProgressDialog {

private String title = "进度对话框";

private String message = "加载数据中....";

public LoadDialog(Context context, int theme) {

super(context, theme);

}

/**

* 用默认的标题和内容来创建对话框

* @param context

*/

public LoadDialog(Context context) {

super(context);

initDialog();

}

/**

* 用指定的标题和内容来创建对话框

* @param context

* @param title

* @param message

*/

public LoadDialog(Context context,String title,String message){

super(context);

if(title != null){

this.title = title;

}

if(message != null){

this.message = message;

}

initDialog();

}

/**

* 初始化对话框参数,默认对话框不可以取消

*/

public void initDialog(){

setTitle(title);

setMessage(message);

setProgressStyle(ProgressDialog.STYLE_SPINNER);

setCancelable(false);

}

/**

* 打开对话框,设置回调方法,传递需要执行业务方法的类模板,方法名和参数列表

* @param callback 回调方法,该方法在对话框关闭后回调,并获取返回的数据

* @param serviceClass 执行业务方法的类模板

* @param method 执行业务方法的方法名

* @param params 执行业务方法的参数列表

*/

public void execute(Callback callback,Class serviceClass,String method,Object... params){

super.show();

ServiceAysnTask task = new ServiceAysnTask(callback,serviceClass,method);

task.execute(params);

}

/**

* 回调方法的接口

*

*/

public interface Callback{

public void getResult(Map map);

}

/**

* 与远程服务通信的线程类

* @author BDK

* AsyncTask 异步任务

*/

private class ServiceAysnTask extends AsyncTaskobject,object,map{

private Class serviceClass;

private String method;

private Callback callback;

public ServiceAysnTask(Callback callback,Class serviceClass,String method){

this.callback = callback;

this.serviceClass = serviceClass;

this.method = method;

}

@Override

protected Map doInBackground(Object... params) {

Map resultMap = null;

try {

Object obj = serviceClass.newInstance();//创建类模板对象

Class [] paramTypes = new Class[params.length];

for (int i = 0; i paramTypes.length; i++) {

paramTypes[i] = params[i].getClass();

}

//根据类模板得到方法

Method m = serviceClass.getMethod(method, paramTypes);

resultMap = (Map) m.invoke(obj, params);

} catch (Exception e) {

e.printStackTrace();

}

LoadDialog.this.cancel();

return resultMap;

}

@Override

protected void onPostExecute(Map result) {

super.onPostExecute(result);

if(result == null){

Toast.makeText(LoadDialog.this.getContext(), "网络通信异常", Toast.LENGTH_LONG).show();

return;

}

callback.getResult(result);

}

}

}

/object,object,map


文章标题:android的异步加载,android 异步加载布局
本文路径:http://scyanting.com/article/hogjod.html