使用自定义简单适配器时,如何在背景上加载图像?

时间:2014-02-23 10:09:13

标签: android android-asynctask loadimage

任何人都可以帮我解决这个问题吗? 我希望自定义列表视图可以在后台加载照片,这样可以减少一些响应时间。

此外,任何人都可以告诉我,如果我想使用这个自定义列表视图加载更多记录,它怎么能实现?

谢谢!

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new        StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy); 
    setContentView(R.layout.activity);
    //ProgressDialog dialog = ProgressDialog.show(this, "", "Loading...", true);
    //new Thread(new Runnable() {
       // public void run() {
        list = (ListView) findViewById(R.id.MyListView);    mylist = new ArrayList<HashMap<String, Object>>(); 





        try{
        LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, true);
       Location location = locationManager.getLastKnownLocation(provider);
        lat = location.getLatitude();
        lon = location.getLongitude();
       mylist=latlng(mylist,lat,lon,From);
        }catch(Exception e){
            //ShowMsgDialog(e.toString());

        }
        mSchedule = new SimpleAdapter(this, mylist,  R.layout.texteest,new String[] {"textView1","imageView1","textView3"},new int[] {R.id.textView1,R.id.imageView1,R.id.textView3});  
        mSchedule.setViewBinder(new MyViewBinder());
            list.setAdapter(mSchedule);  
            Button btn_map;
            btn_map=(Button)findViewById(R.id.button1);

            list.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {



                    Intent i = new Intent(this,"other_java".class);
                    startActivity(i);
                }
        });
            btn_map.setOnClickListener(new ImageButton.OnClickListener(){   
            public void onClick(View v) {
                //mapresult = Map.showmap;

                    Intent i = new Intent(this,Map.class);
                    startActivity(i);
                }});

        }


private ArrayList<HashMap<String, Object>> latlng(ArrayList<HashMap<String, Object>> mylist,double lat,double lon, int from2) {

        try{

            ArrayList<NameValuePair> params =new ArrayList<NameValuePair>();    
               params.add(new BasicNameValuePair("lat",String.valueOf(lat)));
              params.add(new BasicNameValuePair("lng",String.valueOf(lon)));
             params.add(new BasicNameValuePair("fromwhere",String.valueOf(From)));

               result = httprequestjava.executeQuery(params);


       mapresult = result;
      result = result.substring(1);

       JSONArray jsonArray = new JSONArray(result);


    //   result = "";
       for(int i = 0; i < jsonArray.length(); i++) {
           JSONObject jsonData = jsonArray.getJSONObject(i);

            HashMap<String, Object> map = new HashMap<String, Object>();  
            map.put("textView1",jsonData.getString("somecontent"));  
          myUrl = new URL(jsonData.getString("img_src"));
               InputStream inputStream = (InputStream)myUrl.getContent();
               Drawable drawable = Drawable.createFromStream(inputStream, null);
               Bitmap mIcon1 =BitmapFactory.decodeStream(myUrl.openConnection().getInputStream());
           map.put("imageView1",mIcon1);  
           map.put("textView3",jsonData.getString("distance")+"m");
            //map.put("ItemText", "This is text.....");  
           //type is not yet done
            mylist.add(map);


       }
       }catch(Exception e){
           Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
       }

        return mylist;

}



class MyViewBinder implements ViewBinder
{
    @Override
    public boolean setViewValue(View view, Object data,
            String textRepresentation) {
        if((view instanceof ImageView)&(data instanceof Bitmap))
        {
            try{
            ImageView iv = (ImageView)view;
            Bitmap bmp = (Bitmap)data;
            iv.setImageBitmap(bmp);
            }catch(Exception e)
            {
                 Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return false;
    }


}

//auto gen

}

1 个答案:

答案 0 :(得分:0)

使用Universal Image Loader库可以更轻松地完成图片下载任务。 它将使您的任务变得更加简单,并有助于保持代码清洁。