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

原文地址:https://www.cnblogs.com/liu666bin/p/2826158.html