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;
            }
        }

  • 相关阅读:
    IntelliJ如何设置自动导包
    203.数的表示
    202.磁悬浮动力系统应用研究与模型搭建
    201.一种六磁子交通系统
    200.软件工程_期末_李振宏老师
    199.维护
    SSH学习-Struts2中的session
    SSH学习-Struts2消息传递机制
    SSH学习-struts2配置基本步骤
    云笔记项目-MyBatis关联映射查询
  • 原文地址:https://www.cnblogs.com/liu666bin/p/2826158.html
Copyright © 2011-2022 走看看