zoukankan      html  css  js  c++  java
  • AsyncTask 实现异步加载

    requestMenuList(list,restaurantId);  //list是listView对象

    public void requestMenuList(ListView list,String id)
        {
           mFinder = new AsyncMenuFinder(getApplicationContext(),list,id);
           mFinder.execute();
           
        }

    private  class AsyncMenuFinder extends AsyncTask<String, Void , ArrayList<HashMap<String,String>>>{

            Context mcontext;
            ListView mlist;
            String mid;
             
            public  AsyncMenuFinder(Context context,ListView list,String id){
                mcontext = context;
                mlist = list;
                mid = id;
            }
            @Override
            protected ArrayList<HashMap<String,String>> doInBackground(String... id) {
                ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
                HashMap<String, String> map;
                HashMap<String, String> pairs = new HashMap<String, String>();
                pairs.put("rest_id", mid);
            
                try {
                    list= getData(pairs);//在这里获取数据
                    //TO DO with result
                } catch (ClientProtocolException e) {
                    Log.d(getClass().getSimpleName(), "", e);
                } catch (IOException e) {
                    Log.d(getClass().getSimpleName(), "", e);
                } catch (AccountInvalidException e) {
                    Log.d(getClass().getSimpleName(), "", e);
                } catch (JSONException e) {
                    Log.d(getClass().getSimpleName(), "", e);
                }
              
                return list;
            }
            
            protected void onPostExecute(ArrayList<HashMap<String, String>> list){

        //doInBackground返回的list就是这里的形参

        //mcontext是上下文环境
                MenuListAdapter listAdapter = new MenuListAdapter(mcontext, list);
                mlist.setAdapter(listAdapter);
               
            }
            
        }

       class MenuListAdapter extends BaseAdapter {

            private LayoutInflater inflater;
            private ArrayList<HashMap<String, String>> data;

            private class MenuItem {
                private TextView foodId;
                private TextView foodName;
           
            }

            public MenuListAdapter(Context context, ArrayList<HashMap<String, String>> data) {
                this.data = data;
                this.inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return data.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return data.get(position);
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return position;
            }

            @Override
            public View getView(int position, View view, ViewGroup arg2) {
                // TODO Auto-generated method stub
                MenuItem item;

                if (view == null) {
                    view = inflater.inflate(R.layout.recipe_list_item, null);
                    item = new MenuItem();
                    item.foodId = (TextView) view.findViewById(R.id.recipeId);
                    item.foodName = (TextView) view.findViewById(R.id.recipeName);
     

                    view.setTag(item);
                } else {
                    item = (MenuItem) view.getTag();
                }
                item.foodId.setText(data.get(position).get("recipeId"));
                item.foodName.setText(data.get(position).get("recipeName"));
     

                return view;
            }
        }

  • 相关阅读:
    学习笔记 css3--选择器&新增颜色模式&文本相关
    HTML5之新增标签用途及应用场景
    js中邦定事件与解绑支持匿名函数
    常用网站开发类Firefox扩展插件 (转)
    使用Easy4net编写代码生成器
    jQuery分页插件jBootstrapPage,一个Bootstrap风格的分页插件
    使用jQuery插件jRemoteValidate进行远程ajax验证,可以自定义返回的信息
    (新)自己动手写ORM框架(1)-增删查改的使用
    (3) iOS开发之UI处理-UIView篇
    (2) iOS开发之UI处理-UILabel篇
  • 原文地址:https://www.cnblogs.com/liu666bin/p/2826158.html
Copyright © 2011-2022 走看看